blob: d74588e6285ae4998eb6db7a57497e37523b673d [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);
Bill Wendlingfef06052008-09-16 21:48:12 +0000271 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 if (Subtarget->is64Bit()) {
273 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
274 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
275 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000276 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 }
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 Johannesen9011d872008-09-29 22:25:26 +0000300 setOperationAction(ISD::ATOMIC_LOAD_SUB_8 , MVT::i8, Custom);
301 setOperationAction(ISD::ATOMIC_LOAD_SUB_16, MVT::i16, Custom);
302 setOperationAction(ISD::ATOMIC_LOAD_SUB_32, MVT::i32, Custom);
303 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000304
Dale Johannesenf160d802008-10-02 18:53:47 +0000305 if (!Subtarget->is64Bit()) {
306 setOperationAction(ISD::ATOMIC_LOAD_ADD_64, MVT::i64, Custom);
307 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Custom);
308 setOperationAction(ISD::ATOMIC_LOAD_AND_64, MVT::i64, Custom);
309 setOperationAction(ISD::ATOMIC_LOAD_OR_64, MVT::i64, Custom);
310 setOperationAction(ISD::ATOMIC_LOAD_XOR_64, MVT::i64, Custom);
311 setOperationAction(ISD::ATOMIC_LOAD_NAND_64, MVT::i64, Custom);
312 setOperationAction(ISD::ATOMIC_SWAP_64, MVT::i64, Custom);
313 }
314
Dan Gohman472d12c2008-06-30 20:59:49 +0000315 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
316 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 // FIXME - use subtarget debug flags
318 if (!Subtarget->isTargetDarwin() &&
319 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000320 !Subtarget->isTargetCygMing()) {
321 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
322 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
323 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324
325 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
326 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
327 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
328 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
329 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 setExceptionPointerRegister(X86::RAX);
331 setExceptionSelectorRegister(X86::RDX);
332 } else {
333 setExceptionPointerRegister(X86::EAX);
334 setExceptionSelectorRegister(X86::EDX);
335 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000336 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000337 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
338
Duncan Sands7407a9f2007-09-11 14:10:23 +0000339 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000340
Chris Lattner56b941f2008-01-15 21:58:22 +0000341 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000342
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
344 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000346 if (Subtarget->is64Bit()) {
347 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000349 } else {
350 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000352 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353
354 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
355 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
356 if (Subtarget->is64Bit())
357 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
358 if (Subtarget->isTargetCygMing())
359 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
360 else
361 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
362
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000363 if (X86ScalarSSEf64) {
364 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 // Set up the FP register classes.
366 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
367 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
368
369 // Use ANDPD to simulate FABS.
370 setOperationAction(ISD::FABS , MVT::f64, Custom);
371 setOperationAction(ISD::FABS , MVT::f32, Custom);
372
373 // Use XORP to simulate FNEG.
374 setOperationAction(ISD::FNEG , MVT::f64, Custom);
375 setOperationAction(ISD::FNEG , MVT::f32, Custom);
376
377 // Use ANDPD and ORPD to simulate FCOPYSIGN.
378 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
379 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
380
381 // We don't support sin/cos/fmod
382 setOperationAction(ISD::FSIN , MVT::f64, Expand);
383 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384 setOperationAction(ISD::FSIN , MVT::f32, Expand);
385 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386
387 // Expand FP immediates into loads from the stack, except for the special
388 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000389 addLegalFPImmediate(APFloat(+0.0)); // xorpd
390 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000391
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000392 // Floating truncations from f80 and extensions to f80 go through memory.
393 // If optimizing, we lie about this though and handle it in
394 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
395 if (Fast) {
396 setConvertAction(MVT::f32, MVT::f80, Expand);
397 setConvertAction(MVT::f64, MVT::f80, Expand);
398 setConvertAction(MVT::f80, MVT::f32, Expand);
399 setConvertAction(MVT::f80, MVT::f64, Expand);
400 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000401 } else if (X86ScalarSSEf32) {
402 // Use SSE for f32, x87 for f64.
403 // Set up the FP register classes.
404 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
405 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
406
407 // Use ANDPS to simulate FABS.
408 setOperationAction(ISD::FABS , MVT::f32, Custom);
409
410 // Use XORP to simulate FNEG.
411 setOperationAction(ISD::FNEG , MVT::f32, Custom);
412
413 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
414
415 // Use ANDPS and ORPS to simulate FCOPYSIGN.
416 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
417 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
418
419 // We don't support sin/cos/fmod
420 setOperationAction(ISD::FSIN , MVT::f32, Expand);
421 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000422
Nate Begemane2ba64f2008-02-14 08:57:00 +0000423 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000424 addLegalFPImmediate(APFloat(+0.0f)); // xorps
425 addLegalFPImmediate(APFloat(+0.0)); // FLD0
426 addLegalFPImmediate(APFloat(+1.0)); // FLD1
427 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
428 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
429
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000430 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
431 // this though and handle it in InstructionSelectPreprocess so that
432 // dagcombine2 can hack on these.
433 if (Fast) {
434 setConvertAction(MVT::f32, MVT::f64, Expand);
435 setConvertAction(MVT::f32, MVT::f80, Expand);
436 setConvertAction(MVT::f80, MVT::f32, Expand);
437 setConvertAction(MVT::f64, MVT::f32, Expand);
438 // And x87->x87 truncations also.
439 setConvertAction(MVT::f80, MVT::f64, Expand);
440 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000441
442 if (!UnsafeFPMath) {
443 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
444 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
445 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000447 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 // Set up the FP register classes.
449 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
450 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
451
452 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
453 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
454 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
455 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000456
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000457 // Floating truncations go through memory. If optimizing, we lie about
458 // this though and handle it in InstructionSelectPreprocess so that
459 // dagcombine2 can hack on these.
460 if (Fast) {
461 setConvertAction(MVT::f80, MVT::f32, Expand);
462 setConvertAction(MVT::f64, MVT::f32, Expand);
463 setConvertAction(MVT::f80, MVT::f64, Expand);
464 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465
466 if (!UnsafeFPMath) {
467 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
468 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
469 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000470 addLegalFPImmediate(APFloat(+0.0)); // FLD0
471 addLegalFPImmediate(APFloat(+1.0)); // FLD1
472 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
473 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000474 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
475 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
476 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
477 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 }
479
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000480 // Long double always uses X87.
481 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000482 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
483 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000484 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000485 APFloat TmpFlt(+0.0);
486 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
487 addLegalFPImmediate(TmpFlt); // FLD0
488 TmpFlt.changeSign();
489 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
490 APFloat TmpFlt2(+1.0);
491 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
492 addLegalFPImmediate(TmpFlt2); // FLD1
493 TmpFlt2.changeSign();
494 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
495 }
496
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000497 if (!UnsafeFPMath) {
498 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
499 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
500 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000501
Dan Gohman2f7b1982007-10-11 23:21:31 +0000502 // Always use a library call for pow.
503 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
504 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
505 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
506
Dale Johannesen92b33082008-09-04 00:47:13 +0000507 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000508 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000509 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000510 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000511 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);
Dale Johannesen177edff2008-09-10 17:31:40 +0000555 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 }
561
562 if (Subtarget->hasMMX()) {
563 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
564 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
565 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000566 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
568
569 // FIXME: add MMX packed arithmetics
570
571 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
572 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
573 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
574 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
575
576 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
577 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
578 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000579 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580
581 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
582 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
583
584 setOperationAction(ISD::AND, MVT::v8i8, Promote);
585 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
586 setOperationAction(ISD::AND, MVT::v4i16, Promote);
587 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
588 setOperationAction(ISD::AND, MVT::v2i32, Promote);
589 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
590 setOperationAction(ISD::AND, MVT::v1i64, Legal);
591
592 setOperationAction(ISD::OR, MVT::v8i8, Promote);
593 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
594 setOperationAction(ISD::OR, MVT::v4i16, Promote);
595 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
596 setOperationAction(ISD::OR, MVT::v2i32, Promote);
597 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
598 setOperationAction(ISD::OR, MVT::v1i64, Legal);
599
600 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
601 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
602 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
603 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
604 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
605 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
606 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
607
608 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
609 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
610 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
611 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
612 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
613 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000614 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
615 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
617
618 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
619 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
620 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000621 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
623
624 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
625 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
626 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
627 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
628
Evan Cheng759fe022008-07-22 18:39:19 +0000629 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
631 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000633
634 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 }
636
637 if (Subtarget->hasSSE1()) {
638 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
639
640 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
641 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
642 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
643 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
644 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
645 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
647 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
648 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
649 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
650 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000651 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652 }
653
654 if (Subtarget->hasSSE2()) {
655 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
656 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
657 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
658 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
659 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
660
661 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
662 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
663 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
664 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
665 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
666 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
667 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
668 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
669 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
670 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
671 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
672 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
673 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
674 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
675 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676
Nate Begeman03605a02008-07-17 16:51:19 +0000677 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
678 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
679 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
680 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000681
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
683 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
684 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
685 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
687
688 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000689 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
690 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000691 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000692 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000693 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000694 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
695 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
696 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 }
698 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
699 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
700 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
701 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000702 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000704 if (Subtarget->is64Bit()) {
705 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000706 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000707 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708
709 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
710 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000711 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
712 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
713 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
714 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
715 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
716 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
717 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
718 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
719 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
720 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 }
722
Chris Lattner3bc08502008-01-17 19:59:44 +0000723 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000724
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 // Custom lower v2i64 and v2f64 selects.
726 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
727 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
728 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
729 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000730
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000732
733 if (Subtarget->hasSSE41()) {
734 // FIXME: Do we need to handle scalar-to-vector here?
735 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000736 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000737
738 // i8 and i16 vectors are custom , because the source register and source
739 // source memory operand types are not the same width. f32 vectors are
740 // custom since the immediate controlling the insert encodes additional
741 // information.
742 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
743 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
744 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
745 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
746
747 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
748 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
749 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000750 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000751
752 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000753 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
754 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000755 }
756 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757
Nate Begeman03605a02008-07-17 16:51:19 +0000758 if (Subtarget->hasSSE42()) {
759 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
760 }
761
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000762 // We want to custom lower some of our intrinsics.
763 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
764
765 // We have target-specific dag combine patterns for the following nodes:
766 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000767 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000769 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770
771 computeRegisterProperties();
772
773 // FIXME: These should be based on subtarget info. Plus, the values should
774 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000775 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
776 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
777 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000779 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000780}
781
Scott Michel502151f2008-03-10 15:42:14 +0000782
Dan Gohman8181bd12008-07-27 21:46:04 +0000783MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000784 return MVT::i8;
785}
786
787
Evan Cheng5a67b812008-01-23 23:17:41 +0000788/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
789/// the desired ByVal argument alignment.
790static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
791 if (MaxAlign == 16)
792 return;
793 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
794 if (VTy->getBitWidth() == 128)
795 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000796 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
797 unsigned EltAlign = 0;
798 getMaxByValAlign(ATy->getElementType(), EltAlign);
799 if (EltAlign > MaxAlign)
800 MaxAlign = EltAlign;
801 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
802 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
803 unsigned EltAlign = 0;
804 getMaxByValAlign(STy->getElementType(i), EltAlign);
805 if (EltAlign > MaxAlign)
806 MaxAlign = EltAlign;
807 if (MaxAlign == 16)
808 break;
809 }
810 }
811 return;
812}
813
814/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
815/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000816/// that contain SSE vectors are placed at 16-byte boundaries while the rest
817/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000818unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000819 if (Subtarget->is64Bit()) {
820 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000821 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000822 if (TyAlign > 8)
823 return TyAlign;
824 return 8;
825 }
826
Evan Cheng5a67b812008-01-23 23:17:41 +0000827 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000828 if (Subtarget->hasSSE1())
829 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000830 return Align;
831}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832
Evan Cheng8c590372008-05-15 08:39:06 +0000833/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000834/// and store operations as a result of memset, memcpy, and memmove
835/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000836/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000837MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000838X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
839 bool isSrcConst, bool isSrcStr) const {
840 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
841 return MVT::v4i32;
842 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
843 return MVT::v4f32;
844 if (Subtarget->is64Bit() && Size >= 8)
845 return MVT::i64;
846 return MVT::i32;
847}
848
849
Evan Cheng6fb06762007-11-09 01:32:10 +0000850/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
851/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000852SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000853 SelectionDAG &DAG) const {
854 if (usesGlobalOffsetTable())
855 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
856 if (!Subtarget->isPICStyleRIPRel())
857 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
858 return Table;
859}
860
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861//===----------------------------------------------------------------------===//
862// Return Value Calling Convention Implementation
863//===----------------------------------------------------------------------===//
864
865#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000866
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000868SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
870
871 SmallVector<CCValAssign, 16> RVLocs;
872 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
873 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
874 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000875 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000876
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000877 // If this is the first return lowered for this function, add the regs to the
878 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000879 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880 for (unsigned i = 0; i != RVLocs.size(); ++i)
881 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000882 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000884 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000885
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000886 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000887 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000888 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000889 SDValue TailCall = Chain;
890 SDValue TargetAddress = TailCall.getOperand(1);
891 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000892 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000893 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000894 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000895 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000896 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
897 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000898 assert(StackAdjustment.getOpcode() == ISD::Constant &&
899 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000900
Dan Gohman8181bd12008-07-27 21:46:04 +0000901 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000902 Operands.push_back(Chain.getOperand(0));
903 Operands.push_back(TargetAddress);
904 Operands.push_back(StackAdjustment);
905 // Copy registers used by the call. Last operand is a flag so it is not
906 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000907 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000908 Operands.push_back(Chain.getOperand(i));
909 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000910 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
911 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000912 }
913
914 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000915 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000916
Dan Gohman8181bd12008-07-27 21:46:04 +0000917 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000918 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
919 // Operand #1 = Bytes To Pop
920 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
921
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000923 for (unsigned i = 0; i != RVLocs.size(); ++i) {
924 CCValAssign &VA = RVLocs[i];
925 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000926 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000927
Chris Lattnerb56cc342008-03-11 03:23:40 +0000928 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
929 // the RET instruction and handled by the FP Stackifier.
930 if (RVLocs[i].getLocReg() == X86::ST0 ||
931 RVLocs[i].getLocReg() == X86::ST1) {
932 // If this is a copy from an xmm register to ST(0), use an FPExtend to
933 // change the value to the FP stack register class.
934 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
935 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
936 RetOps.push_back(ValToCopy);
937 // Don't emit a copytoreg.
938 continue;
939 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000940
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000941 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000942 Flag = Chain.getValue(1);
943 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000944
945 // The x86-64 ABI for returning structs by value requires that we copy
946 // the sret argument into %rax for the return. We saved the argument into
947 // a virtual register in the entry block, so now we copy the value out
948 // and into %rax.
949 if (Subtarget->is64Bit() &&
950 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
951 MachineFunction &MF = DAG.getMachineFunction();
952 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
953 unsigned Reg = FuncInfo->getSRetReturnReg();
954 if (!Reg) {
955 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
956 FuncInfo->setSRetReturnReg(Reg);
957 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000958 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000959
960 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
961 Flag = Chain.getValue(1);
962 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000963
Chris Lattnerb56cc342008-03-11 03:23:40 +0000964 RetOps[0] = Chain; // Update chain.
965
966 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +0000967 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +0000968 RetOps.push_back(Flag);
969
970 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971}
972
973
974/// LowerCallResult - Lower the result values of an ISD::CALL into the
975/// appropriate copies out of appropriate physical registers. This assumes that
976/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
977/// being lowered. The returns a SDNode with the same number of values as the
978/// ISD::CALL.
979SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +0000980LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000981 unsigned CallingConv, SelectionDAG &DAG) {
982
983 // Assign locations to each value returned by this call.
984 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +0000985 bool isVarArg = TheCall->isVarArg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
987 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
988
Dan Gohman8181bd12008-07-27 21:46:04 +0000989 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000990
991 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000992 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +0000993 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000994
995 // If this is a call to a function that returns an fp value on the floating
996 // point stack, but where we prefer to use the value in xmm registers, copy
997 // 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 +0000998 if ((RVLocs[i].getLocReg() == X86::ST0 ||
999 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001000 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1001 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001003
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001004 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1005 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001006 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001007 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001008
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001009 if (CopyVT != RVLocs[i].getValVT()) {
1010 // Round the F80 the right size, which also moves to the appropriate xmm
1011 // register.
1012 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1013 // This truncation won't change the value.
1014 DAG.getIntPtrConstant(1));
1015 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001016
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001017 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001018 }
Duncan Sands698842f2008-07-02 17:40:58 +00001019
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 // Merge everything together with a MERGE_VALUES node.
1021 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +00001022 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
Gabor Greif1c80d112008-08-28 21:40:38 +00001023 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001024}
1025
1026
1027//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001028// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029//===----------------------------------------------------------------------===//
1030// StdCall calling convention seems to be standard for many Windows' API
1031// routines and around. It differs from C calling convention just a little:
1032// callee should clean up the stack, not caller. Symbols should be also
1033// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001034// For info on fast calling convention see Fast Calling Convention (tail call)
1035// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001036
1037/// AddLiveIn - This helper function adds the specified physical register to the
1038/// MachineFunction as a live in value. It also creates a corresponding virtual
1039/// register for it.
1040static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1041 const TargetRegisterClass *RC) {
1042 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001043 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1044 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001045 return VReg;
1046}
1047
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001048/// CallIsStructReturn - Determines whether a CALL node uses struct return
1049/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001050static bool CallIsStructReturn(CallSDNode *TheCall) {
1051 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001052 if (!NumOps)
1053 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001054
Dan Gohman705e3f72008-09-13 01:54:27 +00001055 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001056}
1057
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001058/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1059/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001060static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001061 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001062 if (!NumArgs)
1063 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001064
1065 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001066}
1067
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001068/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1069/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001070/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001071bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001072 if (IsVarArg)
1073 return false;
1074
Dan Gohman705e3f72008-09-13 01:54:27 +00001075 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001076 default:
1077 return false;
1078 case CallingConv::X86_StdCall:
1079 return !Subtarget->is64Bit();
1080 case CallingConv::X86_FastCall:
1081 return !Subtarget->is64Bit();
1082 case CallingConv::Fast:
1083 return PerformTailCallOpt;
1084 }
1085}
1086
Dan Gohman705e3f72008-09-13 01:54:27 +00001087/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1088/// given CallingConvention value.
1089CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001090 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001091 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001092 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001093 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1094 return CC_X86_64_TailCall;
1095 else
1096 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001097 }
1098
Gordon Henriksen18ace102008-01-05 16:56:59 +00001099 if (CC == CallingConv::X86_FastCall)
1100 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001101 else if (CC == CallingConv::Fast)
1102 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001103 else
1104 return CC_X86_32_C;
1105}
1106
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001107/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1108/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001109NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001110X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001111 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001112 if (CC == CallingConv::X86_FastCall)
1113 return FastCall;
1114 else if (CC == CallingConv::X86_StdCall)
1115 return StdCall;
1116 return None;
1117}
1118
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001119
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001120/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1121/// in a register before calling.
1122bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1123 return !IsTailCall && !Is64Bit &&
1124 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1125 Subtarget->isPICStyleGOT();
1126}
1127
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001128/// CallRequiresFnAddressInReg - Check whether the call requires the function
1129/// address to be loaded in a register.
1130bool
1131X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1132 return !Is64Bit && IsTailCall &&
1133 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1134 Subtarget->isPICStyleGOT();
1135}
1136
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001137/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1138/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001139/// the specific parameter attribute. The copy will be passed as a byval
1140/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001141static SDValue
1142CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001143 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001144 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001145 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001146 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001147}
1148
Dan Gohman8181bd12008-07-27 21:46:04 +00001149SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001150 const CCValAssign &VA,
1151 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001152 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001153 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001154 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001155 ISD::ArgFlagsTy Flags =
1156 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001157 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001158 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001159
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001160 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1161 // changed with more analysis.
1162 // In case of tail call optimization mark all arguments mutable. Since they
1163 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001164 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001165 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001166 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001167 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001168 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001169 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001170 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001171}
1172
Dan Gohman8181bd12008-07-27 21:46:04 +00001173SDValue
1174X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001175 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001176 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1177
1178 const Function* Fn = MF.getFunction();
1179 if (Fn->hasExternalLinkage() &&
1180 Subtarget->isTargetCygMing() &&
1181 Fn->getName() == "main")
1182 FuncInfo->setForceFramePointer(true);
1183
1184 // Decorate the function name.
1185 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1186
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001187 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001188 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001189 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001190 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001191 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001192 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001193
1194 assert(!(isVarArg && CC == CallingConv::Fast) &&
1195 "Var args not supported with calling convention fastcc");
1196
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001197 // Assign locations to all of the incoming arguments.
1198 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001199 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001200 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001201
Dan Gohman8181bd12008-07-27 21:46:04 +00001202 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001203 unsigned LastVal = ~0U;
1204 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1205 CCValAssign &VA = ArgLocs[i];
1206 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1207 // places.
1208 assert(VA.getValNo() != LastVal &&
1209 "Don't support value assigned to multiple locs yet");
1210 LastVal = VA.getValNo();
1211
1212 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001213 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001214 TargetRegisterClass *RC;
1215 if (RegVT == MVT::i32)
1216 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001217 else if (Is64Bit && RegVT == MVT::i64)
1218 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001219 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001220 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001221 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001222 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001223 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001224 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001225 else if (RegVT.isVector()) {
1226 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001227 if (!Is64Bit)
1228 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1229 else {
1230 // Darwin calling convention passes MMX values in either GPRs or
1231 // XMMs in x86-64. Other targets pass them in memory.
1232 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1233 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1234 RegVT = MVT::v2i64;
1235 } else {
1236 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1237 RegVT = MVT::i64;
1238 }
1239 }
1240 } else {
1241 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001242 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001245 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001246
1247 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1248 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1249 // right size.
1250 if (VA.getLocInfo() == CCValAssign::SExt)
1251 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1252 DAG.getValueType(VA.getValVT()));
1253 else if (VA.getLocInfo() == CCValAssign::ZExt)
1254 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1255 DAG.getValueType(VA.getValVT()));
1256
1257 if (VA.getLocInfo() != CCValAssign::Full)
1258 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1259
Gordon Henriksen18ace102008-01-05 16:56:59 +00001260 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001261 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001262 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001263 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1264 else if (RC == X86::VR128RegisterClass) {
1265 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1266 DAG.getConstant(0, MVT::i64));
1267 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1268 }
1269 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001270
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001271 ArgValues.push_back(ArgValue);
1272 } else {
1273 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001274 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001275 }
1276 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001277
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001278 // The x86-64 ABI for returning structs by value requires that we copy
1279 // the sret argument into %rax for the return. Save the argument into
1280 // a virtual register so that we can access it from the return points.
1281 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1282 MachineFunction &MF = DAG.getMachineFunction();
1283 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1284 unsigned Reg = FuncInfo->getSRetReturnReg();
1285 if (!Reg) {
1286 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1287 FuncInfo->setSRetReturnReg(Reg);
1288 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001289 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001290 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1291 }
1292
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001293 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001294 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001295 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001296 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001297
1298 // If the function takes variable number of arguments, make a frame index for
1299 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001300 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001301 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1302 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1303 }
1304 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001305 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1306
1307 // FIXME: We should really autogenerate these arrays
1308 static const unsigned GPR64ArgRegsWin64[] = {
1309 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001310 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001311 static const unsigned XMMArgRegsWin64[] = {
1312 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1313 };
1314 static const unsigned GPR64ArgRegs64Bit[] = {
1315 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1316 };
1317 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001318 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1319 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1320 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001321 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1322
1323 if (IsWin64) {
1324 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1325 GPR64ArgRegs = GPR64ArgRegsWin64;
1326 XMMArgRegs = XMMArgRegsWin64;
1327 } else {
1328 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1329 GPR64ArgRegs = GPR64ArgRegs64Bit;
1330 XMMArgRegs = XMMArgRegs64Bit;
1331 }
1332 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1333 TotalNumIntRegs);
1334 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1335 TotalNumXMMRegs);
1336
Gordon Henriksen18ace102008-01-05 16:56:59 +00001337 // For X86-64, if there are vararg parameters that are passed via
1338 // registers, then we must store them to their spots on the stack so they
1339 // may be loaded by deferencing the result of va_next.
1340 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001341 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1342 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1343 TotalNumXMMRegs * 16, 16);
1344
Gordon Henriksen18ace102008-01-05 16:56:59 +00001345 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001346 SmallVector<SDValue, 8> MemOps;
1347 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1348 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001349 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001350 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001351 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1352 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001353 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1354 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001355 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001356 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001357 MemOps.push_back(Store);
1358 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001359 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001360 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001361
Gordon Henriksen18ace102008-01-05 16:56:59 +00001362 // Now store the XMM (fp + vector) parameter registers.
1363 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001364 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001365 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001366 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1367 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001368 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1369 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001370 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001371 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001372 MemOps.push_back(Store);
1373 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001374 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001375 }
1376 if (!MemOps.empty())
1377 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1378 &MemOps[0], MemOps.size());
1379 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001380 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001381
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001382 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001383
Gordon Henriksen18ace102008-01-05 16:56:59 +00001384 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001385 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001386 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001387 BytesCallerReserves = 0;
1388 } else {
1389 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001391 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001392 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393 BytesCallerReserves = StackSize;
1394 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001395
Gordon Henriksen18ace102008-01-05 16:56:59 +00001396 if (!Is64Bit) {
1397 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1398 if (CC == CallingConv::X86_FastCall)
1399 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1400 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401
Anton Korobeynikove844e472007-08-15 17:12:32 +00001402 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001403
1404 // Return the new list of results.
Gabor Greif1c80d112008-08-28 21:40:38 +00001405 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif46bf5472008-08-26 22:36:50 +00001406 ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001407}
1408
Dan Gohman8181bd12008-07-27 21:46:04 +00001409SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001410X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001411 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001412 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001413 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001414 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001415 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001416 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001417 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001418 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();
Dan Gohman705e3f72008-09-13 01:54:27 +00001465 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1466 SDValue Chain = TheCall->getChain();
1467 unsigned CC = TheCall->getCallingConv();
1468 bool isVarArg = TheCall->isVarArg();
1469 bool IsTailCall = TheCall->isTailCall() &&
1470 CC == CallingConv::Fast && PerformTailCallOpt;
1471 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001472 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001473 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001474
1475 assert(!(isVarArg && CC == CallingConv::Fast) &&
1476 "Var args not supported with calling convention fastcc");
1477
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001478 // Analyze operands of the call, assigning locations to each operand.
1479 SmallVector<CCValAssign, 16> ArgLocs;
1480 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001481 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001482
1483 // Get a count of how many bytes are to be pushed on the stack.
1484 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001485 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001486 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001487
Gordon Henriksen18ace102008-01-05 16:56:59 +00001488 int FPDiff = 0;
1489 if (IsTailCall) {
1490 // Lower arguments at fp - stackoffset + fpdiff.
1491 unsigned NumBytesCallerPushed =
1492 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1493 FPDiff = NumBytesCallerPushed - NumBytes;
1494
1495 // Set the delta of movement of the returnaddr stackslot.
1496 // But only set if delta is greater than previous delta.
1497 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1498 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1499 }
1500
Chris Lattner5872a362008-01-17 07:00:52 +00001501 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502
Dan Gohman8181bd12008-07-27 21:46:04 +00001503 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001504 // Load return adress for tail calls.
1505 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1506 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001507
Dan Gohman8181bd12008-07-27 21:46:04 +00001508 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1509 SmallVector<SDValue, 8> MemOpChains;
1510 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001511
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001512 // Walk the register/memloc assignments, inserting copies/loads. In the case
1513 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001514 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1515 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001516 SDValue Arg = TheCall->getArg(i);
1517 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1518 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001519
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001520 // Promote the value if needed.
1521 switch (VA.getLocInfo()) {
1522 default: assert(0 && "Unknown loc info!");
1523 case CCValAssign::Full: break;
1524 case CCValAssign::SExt:
1525 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1526 break;
1527 case CCValAssign::ZExt:
1528 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1529 break;
1530 case CCValAssign::AExt:
1531 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1532 break;
1533 }
1534
1535 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001536 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001537 MVT RegVT = VA.getLocVT();
1538 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001539 switch (VA.getLocReg()) {
1540 default:
1541 break;
1542 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1543 case X86::R8: {
1544 // Special case: passing MMX values in GPR registers.
1545 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1546 break;
1547 }
1548 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1549 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1550 // Special case: passing MMX values in XMM registers.
1551 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1552 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1553 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1554 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1555 getMOVLMask(2, DAG));
1556 break;
1557 }
1558 }
1559 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001560 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1561 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001562 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001563 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001564 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001565 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1566
Dan Gohman705e3f72008-09-13 01:54:27 +00001567 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1568 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001569 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001570 }
1571 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001572
1573 if (!MemOpChains.empty())
1574 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1575 &MemOpChains[0], MemOpChains.size());
1576
1577 // Build a sequence of copy-to-reg nodes chained together with token chain
1578 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001579 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001580 // Tail call byval lowering might overwrite argument registers so in case of
1581 // tail call optimization the copies to registers are lowered later.
1582 if (!IsTailCall)
1583 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1584 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1585 InFlag);
1586 InFlag = Chain.getValue(1);
1587 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001588
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001589 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001590 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001591 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1592 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1593 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1594 InFlag);
1595 InFlag = Chain.getValue(1);
1596 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001597 // If we are tail calling and generating PIC/GOT style code load the address
1598 // of the callee into ecx. The value in ecx is used as target of the tail
1599 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1600 // calls on PIC/GOT architectures. Normally we would just put the address of
1601 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1602 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001603 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001604 // Note: The actual moving to ecx is done further down.
1605 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001606 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001607 !G->getGlobal()->hasProtectedVisibility())
1608 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001609 else if (isa<ExternalSymbolSDNode>(Callee))
1610 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001611 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001612
Gordon Henriksen18ace102008-01-05 16:56:59 +00001613 if (Is64Bit && isVarArg) {
1614 // From AMD64 ABI document:
1615 // For calls that may call functions that use varargs or stdargs
1616 // (prototype-less calls or calls to functions containing ellipsis (...) in
1617 // the declaration) %al is used as hidden argument to specify the number
1618 // of SSE registers used. The contents of %al do not need to match exactly
1619 // the number of registers, but must be an ubound on the number of SSE
1620 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001621
1622 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001623 // Count the number of XMM registers allocated.
1624 static const unsigned XMMArgRegs[] = {
1625 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1626 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1627 };
1628 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1629
1630 Chain = DAG.getCopyToReg(Chain, X86::AL,
1631 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1632 InFlag = Chain.getValue(1);
1633 }
1634
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001635
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001636 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001637 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001638 SmallVector<SDValue, 8> MemOpChains2;
1639 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001640 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001641 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001642 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001643 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1644 CCValAssign &VA = ArgLocs[i];
1645 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001646 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001647 SDValue Arg = TheCall->getArg(i);
1648 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001649 // Create frame index.
1650 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001651 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001652 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001653 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001654
Duncan Sandsc93fae32008-03-21 09:14:45 +00001655 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001656 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001657 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001658 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001659 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1660 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1661
1662 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001663 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001664 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001665 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001666 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001667 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001668 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001669 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001670 }
1671 }
1672
1673 if (!MemOpChains2.empty())
1674 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001675 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001676
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001677 // Copy arguments to their registers.
1678 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1679 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1680 InFlag);
1681 InFlag = Chain.getValue(1);
1682 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001683 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001684
Gordon Henriksen18ace102008-01-05 16:56:59 +00001685 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001686 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1687 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001688 }
1689
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001690 // If the callee is a GlobalAddress node (quite common, every direct call is)
1691 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1692 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1693 // We should use extra load for direct calls to dllimported functions in
1694 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001695 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1696 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001697 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bill Wendlingfef06052008-09-16 21:48:12 +00001698 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1699 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001700 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001701 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001702
1703 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001704 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001705 Callee,InFlag);
1706 Callee = DAG.getRegister(Opc, getPointerTy());
1707 // Add register as live out.
1708 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001709 }
1710
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001711 // Returns a chain & a flag for retval copy to use.
1712 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001713 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001714
1715 if (IsTailCall) {
1716 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001717 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1718 Ops.push_back(DAG.getIntPtrConstant(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001719 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001720 Ops.push_back(InFlag);
1721 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1722 InFlag = Chain.getValue(1);
1723
1724 // Returns a chain & a flag for retval copy to use.
1725 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1726 Ops.clear();
1727 }
1728
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001729 Ops.push_back(Chain);
1730 Ops.push_back(Callee);
1731
Gordon Henriksen18ace102008-01-05 16:56:59 +00001732 if (IsTailCall)
1733 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001734
Gordon Henriksen18ace102008-01-05 16:56:59 +00001735 // Add argument registers to the end of the list so that they are known live
1736 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001737 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1738 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1739 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001740
Evan Cheng8ba45e62008-03-18 23:36:35 +00001741 // Add an implicit use GOT pointer in EBX.
1742 if (!IsTailCall && !Is64Bit &&
1743 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1744 Subtarget->isPICStyleGOT())
1745 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1746
1747 // Add an implicit use of AL for x86 vararg functions.
1748 if (Is64Bit && isVarArg)
1749 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1750
Gabor Greif1c80d112008-08-28 21:40:38 +00001751 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001752 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001753
Gordon Henriksen18ace102008-01-05 16:56:59 +00001754 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001755 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001756 "Flag must be set. Depend on flag being set in LowerRET");
1757 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman705e3f72008-09-13 01:54:27 +00001758 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001759
Gabor Greif1c80d112008-08-28 21:40:38 +00001760 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001761 }
1762
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001763 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001764 InFlag = Chain.getValue(1);
1765
1766 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001767 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001768 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001769 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001770 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001771 // If this is is a call to a struct-return function, the callee
1772 // pops the hidden struct pointer, so we have to push it back.
1773 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001774 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001775 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001776 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001777
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001778 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001779 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001780 DAG.getIntPtrConstant(NumBytes),
1781 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001782 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001783 InFlag = Chain.getValue(1);
1784
1785 // Handle result values, copying them out of physregs into vregs that we
1786 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001787 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001788 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001789}
1790
1791
1792//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001793// Fast Calling Convention (tail call) implementation
1794//===----------------------------------------------------------------------===//
1795
1796// Like std call, callee cleans arguments, convention except that ECX is
1797// reserved for storing the tail called function address. Only 2 registers are
1798// free for argument passing (inreg). Tail call optimization is performed
1799// provided:
1800// * tailcallopt is enabled
1801// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001802// On X86_64 architecture with GOT-style position independent code only local
1803// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001804// To keep the stack aligned according to platform abi the function
1805// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1806// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001807// If a tail called function callee has more arguments than the caller the
1808// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001809// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001810// original REtADDR, but before the saved framepointer or the spilled registers
1811// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1812// stack layout:
1813// arg1
1814// arg2
1815// RETADDR
1816// [ new RETADDR
1817// move area ]
1818// (possible EBP)
1819// ESI
1820// EDI
1821// local1 ..
1822
1823/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1824/// for a 16 byte align requirement.
1825unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1826 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001827 MachineFunction &MF = DAG.getMachineFunction();
1828 const TargetMachine &TM = MF.getTarget();
1829 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1830 unsigned StackAlignment = TFI.getStackAlignment();
1831 uint64_t AlignMask = StackAlignment - 1;
1832 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001833 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001834 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1835 // Number smaller than 12 so just add the difference.
1836 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1837 } else {
1838 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1839 Offset = ((~AlignMask) & Offset) + StackAlignment +
1840 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001841 }
Evan Chengded8f902008-09-07 09:07:23 +00001842 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001843}
1844
1845/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001846/// following the call is a return. A function is eligible if caller/callee
1847/// calling conventions match, currently only fastcc supports tail calls, and
1848/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001849bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001850 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001851 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001852 if (!PerformTailCallOpt)
1853 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001854
Dan Gohman705e3f72008-09-13 01:54:27 +00001855 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001856 MachineFunction &MF = DAG.getMachineFunction();
1857 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001858 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001859 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001860 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001861 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001862 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001863 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001864 return true;
1865
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001866 // Can only do local tail calls (in same module, hidden or protected) on
1867 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001868 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1869 return G->getGlobal()->hasHiddenVisibility()
1870 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001871 }
1872 }
Evan Chenge7a87392007-11-02 01:26:22 +00001873
1874 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001875}
1876
Dan Gohmanca4857a2008-09-03 23:12:08 +00001877FastISel *
1878X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001879 MachineModuleInfo *mmo,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001880 DenseMap<const Value *, unsigned> &vm,
1881 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001882 MachineBasicBlock *> &bm,
1883 DenseMap<const AllocaInst *, int> &am) {
1884
Dan Gohman76dd96e2008-09-23 21:53:34 +00001885 return X86::createFastISel(mf, mmo, vm, bm, am);
Dan Gohman97805ee2008-08-19 21:32:53 +00001886}
1887
1888
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001889//===----------------------------------------------------------------------===//
1890// Other Lowering Hooks
1891//===----------------------------------------------------------------------===//
1892
1893
Dan Gohman8181bd12008-07-27 21:46:04 +00001894SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001895 MachineFunction &MF = DAG.getMachineFunction();
1896 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1897 int ReturnAddrIndex = FuncInfo->getRAIndex();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001898 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikove844e472007-08-15 17:12:32 +00001899
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001900 if (ReturnAddrIndex == 0) {
1901 // Set up a frame object for the return address.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001902 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001903 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001904 }
1905
1906 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1907}
1908
1909
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001910/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1911/// specific condition code. It returns a false if it cannot do a direct
1912/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1913/// needed.
1914static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001915 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001916 SelectionDAG &DAG) {
1917 X86CC = X86::COND_INVALID;
1918 if (!isFP) {
1919 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1920 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1921 // X > -1 -> X == 0, jump !sign.
1922 RHS = DAG.getConstant(0, RHS.getValueType());
1923 X86CC = X86::COND_NS;
1924 return true;
1925 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1926 // X < 0 -> X == 0, jump on sign.
1927 X86CC = X86::COND_S;
1928 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001929 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00001930 // X < 1 -> X <= 0
1931 RHS = DAG.getConstant(0, RHS.getValueType());
1932 X86CC = X86::COND_LE;
1933 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001934 }
1935 }
1936
1937 switch (SetCCOpcode) {
1938 default: break;
1939 case ISD::SETEQ: X86CC = X86::COND_E; break;
1940 case ISD::SETGT: X86CC = X86::COND_G; break;
1941 case ISD::SETGE: X86CC = X86::COND_GE; break;
1942 case ISD::SETLT: X86CC = X86::COND_L; break;
1943 case ISD::SETLE: X86CC = X86::COND_LE; break;
1944 case ISD::SETNE: X86CC = X86::COND_NE; break;
1945 case ISD::SETULT: X86CC = X86::COND_B; break;
1946 case ISD::SETUGT: X86CC = X86::COND_A; break;
1947 case ISD::SETULE: X86CC = X86::COND_BE; break;
1948 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1949 }
1950 } else {
Evan Chengb488ca32008-08-29 23:22:12 +00001951 // First determine if it requires or is profitable to flip the operands.
1952 bool Flip = false;
1953 switch (SetCCOpcode) {
1954 default: break;
1955 case ISD::SETOLT:
1956 case ISD::SETOLE:
1957 case ISD::SETUGT:
1958 case ISD::SETUGE:
1959 Flip = true;
1960 break;
1961 }
1962
1963 // If LHS is a foldable load, but RHS is not, flip the condition.
1964 if (!Flip &&
1965 (ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1966 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1967 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1968 Flip = true;
1969 }
1970 if (Flip)
1971 std::swap(LHS, RHS);
1972
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001973 // On a floating point condition, the flags are set as follows:
1974 // ZF PF CF op
1975 // 0 | 0 | 0 | X > Y
1976 // 0 | 0 | 1 | X < Y
1977 // 1 | 0 | 0 | X == Y
1978 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001979 switch (SetCCOpcode) {
1980 default: break;
1981 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00001982 case ISD::SETEQ:
1983 X86CC = X86::COND_E;
1984 break;
1985 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001986 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00001987 case ISD::SETGT:
1988 X86CC = X86::COND_A;
1989 break;
1990 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001991 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00001992 case ISD::SETGE:
1993 X86CC = X86::COND_AE;
1994 break;
1995 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001996 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00001997 case ISD::SETLT:
1998 X86CC = X86::COND_B;
1999 break;
2000 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002001 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00002002 case ISD::SETLE:
2003 X86CC = X86::COND_BE;
2004 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002005 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00002006 case ISD::SETNE:
2007 X86CC = X86::COND_NE;
2008 break;
2009 case ISD::SETUO:
2010 X86CC = X86::COND_P;
2011 break;
2012 case ISD::SETO:
2013 X86CC = X86::COND_NP;
2014 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002015 }
Evan Chengfc937c92008-08-28 23:48:31 +00002016 }
2017
Evan Chengc6162692008-08-29 22:13:21 +00002018 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002019}
2020
2021/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2022/// code. Current x86 isa includes the following FP cmov instructions:
2023/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2024static bool hasFPCMov(unsigned X86CC) {
2025 switch (X86CC) {
2026 default:
2027 return false;
2028 case X86::COND_B:
2029 case X86::COND_BE:
2030 case X86::COND_E:
2031 case X86::COND_P:
2032 case X86::COND_A:
2033 case X86::COND_AE:
2034 case X86::COND_NE:
2035 case X86::COND_NP:
2036 return true;
2037 }
2038}
2039
2040/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2041/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002042static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002043 if (Op.getOpcode() == ISD::UNDEF)
2044 return true;
2045
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002046 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002047 return (Val >= Low && Val < Hi);
2048}
2049
2050/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2051/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002052static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002053 if (Op.getOpcode() == ISD::UNDEF)
2054 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002055 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002056}
2057
2058/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2059/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2060bool X86::isPSHUFDMask(SDNode *N) {
2061 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2062
Dan Gohman7dc19012007-08-02 21:17:01 +00002063 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002064 return false;
2065
2066 // Check if the value doesn't reference the second vector.
2067 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002068 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002069 if (Arg.getOpcode() == ISD::UNDEF) continue;
2070 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002071 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002072 return false;
2073 }
2074
2075 return true;
2076}
2077
2078/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2079/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2080bool X86::isPSHUFHWMask(SDNode *N) {
2081 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2082
2083 if (N->getNumOperands() != 8)
2084 return false;
2085
2086 // Lower quadword copied in order.
2087 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002088 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002089 if (Arg.getOpcode() == ISD::UNDEF) continue;
2090 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002091 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002092 return false;
2093 }
2094
2095 // Upper quadword shuffled.
2096 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002097 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002098 if (Arg.getOpcode() == ISD::UNDEF) continue;
2099 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002100 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002101 if (Val < 4 || Val > 7)
2102 return false;
2103 }
2104
2105 return true;
2106}
2107
2108/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2109/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2110bool X86::isPSHUFLWMask(SDNode *N) {
2111 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2112
2113 if (N->getNumOperands() != 8)
2114 return false;
2115
2116 // Upper quadword copied in order.
2117 for (unsigned i = 4; i != 8; ++i)
2118 if (!isUndefOrEqual(N->getOperand(i), i))
2119 return false;
2120
2121 // Lower quadword shuffled.
2122 for (unsigned i = 0; i != 4; ++i)
2123 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2124 return false;
2125
2126 return true;
2127}
2128
2129/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2130/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002131static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002132 if (NumElems != 2 && NumElems != 4) return false;
2133
2134 unsigned Half = NumElems / 2;
2135 for (unsigned i = 0; i < Half; ++i)
2136 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2137 return false;
2138 for (unsigned i = Half; i < NumElems; ++i)
2139 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2140 return false;
2141
2142 return true;
2143}
2144
2145bool X86::isSHUFPMask(SDNode *N) {
2146 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2147 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2148}
2149
2150/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2151/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2152/// half elements to come from vector 1 (which would equal the dest.) and
2153/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002154static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002155 if (NumOps != 2 && NumOps != 4) return false;
2156
2157 unsigned Half = NumOps / 2;
2158 for (unsigned i = 0; i < Half; ++i)
2159 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2160 return false;
2161 for (unsigned i = Half; i < NumOps; ++i)
2162 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2163 return false;
2164 return true;
2165}
2166
2167static bool isCommutedSHUFP(SDNode *N) {
2168 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2169 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2170}
2171
2172/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2173/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2174bool X86::isMOVHLPSMask(SDNode *N) {
2175 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2176
2177 if (N->getNumOperands() != 4)
2178 return false;
2179
2180 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2181 return isUndefOrEqual(N->getOperand(0), 6) &&
2182 isUndefOrEqual(N->getOperand(1), 7) &&
2183 isUndefOrEqual(N->getOperand(2), 2) &&
2184 isUndefOrEqual(N->getOperand(3), 3);
2185}
2186
2187/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2188/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2189/// <2, 3, 2, 3>
2190bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2191 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2192
2193 if (N->getNumOperands() != 4)
2194 return false;
2195
2196 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2197 return isUndefOrEqual(N->getOperand(0), 2) &&
2198 isUndefOrEqual(N->getOperand(1), 3) &&
2199 isUndefOrEqual(N->getOperand(2), 2) &&
2200 isUndefOrEqual(N->getOperand(3), 3);
2201}
2202
2203/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2204/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2205bool X86::isMOVLPMask(SDNode *N) {
2206 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2207
2208 unsigned NumElems = N->getNumOperands();
2209 if (NumElems != 2 && NumElems != 4)
2210 return false;
2211
2212 for (unsigned i = 0; i < NumElems/2; ++i)
2213 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2214 return false;
2215
2216 for (unsigned i = NumElems/2; i < NumElems; ++i)
2217 if (!isUndefOrEqual(N->getOperand(i), i))
2218 return false;
2219
2220 return true;
2221}
2222
2223/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2224/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2225/// and MOVLHPS.
2226bool X86::isMOVHPMask(SDNode *N) {
2227 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2228
2229 unsigned NumElems = N->getNumOperands();
2230 if (NumElems != 2 && NumElems != 4)
2231 return false;
2232
2233 for (unsigned i = 0; i < NumElems/2; ++i)
2234 if (!isUndefOrEqual(N->getOperand(i), i))
2235 return false;
2236
2237 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002238 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002239 if (!isUndefOrEqual(Arg, i + NumElems))
2240 return false;
2241 }
2242
2243 return true;
2244}
2245
2246/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2247/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002248bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002249 bool V2IsSplat = false) {
2250 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2251 return false;
2252
2253 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002254 SDValue BitI = Elts[i];
2255 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002256 if (!isUndefOrEqual(BitI, j))
2257 return false;
2258 if (V2IsSplat) {
2259 if (isUndefOrEqual(BitI1, NumElts))
2260 return false;
2261 } else {
2262 if (!isUndefOrEqual(BitI1, j + NumElts))
2263 return false;
2264 }
2265 }
2266
2267 return true;
2268}
2269
2270bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2271 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2272 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2273}
2274
2275/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2276/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002277bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002278 bool V2IsSplat = false) {
2279 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2280 return false;
2281
2282 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002283 SDValue BitI = Elts[i];
2284 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002285 if (!isUndefOrEqual(BitI, j + NumElts/2))
2286 return false;
2287 if (V2IsSplat) {
2288 if (isUndefOrEqual(BitI1, NumElts))
2289 return false;
2290 } else {
2291 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2292 return false;
2293 }
2294 }
2295
2296 return true;
2297}
2298
2299bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2300 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2301 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2302}
2303
2304/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2305/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2306/// <0, 0, 1, 1>
2307bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2308 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2309
2310 unsigned NumElems = N->getNumOperands();
2311 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2312 return false;
2313
2314 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002315 SDValue BitI = N->getOperand(i);
2316 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002317
2318 if (!isUndefOrEqual(BitI, j))
2319 return false;
2320 if (!isUndefOrEqual(BitI1, j))
2321 return false;
2322 }
2323
2324 return true;
2325}
2326
2327/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2328/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2329/// <2, 2, 3, 3>
2330bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2331 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2332
2333 unsigned NumElems = N->getNumOperands();
2334 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2335 return false;
2336
2337 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002338 SDValue BitI = N->getOperand(i);
2339 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002340
2341 if (!isUndefOrEqual(BitI, j))
2342 return false;
2343 if (!isUndefOrEqual(BitI1, j))
2344 return false;
2345 }
2346
2347 return true;
2348}
2349
2350/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2351/// specifies a shuffle of elements that is suitable for input to MOVSS,
2352/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002353static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002354 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002355 return false;
2356
2357 if (!isUndefOrEqual(Elts[0], NumElts))
2358 return false;
2359
2360 for (unsigned i = 1; i < NumElts; ++i) {
2361 if (!isUndefOrEqual(Elts[i], i))
2362 return false;
2363 }
2364
2365 return true;
2366}
2367
2368bool X86::isMOVLMask(SDNode *N) {
2369 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2370 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2371}
2372
2373/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2374/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2375/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002376static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002377 bool V2IsSplat = false,
2378 bool V2IsUndef = false) {
2379 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2380 return false;
2381
2382 if (!isUndefOrEqual(Ops[0], 0))
2383 return false;
2384
2385 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002386 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002387 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2388 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2389 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2390 return false;
2391 }
2392
2393 return true;
2394}
2395
2396static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2397 bool V2IsUndef = false) {
2398 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2399 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2400 V2IsSplat, V2IsUndef);
2401}
2402
2403/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2404/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2405bool X86::isMOVSHDUPMask(SDNode *N) {
2406 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2407
2408 if (N->getNumOperands() != 4)
2409 return false;
2410
2411 // Expect 1, 1, 3, 3
2412 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002413 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002414 if (Arg.getOpcode() == ISD::UNDEF) continue;
2415 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002416 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002417 if (Val != 1) return false;
2418 }
2419
2420 bool HasHi = false;
2421 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002422 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002423 if (Arg.getOpcode() == ISD::UNDEF) continue;
2424 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002425 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002426 if (Val != 3) return false;
2427 HasHi = true;
2428 }
2429
2430 // Don't use movshdup if it can be done with a shufps.
2431 return HasHi;
2432}
2433
2434/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2435/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2436bool X86::isMOVSLDUPMask(SDNode *N) {
2437 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2438
2439 if (N->getNumOperands() != 4)
2440 return false;
2441
2442 // Expect 0, 0, 2, 2
2443 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002444 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002445 if (Arg.getOpcode() == ISD::UNDEF) continue;
2446 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002447 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002448 if (Val != 0) return false;
2449 }
2450
2451 bool HasHi = false;
2452 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002453 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002454 if (Arg.getOpcode() == ISD::UNDEF) continue;
2455 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002456 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002457 if (Val != 2) return false;
2458 HasHi = true;
2459 }
2460
2461 // Don't use movshdup if it can be done with a shufps.
2462 return HasHi;
2463}
2464
2465/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2466/// specifies a identity operation on the LHS or RHS.
2467static bool isIdentityMask(SDNode *N, bool RHS = false) {
2468 unsigned NumElems = N->getNumOperands();
2469 for (unsigned i = 0; i < NumElems; ++i)
2470 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2471 return false;
2472 return true;
2473}
2474
2475/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2476/// a splat of a single element.
2477static bool isSplatMask(SDNode *N) {
2478 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2479
2480 // This is a splat operation if each element of the permute is the same, and
2481 // if the value doesn't reference the second vector.
2482 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002483 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002484 unsigned i = 0;
2485 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002486 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002487 if (isa<ConstantSDNode>(Elt)) {
2488 ElementBase = Elt;
2489 break;
2490 }
2491 }
2492
Gabor Greif1c80d112008-08-28 21:40:38 +00002493 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002494 return false;
2495
2496 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002497 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002498 if (Arg.getOpcode() == ISD::UNDEF) continue;
2499 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2500 if (Arg != ElementBase) return false;
2501 }
2502
2503 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002504 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002505}
2506
2507/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2508/// a splat of a single element and it's a 2 or 4 element mask.
2509bool X86::isSplatMask(SDNode *N) {
2510 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2511
2512 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2513 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2514 return false;
2515 return ::isSplatMask(N);
2516}
2517
2518/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2519/// specifies a splat of zero element.
2520bool X86::isSplatLoMask(SDNode *N) {
2521 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2522
2523 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2524 if (!isUndefOrEqual(N->getOperand(i), 0))
2525 return false;
2526 return true;
2527}
2528
Evan Chenga2497eb2008-09-25 20:50:48 +00002529/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2530/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2531bool X86::isMOVDDUPMask(SDNode *N) {
2532 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2533
2534 unsigned e = N->getNumOperands() / 2;
2535 for (unsigned i = 0; i < e; ++i)
2536 if (!isUndefOrEqual(N->getOperand(i), i))
2537 return false;
2538 for (unsigned i = 0; i < e; ++i)
2539 if (!isUndefOrEqual(N->getOperand(e+i), i))
2540 return false;
2541 return true;
2542}
2543
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002544/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2545/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2546/// instructions.
2547unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2548 unsigned NumOperands = N->getNumOperands();
2549 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2550 unsigned Mask = 0;
2551 for (unsigned i = 0; i < NumOperands; ++i) {
2552 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002553 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002554 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002555 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002556 if (Val >= NumOperands) Val -= NumOperands;
2557 Mask |= Val;
2558 if (i != NumOperands - 1)
2559 Mask <<= Shift;
2560 }
2561
2562 return Mask;
2563}
2564
2565/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2566/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2567/// instructions.
2568unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2569 unsigned Mask = 0;
2570 // 8 nodes, but we only care about the last 4.
2571 for (unsigned i = 7; i >= 4; --i) {
2572 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002573 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002574 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002575 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002576 Mask |= (Val - 4);
2577 if (i != 4)
2578 Mask <<= 2;
2579 }
2580
2581 return Mask;
2582}
2583
2584/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2585/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2586/// instructions.
2587unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2588 unsigned Mask = 0;
2589 // 8 nodes, but we only care about the first 4.
2590 for (int i = 3; i >= 0; --i) {
2591 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002592 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002593 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002594 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002595 Mask |= Val;
2596 if (i != 0)
2597 Mask <<= 2;
2598 }
2599
2600 return Mask;
2601}
2602
2603/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2604/// specifies a 8 element shuffle that can be broken into a pair of
2605/// PSHUFHW and PSHUFLW.
2606static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2607 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2608
2609 if (N->getNumOperands() != 8)
2610 return false;
2611
2612 // Lower quadword shuffled.
2613 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002614 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002615 if (Arg.getOpcode() == ISD::UNDEF) continue;
2616 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002617 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002618 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002619 return false;
2620 }
2621
2622 // Upper quadword shuffled.
2623 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002624 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002625 if (Arg.getOpcode() == ISD::UNDEF) continue;
2626 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002627 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002628 if (Val < 4 || Val > 7)
2629 return false;
2630 }
2631
2632 return true;
2633}
2634
Chris Lattnere6aa3862007-11-25 00:24:49 +00002635/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002636/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002637static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2638 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002639 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002640 MVT VT = Op.getValueType();
2641 MVT MaskVT = Mask.getValueType();
2642 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002643 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002644 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002645
2646 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002647 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002648 if (Arg.getOpcode() == ISD::UNDEF) {
2649 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2650 continue;
2651 }
2652 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002653 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002654 if (Val < NumElems)
2655 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2656 else
2657 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2658 }
2659
2660 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002661 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002662 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2663}
2664
Evan Chenga6769df2007-12-07 21:30:01 +00002665/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2666/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002667static
Dan Gohman8181bd12008-07-27 21:46:04 +00002668SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002669 MVT MaskVT = Mask.getValueType();
2670 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002671 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002672 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002673 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002674 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002675 if (Arg.getOpcode() == ISD::UNDEF) {
2676 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2677 continue;
2678 }
2679 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002680 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002681 if (Val < NumElems)
2682 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2683 else
2684 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2685 }
2686 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2687}
2688
2689
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002690/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2691/// match movhlps. The lower half elements should come from upper half of
2692/// V1 (and in order), and the upper half elements should come from the upper
2693/// half of V2 (and in order).
2694static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2695 unsigned NumElems = Mask->getNumOperands();
2696 if (NumElems != 4)
2697 return false;
2698 for (unsigned i = 0, e = 2; i != e; ++i)
2699 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2700 return false;
2701 for (unsigned i = 2; i != 4; ++i)
2702 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2703 return false;
2704 return true;
2705}
2706
2707/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002708/// is promoted to a vector. It also returns the LoadSDNode by reference if
2709/// required.
2710static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002711 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2712 return false;
2713 N = N->getOperand(0).getNode();
2714 if (!ISD::isNON_EXTLoad(N))
2715 return false;
2716 if (LD)
2717 *LD = cast<LoadSDNode>(N);
2718 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002719}
2720
2721/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2722/// match movlp{s|d}. The lower half elements should come from lower half of
2723/// V1 (and in order), and the upper half elements should come from the upper
2724/// half of V2 (and in order). And since V1 will become the source of the
2725/// MOVLP, it must be either a vector load or a scalar load to vector.
2726static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2727 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2728 return false;
2729 // Is V2 is a vector load, don't do this transformation. We will try to use
2730 // load folding shufps op.
2731 if (ISD::isNON_EXTLoad(V2))
2732 return false;
2733
2734 unsigned NumElems = Mask->getNumOperands();
2735 if (NumElems != 2 && NumElems != 4)
2736 return false;
2737 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2738 if (!isUndefOrEqual(Mask->getOperand(i), i))
2739 return false;
2740 for (unsigned i = NumElems/2; i != NumElems; ++i)
2741 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2742 return false;
2743 return true;
2744}
2745
2746/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2747/// all the same.
2748static bool isSplatVector(SDNode *N) {
2749 if (N->getOpcode() != ISD::BUILD_VECTOR)
2750 return false;
2751
Dan Gohman8181bd12008-07-27 21:46:04 +00002752 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002753 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2754 if (N->getOperand(i) != SplatValue)
2755 return false;
2756 return true;
2757}
2758
2759/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2760/// to an undef.
2761static bool isUndefShuffle(SDNode *N) {
2762 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2763 return false;
2764
Dan Gohman8181bd12008-07-27 21:46:04 +00002765 SDValue V1 = N->getOperand(0);
2766 SDValue V2 = N->getOperand(1);
2767 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002768 unsigned NumElems = Mask.getNumOperands();
2769 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002770 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002771 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002772 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002773 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2774 return false;
2775 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2776 return false;
2777 }
2778 }
2779 return true;
2780}
2781
2782/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2783/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002784static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002785 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002786 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002787 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002788 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002789}
2790
2791/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2792/// to an zero vector.
2793static bool isZeroShuffle(SDNode *N) {
2794 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2795 return false;
2796
Dan Gohman8181bd12008-07-27 21:46:04 +00002797 SDValue V1 = N->getOperand(0);
2798 SDValue V2 = N->getOperand(1);
2799 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002800 unsigned NumElems = Mask.getNumOperands();
2801 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002802 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002803 if (Arg.getOpcode() == ISD::UNDEF)
2804 continue;
2805
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002806 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002807 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002808 unsigned Opc = V1.getNode()->getOpcode();
2809 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002810 continue;
2811 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002812 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002813 return false;
2814 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002815 unsigned Opc = V2.getNode()->getOpcode();
2816 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002817 continue;
2818 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002819 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002820 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002821 }
2822 }
2823 return true;
2824}
2825
2826/// getZeroVector - Returns a vector of specified type with all zero elements.
2827///
Dan Gohman8181bd12008-07-27 21:46:04 +00002828static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002829 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002830
2831 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2832 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002833 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002834 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002835 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002836 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002837 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002838 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002839 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002840 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002841 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002842 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2843 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002844 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002845}
2846
Chris Lattnere6aa3862007-11-25 00:24:49 +00002847/// getOnesVector - Returns a vector of specified type with all bits set.
2848///
Dan Gohman8181bd12008-07-27 21:46:04 +00002849static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002850 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002851
2852 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2853 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002854 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2855 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002856 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002857 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2858 else // SSE
2859 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2860 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2861}
2862
2863
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002864/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2865/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002866static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002867 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2868
2869 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002870 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002871 unsigned NumElems = Mask.getNumOperands();
2872 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002873 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002874 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002875 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002876 if (Val > NumElems) {
2877 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2878 Changed = true;
2879 }
2880 }
2881 MaskVec.push_back(Arg);
2882 }
2883
2884 if (Changed)
2885 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2886 &MaskVec[0], MaskVec.size());
2887 return Mask;
2888}
2889
2890/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2891/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002892static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002893 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2894 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002895
Dan Gohman8181bd12008-07-27 21:46:04 +00002896 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002897 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2898 for (unsigned i = 1; i != NumElems; ++i)
2899 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2900 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2901}
2902
2903/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2904/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002905static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002906 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2907 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002908 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002909 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2910 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2911 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2912 }
2913 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2914}
2915
2916/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2917/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002918static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002919 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2920 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002921 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002922 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002923 for (unsigned i = 0; i != Half; ++i) {
2924 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2925 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2926 }
2927 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2928}
2929
Chris Lattner2d91b962008-03-09 01:05:04 +00002930/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2931/// element #0 of a vector with the specified index, leaving the rest of the
2932/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002933static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002934 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002935 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2936 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002937 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002938 // Element #0 of the result gets the elt we are replacing.
2939 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2940 for (unsigned i = 1; i != NumElems; ++i)
2941 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2942 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2943}
2944
Evan Chengbf8b2c52008-04-05 00:30:36 +00002945/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002946static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002947 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2948 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002949 if (PVT == VT)
2950 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002951 SDValue V1 = Op.getOperand(0);
2952 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002953 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002954 // Special handling of v4f32 -> v4i32.
2955 if (VT != MVT::v4f32) {
2956 Mask = getUnpacklMask(NumElems, DAG);
2957 while (NumElems > 4) {
2958 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2959 NumElems >>= 1;
2960 }
Evan Cheng8c590372008-05-15 08:39:06 +00002961 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002962 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002963
Evan Chengbf8b2c52008-04-05 00:30:36 +00002964 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002965 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002966 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002967 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2968}
2969
Evan Chenga2497eb2008-09-25 20:50:48 +00002970/// isVectorLoad - Returns true if the node is a vector load, a scalar
2971/// load that's promoted to vector, or a load bitcasted.
2972static bool isVectorLoad(SDValue Op) {
2973 assert(Op.getValueType().isVector() && "Expected a vector type");
2974 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
2975 Op.getOpcode() == ISD::BIT_CONVERT) {
2976 return isa<LoadSDNode>(Op.getOperand(0));
2977 }
2978 return isa<LoadSDNode>(Op);
2979}
2980
2981
2982/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
2983///
2984static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
2985 SelectionDAG &DAG, bool HasSSE3) {
2986 // If we have sse3 and shuffle has more than one use or input is a load, then
2987 // use movddup. Otherwise, use movlhps.
2988 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
2989 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
2990 MVT VT = Op.getValueType();
2991 if (VT == PVT)
2992 return Op;
2993 unsigned NumElems = PVT.getVectorNumElements();
2994 if (NumElems == 2) {
2995 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
2996 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2997 } else {
2998 assert(NumElems == 4);
2999 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3000 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
3001 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst0, Cst1, Cst0, Cst1);
3002 }
3003
3004 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
3005 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
3006 DAG.getNode(ISD::UNDEF, PVT), Mask);
3007 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3008}
3009
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003010/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003011/// vector of zero or undef vector. This produces a shuffle where the low
3012/// element of V2 is swizzled into the zero/undef vector, landing at element
3013/// 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 +00003014static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003015 bool isZero, bool HasSSE2,
3016 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003017 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003018 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00003019 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003020 unsigned NumElems = V2.getValueType().getVectorNumElements();
3021 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3022 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003023 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003024 for (unsigned i = 0; i != NumElems; ++i)
3025 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3026 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3027 else
3028 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003029 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003030 &MaskVec[0], MaskVec.size());
3031 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3032}
3033
Evan Chengdea99362008-05-29 08:22:04 +00003034/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3035/// a shuffle that is zero.
3036static
Dan Gohman8181bd12008-07-27 21:46:04 +00003037unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003038 unsigned NumElems, bool Low,
3039 SelectionDAG &DAG) {
3040 unsigned NumZeros = 0;
3041 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003042 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003043 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003044 if (Idx.getOpcode() == ISD::UNDEF) {
3045 ++NumZeros;
3046 continue;
3047 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003048 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3049 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003050 ++NumZeros;
3051 else
3052 break;
3053 }
3054 return NumZeros;
3055}
3056
3057/// isVectorShift - Returns true if the shuffle can be implemented as a
3058/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003059static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3060 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003061 unsigned NumElems = Mask.getNumOperands();
3062
3063 isLeft = true;
3064 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3065 if (!NumZeros) {
3066 isLeft = false;
3067 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3068 if (!NumZeros)
3069 return false;
3070 }
3071
3072 bool SeenV1 = false;
3073 bool SeenV2 = false;
3074 for (unsigned i = NumZeros; i < NumElems; ++i) {
3075 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003076 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003077 if (Idx.getOpcode() == ISD::UNDEF)
3078 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003079 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003080 if (Index < NumElems)
3081 SeenV1 = true;
3082 else {
3083 Index -= NumElems;
3084 SeenV2 = true;
3085 }
3086 if (Index != Val)
3087 return false;
3088 }
3089 if (SeenV1 && SeenV2)
3090 return false;
3091
3092 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3093 ShAmt = NumZeros;
3094 return true;
3095}
3096
3097
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003098/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3099///
Dan Gohman8181bd12008-07-27 21:46:04 +00003100static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003101 unsigned NumNonZero, unsigned NumZero,
3102 SelectionDAG &DAG, TargetLowering &TLI) {
3103 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003104 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003105
Dan Gohman8181bd12008-07-27 21:46:04 +00003106 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003107 bool First = true;
3108 for (unsigned i = 0; i < 16; ++i) {
3109 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3110 if (ThisIsNonZero && First) {
3111 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003112 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003113 else
3114 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3115 First = false;
3116 }
3117
3118 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003119 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003120 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3121 if (LastIsNonZero) {
3122 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3123 }
3124 if (ThisIsNonZero) {
3125 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3126 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3127 ThisElt, DAG.getConstant(8, MVT::i8));
3128 if (LastIsNonZero)
3129 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3130 } else
3131 ThisElt = LastElt;
3132
Gabor Greif1c80d112008-08-28 21:40:38 +00003133 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003134 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003135 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003136 }
3137 }
3138
3139 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3140}
3141
3142/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3143///
Dan Gohman8181bd12008-07-27 21:46:04 +00003144static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003145 unsigned NumNonZero, unsigned NumZero,
3146 SelectionDAG &DAG, TargetLowering &TLI) {
3147 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003148 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003149
Dan Gohman8181bd12008-07-27 21:46:04 +00003150 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003151 bool First = true;
3152 for (unsigned i = 0; i < 8; ++i) {
3153 bool isNonZero = (NonZeros & (1 << i)) != 0;
3154 if (isNonZero) {
3155 if (First) {
3156 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003157 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003158 else
3159 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3160 First = false;
3161 }
3162 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003163 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003164 }
3165 }
3166
3167 return V;
3168}
3169
Evan Chengdea99362008-05-29 08:22:04 +00003170/// getVShift - Return a vector logical shift node.
3171///
Dan Gohman8181bd12008-07-27 21:46:04 +00003172static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003173 unsigned NumBits, SelectionDAG &DAG,
3174 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003175 bool isMMX = VT.getSizeInBits() == 64;
3176 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003177 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3178 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3179 return DAG.getNode(ISD::BIT_CONVERT, VT,
3180 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003181 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003182}
3183
Dan Gohman8181bd12008-07-27 21:46:04 +00003184SDValue
3185X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003186 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003187 if (ISD::isBuildVectorAllZeros(Op.getNode())
3188 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003189 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3190 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3191 // eliminated on x86-32 hosts.
3192 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3193 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003194
Gabor Greif1c80d112008-08-28 21:40:38 +00003195 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003196 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003197 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003198 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003199
Duncan Sands92c43912008-06-06 12:08:01 +00003200 MVT VT = Op.getValueType();
3201 MVT EVT = VT.getVectorElementType();
3202 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003203
3204 unsigned NumElems = Op.getNumOperands();
3205 unsigned NumZero = 0;
3206 unsigned NumNonZero = 0;
3207 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003208 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003209 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003210 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003211 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003212 if (Elt.getOpcode() == ISD::UNDEF)
3213 continue;
3214 Values.insert(Elt);
3215 if (Elt.getOpcode() != ISD::Constant &&
3216 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003217 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003218 if (isZeroNode(Elt))
3219 NumZero++;
3220 else {
3221 NonZeros |= (1 << i);
3222 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003223 }
3224 }
3225
3226 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003227 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3228 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003229 }
3230
Chris Lattner66a4dda2008-03-09 05:42:06 +00003231 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003232 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003233 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003234 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003235
Chris Lattner2d91b962008-03-09 01:05:04 +00003236 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3237 // the value are obviously zero, truncate the value to i32 and do the
3238 // insertion that way. Only do this if the value is non-constant or if the
3239 // value is a constant being inserted into element 0. It is cheaper to do
3240 // a constant pool load than it is to do a movd + shuffle.
3241 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3242 (!IsAllConstants || Idx == 0)) {
3243 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3244 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003245 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3246 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003247
3248 // Truncate the value (which may itself be a constant) to i32, and
3249 // convert it to a vector with movd (S2V+shuffle to zero extend).
3250 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3251 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003252 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3253 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003254
3255 // Now we have our 32-bit value zero extended in the low element of
3256 // a vector. If Idx != 0, swizzle it into place.
3257 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003258 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003259 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3260 getSwapEltZeroMask(VecElts, Idx, DAG)
3261 };
3262 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3263 }
3264 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3265 }
3266 }
3267
Chris Lattnerac914892008-03-08 22:59:52 +00003268 // If we have a constant or non-constant insertion into the low element of
3269 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3270 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3271 // depending on what the source datatype is. Because we can only get here
3272 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3273 if (Idx == 0 &&
3274 // Don't do this for i64 values on x86-32.
3275 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003276 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003277 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003278 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3279 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003280 }
Evan Chengdea99362008-05-29 08:22:04 +00003281
3282 // Is it a vector logical left shift?
3283 if (NumElems == 2 && Idx == 1 &&
3284 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003285 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003286 return getVShift(true, VT,
3287 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3288 NumBits/2, DAG, *this);
3289 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003290
3291 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003292 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003293
Chris Lattnerac914892008-03-08 22:59:52 +00003294 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3295 // is a non-constant being inserted into an element other than the low one,
3296 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3297 // movd/movss) to move this into the low element, then shuffle it into
3298 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003299 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003300 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3301
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003302 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003303 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3304 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003305 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3306 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003307 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003308 for (unsigned i = 0; i < NumElems; i++)
3309 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003310 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003311 &MaskVec[0], MaskVec.size());
3312 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3313 DAG.getNode(ISD::UNDEF, VT), Mask);
3314 }
3315 }
3316
Chris Lattner66a4dda2008-03-09 05:42:06 +00003317 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3318 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003319 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003320
Dan Gohman21463242007-07-24 22:55:08 +00003321 // A vector full of immediates; various special cases are already
3322 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003323 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003324 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003325
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003326 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003327 if (EVTBits == 64) {
3328 if (NumNonZero == 1) {
3329 // One half is zero or undef.
3330 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003331 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003332 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003333 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3334 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003335 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003336 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003337 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003338
3339 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3340 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003341 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003342 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003343 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003344 }
3345
3346 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003347 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003348 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003349 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003350 }
3351
3352 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003353 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003354 V.resize(NumElems);
3355 if (NumElems == 4 && NumZero > 0) {
3356 for (unsigned i = 0; i < 4; ++i) {
3357 bool isZero = !(NonZeros & (1 << i));
3358 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003359 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003360 else
3361 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3362 }
3363
3364 for (unsigned i = 0; i < 2; ++i) {
3365 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3366 default: break;
3367 case 0:
3368 V[i] = V[i*2]; // Must be a zero vector.
3369 break;
3370 case 1:
3371 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3372 getMOVLMask(NumElems, DAG));
3373 break;
3374 case 2:
3375 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3376 getMOVLMask(NumElems, DAG));
3377 break;
3378 case 3:
3379 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3380 getUnpacklMask(NumElems, DAG));
3381 break;
3382 }
3383 }
3384
Duncan Sands92c43912008-06-06 12:08:01 +00003385 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3386 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003387 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003388 bool Reverse = (NonZeros & 0x3) == 2;
3389 for (unsigned i = 0; i < 2; ++i)
3390 if (Reverse)
3391 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3392 else
3393 MaskVec.push_back(DAG.getConstant(i, EVT));
3394 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3395 for (unsigned i = 0; i < 2; ++i)
3396 if (Reverse)
3397 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3398 else
3399 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003400 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003401 &MaskVec[0], MaskVec.size());
3402 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3403 }
3404
3405 if (Values.size() > 2) {
3406 // Expand into a number of unpckl*.
3407 // e.g. for v4f32
3408 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3409 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3410 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003411 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003412 for (unsigned i = 0; i < NumElems; ++i)
3413 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3414 NumElems >>= 1;
3415 while (NumElems != 0) {
3416 for (unsigned i = 0; i < NumElems; ++i)
3417 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3418 UnpckMask);
3419 NumElems >>= 1;
3420 }
3421 return V[0];
3422 }
3423
Dan Gohman8181bd12008-07-27 21:46:04 +00003424 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003425}
3426
Evan Chengfca29242007-12-07 08:07:39 +00003427static
Dan Gohman8181bd12008-07-27 21:46:04 +00003428SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003429 SDValue PermMask, SelectionDAG &DAG,
3430 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003431 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003432 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3433 MVT MaskEVT = MaskVT.getVectorElementType();
3434 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003435 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3436 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003437
3438 // First record which half of which vector the low elements come from.
3439 SmallVector<unsigned, 4> LowQuad(4);
3440 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003441 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003442 if (Elt.getOpcode() == ISD::UNDEF)
3443 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003444 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003445 int QuadIdx = EltIdx / 4;
3446 ++LowQuad[QuadIdx];
3447 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003448
Evan Cheng75184a92007-12-11 01:46:18 +00003449 int BestLowQuad = -1;
3450 unsigned MaxQuad = 1;
3451 for (unsigned i = 0; i < 4; ++i) {
3452 if (LowQuad[i] > MaxQuad) {
3453 BestLowQuad = i;
3454 MaxQuad = LowQuad[i];
3455 }
Evan Chengfca29242007-12-07 08:07:39 +00003456 }
3457
Evan Cheng75184a92007-12-11 01:46:18 +00003458 // Record which half of which vector the high elements come from.
3459 SmallVector<unsigned, 4> HighQuad(4);
3460 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003461 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003462 if (Elt.getOpcode() == ISD::UNDEF)
3463 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003464 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003465 int QuadIdx = EltIdx / 4;
3466 ++HighQuad[QuadIdx];
3467 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003468
Evan Cheng75184a92007-12-11 01:46:18 +00003469 int BestHighQuad = -1;
3470 MaxQuad = 1;
3471 for (unsigned i = 0; i < 4; ++i) {
3472 if (HighQuad[i] > MaxQuad) {
3473 BestHighQuad = i;
3474 MaxQuad = HighQuad[i];
3475 }
3476 }
3477
3478 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3479 if (BestLowQuad != -1 || BestHighQuad != -1) {
3480 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003481 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003482
Evan Cheng75184a92007-12-11 01:46:18 +00003483 if (BestLowQuad != -1)
3484 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3485 else
3486 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003487
Evan Cheng75184a92007-12-11 01:46:18 +00003488 if (BestHighQuad != -1)
3489 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3490 else
3491 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003492
Dan Gohman8181bd12008-07-27 21:46:04 +00003493 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003494 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3495 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3496 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3497 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3498
3499 // Now sort high and low parts separately.
3500 BitVector InOrder(8);
3501 if (BestLowQuad != -1) {
3502 // Sort lower half in order using PSHUFLW.
3503 MaskVec.clear();
3504 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003505
Evan Cheng75184a92007-12-11 01:46:18 +00003506 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003507 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003508 if (Elt.getOpcode() == ISD::UNDEF) {
3509 MaskVec.push_back(Elt);
3510 InOrder.set(i);
3511 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003512 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003513 if (EltIdx != i)
3514 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003515
Evan Cheng75184a92007-12-11 01:46:18 +00003516 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003517
Evan Cheng75184a92007-12-11 01:46:18 +00003518 // If this element is in the right place after this shuffle, then
3519 // remember it.
3520 if ((int)(EltIdx / 4) == BestLowQuad)
3521 InOrder.set(i);
3522 }
3523 }
3524 if (AnyOutOrder) {
3525 for (unsigned i = 4; i != 8; ++i)
3526 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003527 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003528 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3529 }
3530 }
3531
3532 if (BestHighQuad != -1) {
3533 // Sort high half in order using PSHUFHW if possible.
3534 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003535
Evan Cheng75184a92007-12-11 01:46:18 +00003536 for (unsigned i = 0; i != 4; ++i)
3537 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003538
Evan Cheng75184a92007-12-11 01:46:18 +00003539 bool AnyOutOrder = false;
3540 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003541 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003542 if (Elt.getOpcode() == ISD::UNDEF) {
3543 MaskVec.push_back(Elt);
3544 InOrder.set(i);
3545 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003546 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003547 if (EltIdx != i)
3548 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003549
Evan Cheng75184a92007-12-11 01:46:18 +00003550 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003551
Evan Cheng75184a92007-12-11 01:46:18 +00003552 // If this element is in the right place after this shuffle, then
3553 // remember it.
3554 if ((int)(EltIdx / 4) == BestHighQuad)
3555 InOrder.set(i);
3556 }
3557 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003558
Evan Cheng75184a92007-12-11 01:46:18 +00003559 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003560 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003561 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3562 }
3563 }
3564
3565 // The other elements are put in the right place using pextrw and pinsrw.
3566 for (unsigned i = 0; i != 8; ++i) {
3567 if (InOrder[i])
3568 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003569 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003570 if (Elt.getOpcode() == ISD::UNDEF)
3571 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003572 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003573 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003574 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3575 DAG.getConstant(EltIdx, PtrVT))
3576 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3577 DAG.getConstant(EltIdx - 8, PtrVT));
3578 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3579 DAG.getConstant(i, PtrVT));
3580 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003581
Evan Cheng75184a92007-12-11 01:46:18 +00003582 return NewV;
3583 }
3584
Bill Wendling2c7cd592008-08-21 22:35:37 +00003585 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3586 // few as possible. First, let's find out how many elements are already in the
3587 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003588 unsigned V1InOrder = 0;
3589 unsigned V1FromV1 = 0;
3590 unsigned V2InOrder = 0;
3591 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003592 SmallVector<SDValue, 8> V1Elts;
3593 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003594 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003595 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003596 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003597 V1Elts.push_back(Elt);
3598 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003599 ++V1InOrder;
3600 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003601 continue;
3602 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003603 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003604 if (EltIdx == i) {
3605 V1Elts.push_back(Elt);
3606 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3607 ++V1InOrder;
3608 } else if (EltIdx == i+8) {
3609 V1Elts.push_back(Elt);
3610 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3611 ++V2InOrder;
3612 } else if (EltIdx < 8) {
3613 V1Elts.push_back(Elt);
3614 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003615 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003616 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3617 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003618 }
3619 }
3620
3621 if (V2InOrder > V1InOrder) {
3622 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3623 std::swap(V1, V2);
3624 std::swap(V1Elts, V2Elts);
3625 std::swap(V1FromV1, V2FromV2);
3626 }
3627
Evan Cheng75184a92007-12-11 01:46:18 +00003628 if ((V1FromV1 + V1InOrder) != 8) {
3629 // Some elements are from V2.
3630 if (V1FromV1) {
3631 // If there are elements that are from V1 but out of place,
3632 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003633 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003634 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003635 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003636 if (Elt.getOpcode() == ISD::UNDEF) {
3637 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3638 continue;
3639 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003640 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003641 if (EltIdx >= 8)
3642 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3643 else
3644 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3645 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003646 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003647 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003648 }
Evan Cheng75184a92007-12-11 01:46:18 +00003649
3650 NewV = V1;
3651 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003652 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003653 if (Elt.getOpcode() == ISD::UNDEF)
3654 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003655 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003656 if (EltIdx < 8)
3657 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003658 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003659 DAG.getConstant(EltIdx - 8, PtrVT));
3660 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3661 DAG.getConstant(i, PtrVT));
3662 }
3663 return NewV;
3664 } else {
3665 // All elements are from V1.
3666 NewV = V1;
3667 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003668 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003669 if (Elt.getOpcode() == ISD::UNDEF)
3670 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003671 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003672 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003673 DAG.getConstant(EltIdx, PtrVT));
3674 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3675 DAG.getConstant(i, PtrVT));
3676 }
3677 return NewV;
3678 }
3679}
3680
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003681/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3682/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3683/// done when every pair / quad of shuffle mask elements point to elements in
3684/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003685/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3686static
Dan Gohman8181bd12008-07-27 21:46:04 +00003687SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003688 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003689 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003690 TargetLowering &TLI) {
3691 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003692 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003693 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003694 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003695 MVT NewVT = MaskVT;
3696 switch (VT.getSimpleVT()) {
3697 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003698 case MVT::v4f32: NewVT = MVT::v2f64; break;
3699 case MVT::v4i32: NewVT = MVT::v2i64; break;
3700 case MVT::v8i16: NewVT = MVT::v4i32; break;
3701 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003702 }
3703
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003704 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003705 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003706 NewVT = MVT::v2i64;
3707 else
3708 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003709 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003710 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003711 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003712 for (unsigned i = 0; i < NumElems; i += Scale) {
3713 unsigned StartIdx = ~0U;
3714 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003715 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003716 if (Elt.getOpcode() == ISD::UNDEF)
3717 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003718 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003719 if (StartIdx == ~0U)
3720 StartIdx = EltIdx - (EltIdx % Scale);
3721 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003722 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003723 }
3724 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003725 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003726 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003727 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003728 }
3729
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003730 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3731 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3732 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3733 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3734 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003735}
3736
Evan Chenge9b9c672008-05-09 21:53:03 +00003737/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003738///
Dan Gohman8181bd12008-07-27 21:46:04 +00003739static SDValue getVZextMovL(MVT VT, MVT OpVT,
3740 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003741 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003742 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3743 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003744 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003745 LD = dyn_cast<LoadSDNode>(SrcOp);
3746 if (!LD) {
3747 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3748 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003749 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003750 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3751 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3752 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3753 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3754 // PR2108
3755 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3756 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003757 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003758 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003759 SrcOp.getOperand(0)
3760 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003761 }
3762 }
3763 }
3764
3765 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003766 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003767 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3768}
3769
Evan Chengf50554e2008-07-22 21:13:36 +00003770/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3771/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003772static SDValue
3773LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3774 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003775 MVT MaskVT = PermMask.getValueType();
3776 MVT MaskEVT = MaskVT.getVectorElementType();
3777 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003778 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003779 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003780 unsigned NumHi = 0;
3781 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003782 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003783 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003784 if (Elt.getOpcode() == ISD::UNDEF) {
3785 Locs[i] = std::make_pair(-1, -1);
3786 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003787 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003788 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003789 if (Val < 4) {
3790 Locs[i] = std::make_pair(0, NumLo);
3791 Mask1[NumLo] = Elt;
3792 NumLo++;
3793 } else {
3794 Locs[i] = std::make_pair(1, NumHi);
3795 if (2+NumHi < 4)
3796 Mask1[2+NumHi] = Elt;
3797 NumHi++;
3798 }
3799 }
3800 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003801
Evan Chengf50554e2008-07-22 21:13:36 +00003802 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003803 // If no more than two elements come from either vector. This can be
3804 // implemented with two shuffles. First shuffle gather the elements.
3805 // The second shuffle, which takes the first shuffle as both of its
3806 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003807 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3808 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3809 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003810
Dan Gohman8181bd12008-07-27 21:46:04 +00003811 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003812 for (unsigned i = 0; i != 4; ++i) {
3813 if (Locs[i].first == -1)
3814 continue;
3815 else {
3816 unsigned Idx = (i < 2) ? 0 : 4;
3817 Idx += Locs[i].first * 2 + Locs[i].second;
3818 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3819 }
3820 }
3821
3822 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3823 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3824 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003825 } else if (NumLo == 3 || NumHi == 3) {
3826 // Otherwise, we must have three elements from one vector, call it X, and
3827 // one element from the other, call it Y. First, use a shufps to build an
3828 // intermediate vector with the one element from Y and the element from X
3829 // that will be in the same half in the final destination (the indexes don't
3830 // matter). Then, use a shufps to build the final vector, taking the half
3831 // containing the element from Y from the intermediate, and the other half
3832 // from X.
3833 if (NumHi == 3) {
3834 // Normalize it so the 3 elements come from V1.
3835 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3836 std::swap(V1, V2);
3837 }
3838
3839 // Find the element from V2.
3840 unsigned HiIndex;
3841 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003842 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003843 if (Elt.getOpcode() == ISD::UNDEF)
3844 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003845 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003846 if (Val >= 4)
3847 break;
3848 }
3849
3850 Mask1[0] = PermMask.getOperand(HiIndex);
3851 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3852 Mask1[2] = PermMask.getOperand(HiIndex^1);
3853 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3854 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3855 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3856
3857 if (HiIndex >= 2) {
3858 Mask1[0] = PermMask.getOperand(0);
3859 Mask1[1] = PermMask.getOperand(1);
3860 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3861 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3862 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3863 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3864 } else {
3865 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3866 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3867 Mask1[2] = PermMask.getOperand(2);
3868 Mask1[3] = PermMask.getOperand(3);
3869 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003870 Mask1[2] =
3871 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3872 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003873 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003874 Mask1[3] =
3875 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3876 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003877 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3878 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3879 }
Evan Chengf50554e2008-07-22 21:13:36 +00003880 }
3881
3882 // Break it into (shuffle shuffle_hi, shuffle_lo).
3883 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003884 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3885 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3886 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003887 unsigned MaskIdx = 0;
3888 unsigned LoIdx = 0;
3889 unsigned HiIdx = 2;
3890 for (unsigned i = 0; i != 4; ++i) {
3891 if (i == 2) {
3892 MaskPtr = &HiMask;
3893 MaskIdx = 1;
3894 LoIdx = 0;
3895 HiIdx = 2;
3896 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003897 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003898 if (Elt.getOpcode() == ISD::UNDEF) {
3899 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003900 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003901 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3902 (*MaskPtr)[LoIdx] = Elt;
3903 LoIdx++;
3904 } else {
3905 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3906 (*MaskPtr)[HiIdx] = Elt;
3907 HiIdx++;
3908 }
3909 }
3910
Dan Gohman8181bd12008-07-27 21:46:04 +00003911 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003912 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3913 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003914 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003915 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3916 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003917 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003918 for (unsigned i = 0; i != 4; ++i) {
3919 if (Locs[i].first == -1) {
3920 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3921 } else {
3922 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3923 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3924 }
3925 }
3926 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3927 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3928 &MaskOps[0], MaskOps.size()));
3929}
3930
Dan Gohman8181bd12008-07-27 21:46:04 +00003931SDValue
3932X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3933 SDValue V1 = Op.getOperand(0);
3934 SDValue V2 = Op.getOperand(1);
3935 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003936 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003937 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003938 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003939 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3940 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3941 bool V1IsSplat = false;
3942 bool V2IsSplat = false;
3943
Gabor Greif1c80d112008-08-28 21:40:38 +00003944 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003945 return DAG.getNode(ISD::UNDEF, VT);
3946
Gabor Greif1c80d112008-08-28 21:40:38 +00003947 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003948 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003949
Gabor Greif1c80d112008-08-28 21:40:38 +00003950 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003951 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003952 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003953 return V2;
3954
Evan Chengae6c9212008-09-25 23:35:16 +00003955 // Canonicalize movddup shuffles.
3956 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Chengbdd9d9f2008-10-06 21:13:08 +00003957 VT.getSizeInBits() == 128 &&
Evan Chengae6c9212008-09-25 23:35:16 +00003958 X86::isMOVDDUPMask(PermMask.getNode()))
3959 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
3960
Gabor Greif1c80d112008-08-28 21:40:38 +00003961 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003962 if (isMMX || NumElems < 4) return Op;
3963 // Promote it to a v4{if}32 splat.
3964 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003965 }
3966
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003967 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3968 // do it!
3969 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003970 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003971 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003972 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3973 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3974 // FIXME: Figure out a cleaner way to do this.
3975 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00003976 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003977 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003978 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003979 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003980 SDValue NewV1 = NewOp.getOperand(0);
3981 SDValue NewV2 = NewOp.getOperand(1);
3982 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00003983 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003984 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003985 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003986 }
3987 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003988 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003989 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003990 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003991 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003992 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003993 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003994 }
3995 }
3996
Evan Chengdea99362008-05-29 08:22:04 +00003997 // Check if this can be converted into a logical shift.
3998 bool isLeft = false;
3999 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004000 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00004001 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4002 if (isShift && ShVal.hasOneUse()) {
4003 // If the shifted value has multiple uses, it may be cheaper to use
4004 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004005 MVT EVT = VT.getVectorElementType();
4006 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004007 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4008 }
4009
Gabor Greif1c80d112008-08-28 21:40:38 +00004010 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004011 if (V1IsUndef)
4012 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004013 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004014 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004015 if (!isMMX)
4016 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004017 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004018
Gabor Greif1c80d112008-08-28 21:40:38 +00004019 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4020 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4021 X86::isMOVHLPSMask(PermMask.getNode()) ||
4022 X86::isMOVHPMask(PermMask.getNode()) ||
4023 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004024 return Op;
4025
Gabor Greif1c80d112008-08-28 21:40:38 +00004026 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4027 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004028 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4029
Evan Chengdea99362008-05-29 08:22:04 +00004030 if (isShift) {
4031 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004032 MVT EVT = VT.getVectorElementType();
4033 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004034 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4035 }
4036
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004037 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004038 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4039 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004040 V1IsSplat = isSplatVector(V1.getNode());
4041 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004042
4043 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004044 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4045 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4046 std::swap(V1IsSplat, V2IsSplat);
4047 std::swap(V1IsUndef, V2IsUndef);
4048 Commuted = true;
4049 }
4050
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004051 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004052 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004053 if (V2IsUndef) return V1;
4054 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4055 if (V2IsSplat) {
4056 // V2 is a splat, so the mask may be malformed. That is, it may point
4057 // to any V2 element. The instruction selectior won't like this. Get
4058 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004059 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004060 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004061 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4062 }
4063 return Op;
4064 }
4065
Gabor Greif1c80d112008-08-28 21:40:38 +00004066 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4067 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4068 X86::isUNPCKLMask(PermMask.getNode()) ||
4069 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004070 return Op;
4071
4072 if (V2IsSplat) {
4073 // Normalize mask so all entries that point to V2 points to its first
4074 // element then try to match unpck{h|l} again. If match, return a
4075 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004076 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004077 if (NewMask.getNode() != PermMask.getNode()) {
4078 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004079 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004080 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004081 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004082 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004083 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4084 }
4085 }
4086 }
4087
4088 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004089 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004090 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4091
4092 if (Commuted) {
4093 // Commute is back and try unpck* again.
4094 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004095 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4096 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4097 X86::isUNPCKLMask(PermMask.getNode()) ||
4098 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004099 return Op;
4100 }
4101
Evan Chengbf8b2c52008-04-05 00:30:36 +00004102 // Try PSHUF* first, then SHUFP*.
4103 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4104 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004105 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004106 if (V2.getOpcode() != ISD::UNDEF)
4107 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4108 DAG.getNode(ISD::UNDEF, VT), PermMask);
4109 return Op;
4110 }
4111
4112 if (!isMMX) {
4113 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004114 (X86::isPSHUFDMask(PermMask.getNode()) ||
4115 X86::isPSHUFHWMask(PermMask.getNode()) ||
4116 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004117 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004118 if (VT == MVT::v4f32) {
4119 RVT = MVT::v4i32;
4120 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4121 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4122 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4123 } else if (V2.getOpcode() != ISD::UNDEF)
4124 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4125 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4126 if (RVT != VT)
4127 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004128 return Op;
4129 }
4130
Evan Chengbf8b2c52008-04-05 00:30:36 +00004131 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004132 if (X86::isSHUFPMask(PermMask.getNode()) ||
4133 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004134 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004135 }
4136
Evan Cheng75184a92007-12-11 01:46:18 +00004137 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4138 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004139 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004140 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004141 return NewOp;
4142 }
4143
Evan Chengf50554e2008-07-22 21:13:36 +00004144 // Handle all 4 wide cases with a number of shuffles except for MMX.
4145 if (NumElems == 4 && !isMMX)
4146 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004147
Dan Gohman8181bd12008-07-27 21:46:04 +00004148 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004149}
4150
Dan Gohman8181bd12008-07-27 21:46:04 +00004151SDValue
4152X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004153 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004154 MVT VT = Op.getValueType();
4155 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004156 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004157 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004158 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004159 DAG.getValueType(VT));
4160 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004161 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004162 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004163 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004164 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004165 DAG.getValueType(VT));
4166 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004167 } else if (VT == MVT::f32) {
4168 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4169 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004170 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004171 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004172 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004173 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004174 if (User->getOpcode() != ISD::STORE &&
4175 (User->getOpcode() != ISD::BIT_CONVERT ||
4176 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004177 return SDValue();
4178 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004179 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4180 Op.getOperand(1));
4181 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004182 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004183 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004184}
4185
4186
Dan Gohman8181bd12008-07-27 21:46:04 +00004187SDValue
4188X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004189 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004190 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004191
Evan Cheng6c249332008-03-24 21:52:23 +00004192 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004193 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004194 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004195 return Res;
4196 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004197
Duncan Sands92c43912008-06-06 12:08:01 +00004198 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004199 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004200 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004201 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004202 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004203 if (Idx == 0)
4204 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4205 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4206 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4207 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004208 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004209 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004210 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004211 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004212 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004213 DAG.getValueType(VT));
4214 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004215 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004216 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004217 if (Idx == 0)
4218 return Op;
4219 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004220 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004221 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004222 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004223 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004224 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004225 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004226 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004227 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004228 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004229 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004230 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004231 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004232 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004233 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4234 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4235 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004236 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004237 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004238 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4239 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4240 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004241 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004242 if (Idx == 0)
4243 return Op;
4244
4245 // UNPCKHPD the element to the lowest double word, then movsd.
4246 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4247 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004248 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004249 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004250 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004251 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004252 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004253 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004254 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004255 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004256 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4257 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4258 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004259 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004260 }
4261
Dan Gohman8181bd12008-07-27 21:46:04 +00004262 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004263}
4264
Dan Gohman8181bd12008-07-27 21:46:04 +00004265SDValue
4266X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004267 MVT VT = Op.getValueType();
4268 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004269
Dan Gohman8181bd12008-07-27 21:46:04 +00004270 SDValue N0 = Op.getOperand(0);
4271 SDValue N1 = Op.getOperand(1);
4272 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004273
Dan Gohman5a7af042008-08-14 22:53:18 +00004274 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4275 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004276 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004277 : X86ISD::PINSRW;
4278 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4279 // argument.
4280 if (N1.getValueType() != MVT::i32)
4281 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4282 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004283 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begemand77e59e2008-02-11 04:19:36 +00004284 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004285 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004286 // Bits [7:6] of the constant are the source select. This will always be
4287 // zero here. The DAG Combiner may combine an extract_elt index into these
4288 // bits. For example (insert (extract, 3), 2) could be matched by putting
4289 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4290 // Bits [5:4] of the constant are the destination select. This is the
4291 // value of the incoming immediate.
4292 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4293 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004294 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begemand77e59e2008-02-11 04:19:36 +00004295 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4296 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004297 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004298}
4299
Dan Gohman8181bd12008-07-27 21:46:04 +00004300SDValue
4301X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004302 MVT VT = Op.getValueType();
4303 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004304
4305 if (Subtarget->hasSSE41())
4306 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4307
Evan Chenge12a7eb2007-12-12 07:55:34 +00004308 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004309 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004310
Dan Gohman8181bd12008-07-27 21:46:04 +00004311 SDValue N0 = Op.getOperand(0);
4312 SDValue N1 = Op.getOperand(1);
4313 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004314
Duncan Sands92c43912008-06-06 12:08:01 +00004315 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004316 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4317 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004318 if (N1.getValueType() != MVT::i32)
4319 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4320 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004321 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004322 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004323 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004324 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004325}
4326
Dan Gohman8181bd12008-07-27 21:46:04 +00004327SDValue
4328X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004329 if (Op.getValueType() == MVT::v2f32)
4330 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4331 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4332 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4333 Op.getOperand(0))));
4334
Dan Gohman8181bd12008-07-27 21:46:04 +00004335 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004336 MVT VT = MVT::v2i32;
4337 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004338 default: break;
4339 case MVT::v16i8:
4340 case MVT::v8i16:
4341 VT = MVT::v4i32;
4342 break;
4343 }
4344 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4345 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004346}
4347
Bill Wendlingfef06052008-09-16 21:48:12 +00004348// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4349// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4350// one of the above mentioned nodes. It has to be wrapped because otherwise
4351// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4352// be used to form addressing mode. These wrapped nodes will be selected
4353// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004354SDValue
4355X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004356 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004357 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004358 getPointerTy(),
4359 CP->getAlignment());
4360 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4361 // With PIC, the address is actually $g + Offset.
4362 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4363 !Subtarget->isPICStyleRIPRel()) {
4364 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4365 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4366 Result);
4367 }
4368
4369 return Result;
4370}
4371
Dan Gohman8181bd12008-07-27 21:46:04 +00004372SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004373X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
4374 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00004375 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004376 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4377 // With PIC, the address is actually $g + Offset.
4378 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4379 !Subtarget->isPICStyleRIPRel()) {
4380 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4381 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4382 Result);
4383 }
4384
4385 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4386 // load the value at address GV, not the value of GV itself. This means that
4387 // the GlobalAddress must be in the base or index register of the address, not
4388 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4389 // The same applies for external symbols during PIC codegen
4390 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004391 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004392 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004393
4394 return Result;
4395}
4396
Evan Cheng7f250d62008-09-24 00:05:32 +00004397SDValue
4398X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4399 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4400 return LowerGlobalAddress(GV, DAG);
4401}
4402
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004403// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004404static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004405LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004406 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004407 SDValue InFlag;
4408 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004409 DAG.getNode(X86ISD::GlobalBaseReg,
4410 PtrVT), InFlag);
4411 InFlag = Chain.getValue(1);
4412
4413 // emit leal symbol@TLSGD(,%ebx,1), %eax
4414 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004415 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004416 GA->getValueType(0),
4417 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004418 SDValue Ops[] = { Chain, TGA, InFlag };
4419 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004420 InFlag = Result.getValue(2);
4421 Chain = Result.getValue(1);
4422
4423 // call ___tls_get_addr. This function receives its argument in
4424 // the register EAX.
4425 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4426 InFlag = Chain.getValue(1);
4427
4428 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004429 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004430 DAG.getTargetExternalSymbol("___tls_get_addr",
4431 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004432 DAG.getRegister(X86::EAX, PtrVT),
4433 DAG.getRegister(X86::EBX, PtrVT),
4434 InFlag };
4435 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4436 InFlag = Chain.getValue(1);
4437
4438 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4439}
4440
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004441// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004442static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004443LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004444 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004445 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004446
4447 // emit leaq symbol@TLSGD(%rip), %rdi
4448 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004449 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004450 GA->getValueType(0),
4451 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004452 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4453 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004454 Chain = Result.getValue(1);
4455 InFlag = Result.getValue(2);
4456
aslb204cd52008-08-16 12:58:29 +00004457 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004458 // the register RDI.
4459 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4460 InFlag = Chain.getValue(1);
4461
4462 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004463 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004464 DAG.getTargetExternalSymbol("__tls_get_addr",
4465 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004466 DAG.getRegister(X86::RDI, PtrVT),
4467 InFlag };
4468 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4469 InFlag = Chain.getValue(1);
4470
4471 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4472}
4473
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004474// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4475// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004476static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004477 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004478 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004479 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004480 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4481 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004482 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004483 GA->getValueType(0),
4484 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004485 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004486
4487 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004488 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004489 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004490
4491 // The address of the thread local variable is the add of the thread
4492 // pointer with the offset of the variable.
4493 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4494}
4495
Dan Gohman8181bd12008-07-27 21:46:04 +00004496SDValue
4497X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004498 // TODO: implement the "local dynamic" model
4499 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004500 assert(Subtarget->isTargetELF() &&
4501 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004502 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4503 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4504 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004505 if (Subtarget->is64Bit()) {
4506 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4507 } else {
4508 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4509 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4510 else
4511 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4512 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004513}
4514
Dan Gohman8181bd12008-07-27 21:46:04 +00004515SDValue
4516X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004517 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4518 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004519 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4520 // With PIC, the address is actually $g + Offset.
4521 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4522 !Subtarget->isPICStyleRIPRel()) {
4523 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4524 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4525 Result);
4526 }
4527
4528 return Result;
4529}
4530
Dan Gohman8181bd12008-07-27 21:46:04 +00004531SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004532 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004533 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004534 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4535 // With PIC, the address is actually $g + Offset.
4536 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4537 !Subtarget->isPICStyleRIPRel()) {
4538 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4539 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4540 Result);
4541 }
4542
4543 return Result;
4544}
4545
Chris Lattner62814a32007-10-17 06:02:13 +00004546/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4547/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004548SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004549 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004550 MVT VT = Op.getValueType();
4551 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004552 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004553 SDValue ShOpLo = Op.getOperand(0);
4554 SDValue ShOpHi = Op.getOperand(1);
4555 SDValue ShAmt = Op.getOperand(2);
4556 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004557 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4558 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004559
Dan Gohman8181bd12008-07-27 21:46:04 +00004560 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004561 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004562 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4563 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004564 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004565 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4566 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004567 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004568
Dan Gohman8181bd12008-07-27 21:46:04 +00004569 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004570 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004571 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004572 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004573
Dan Gohman8181bd12008-07-27 21:46:04 +00004574 SDValue Hi, Lo;
4575 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4576 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4577 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004578
Chris Lattner62814a32007-10-17 06:02:13 +00004579 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004580 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4581 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004582 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004583 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4584 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004585 }
4586
Dan Gohman8181bd12008-07-27 21:46:04 +00004587 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004588 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004589}
4590
Dan Gohman8181bd12008-07-27 21:46:04 +00004591SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004592 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004593 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004594 "Unknown SINT_TO_FP to lower!");
4595
4596 // These are really Legal; caller falls through into that case.
4597 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004598 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004599 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4600 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004601 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004602
Duncan Sands92c43912008-06-06 12:08:01 +00004603 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004604 MachineFunction &MF = DAG.getMachineFunction();
4605 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004606 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4607 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004608 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004609 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004610
4611 // Build the FILD
4612 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004613 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004614 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004615 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4616 else
4617 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004618 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004619 Ops.push_back(Chain);
4620 Ops.push_back(StackSlot);
4621 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004622 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004623 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004624
Dale Johannesen2fc20782007-09-14 22:26:36 +00004625 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004626 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004627 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004628
4629 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4630 // shouldn't be necessary except that RFP cannot be live across
4631 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4632 MachineFunction &MF = DAG.getMachineFunction();
4633 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004634 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004635 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004636 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004637 Ops.push_back(Chain);
4638 Ops.push_back(Result);
4639 Ops.push_back(StackSlot);
4640 Ops.push_back(DAG.getValueType(Op.getValueType()));
4641 Ops.push_back(InFlag);
4642 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004643 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004644 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004645 }
4646
4647 return Result;
4648}
4649
Dan Gohman8181bd12008-07-27 21:46:04 +00004650std::pair<SDValue,SDValue> X86TargetLowering::
4651FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004652 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4653 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004654 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004655
Dale Johannesen2fc20782007-09-14 22:26:36 +00004656 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004657 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004658 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004659 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004660 if (Subtarget->is64Bit() &&
4661 Op.getValueType() == MVT::i64 &&
4662 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004663 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004664
Evan Cheng05441e62007-10-15 20:11:21 +00004665 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4666 // stack slot.
4667 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004668 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004669 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004670 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004671 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004672 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004673 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4674 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4675 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4676 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004677 }
4678
Dan Gohman8181bd12008-07-27 21:46:04 +00004679 SDValue Chain = DAG.getEntryNode();
4680 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004681 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004682 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004683 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004684 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004685 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004686 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004687 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4688 };
4689 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4690 Chain = Value.getValue(1);
4691 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4692 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4693 }
4694
4695 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004696 SDValue Ops[] = { Chain, Value, StackSlot };
4697 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004698
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004699 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004700}
4701
Dan Gohman8181bd12008-07-27 21:46:04 +00004702SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4703 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4704 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004705 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004706
4707 // Load the result.
4708 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4709}
4710
4711SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004712 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4713 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004714 if (FIST.getNode() == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004715
4716 MVT VT = N->getValueType(0);
4717
4718 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004719 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004720
Duncan Sands698842f2008-07-02 17:40:58 +00004721 // Use MERGE_VALUES to drop the chain result value and get a node with one
4722 // result. This requires turning off getMergeValues simplification, since
4723 // otherwise it will give us Res back.
Gabor Greif1c80d112008-08-28 21:40:38 +00004724 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf19591c2008-06-30 10:19:09 +00004725}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004726
Dan Gohman8181bd12008-07-27 21:46:04 +00004727SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004728 MVT VT = Op.getValueType();
4729 MVT EltVT = VT;
4730 if (VT.isVector())
4731 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004732 std::vector<Constant*> CV;
4733 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004734 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004735 CV.push_back(C);
4736 CV.push_back(C);
4737 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004738 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004739 CV.push_back(C);
4740 CV.push_back(C);
4741 CV.push_back(C);
4742 CV.push_back(C);
4743 }
Dan Gohman11821702007-07-27 17:16:43 +00004744 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004745 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4746 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004747 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004748 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004749 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4750}
4751
Dan Gohman8181bd12008-07-27 21:46:04 +00004752SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004753 MVT VT = Op.getValueType();
4754 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004755 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004756 if (VT.isVector()) {
4757 EltVT = VT.getVectorElementType();
4758 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004759 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004760 std::vector<Constant*> CV;
4761 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004762 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004763 CV.push_back(C);
4764 CV.push_back(C);
4765 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004766 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004767 CV.push_back(C);
4768 CV.push_back(C);
4769 CV.push_back(C);
4770 CV.push_back(C);
4771 }
Dan Gohman11821702007-07-27 17:16:43 +00004772 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004773 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4774 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004775 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004776 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004777 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004778 return DAG.getNode(ISD::BIT_CONVERT, VT,
4779 DAG.getNode(ISD::XOR, MVT::v2i64,
4780 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4781 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4782 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004783 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4784 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004785}
4786
Dan Gohman8181bd12008-07-27 21:46:04 +00004787SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4788 SDValue Op0 = Op.getOperand(0);
4789 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004790 MVT VT = Op.getValueType();
4791 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004792
4793 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004794 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004795 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4796 SrcVT = VT;
4797 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004798 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004799 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004800 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004801 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004802 }
4803
4804 // At this point the operands and the result should have the same
4805 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004806
4807 // First get the sign bit of second operand.
4808 std::vector<Constant*> CV;
4809 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004810 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4811 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004812 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004813 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4814 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4815 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4816 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004817 }
Dan Gohman11821702007-07-27 17:16:43 +00004818 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004819 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4820 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004821 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004822 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004823 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004824
4825 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004826 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004827 // Op0 is MVT::f32, Op1 is MVT::f64.
4828 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4829 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4830 DAG.getConstant(32, MVT::i32));
4831 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4832 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004833 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004834 }
4835
4836 // Clear first operand sign bit.
4837 CV.clear();
4838 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004839 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4840 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004841 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004842 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4843 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4844 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4845 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004846 }
Dan Gohman11821702007-07-27 17:16:43 +00004847 C = ConstantVector::get(CV);
4848 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004849 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004850 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004851 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004852 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004853
4854 // Or the value with the sign bit.
4855 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4856}
4857
Dan Gohman8181bd12008-07-27 21:46:04 +00004858SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004859 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004860 SDValue Cond;
4861 SDValue Op0 = Op.getOperand(0);
4862 SDValue Op1 = Op.getOperand(1);
4863 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004864 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004865 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004866 unsigned X86CC;
4867
Evan Cheng950aac02007-09-25 01:57:46 +00004868 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004869 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004870 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4871 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004872 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004873 }
Evan Cheng950aac02007-09-25 01:57:46 +00004874
4875 assert(isFP && "Illegal integer SetCC!");
4876
Evan Cheng621216e2007-09-29 00:00:36 +00004877 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004878 switch (SetCCOpcode) {
4879 default: assert(false && "Illegal floating point SetCC!");
4880 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004881 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004882 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004883 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004884 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4885 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4886 }
4887 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004888 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004889 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004890 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004891 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4892 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4893 }
4894 }
4895}
4896
Dan Gohman8181bd12008-07-27 21:46:04 +00004897SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4898 SDValue Cond;
4899 SDValue Op0 = Op.getOperand(0);
4900 SDValue Op1 = Op.getOperand(1);
4901 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004902 MVT VT = Op.getValueType();
4903 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4904 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4905
4906 if (isFP) {
4907 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004908 MVT VT0 = Op0.getValueType();
4909 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4910 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004911 bool Swap = false;
4912
4913 switch (SetCCOpcode) {
4914 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004915 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004916 case ISD::SETEQ: SSECC = 0; break;
4917 case ISD::SETOGT:
4918 case ISD::SETGT: Swap = true; // Fallthrough
4919 case ISD::SETLT:
4920 case ISD::SETOLT: SSECC = 1; break;
4921 case ISD::SETOGE:
4922 case ISD::SETGE: Swap = true; // Fallthrough
4923 case ISD::SETLE:
4924 case ISD::SETOLE: SSECC = 2; break;
4925 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004926 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004927 case ISD::SETNE: SSECC = 4; break;
4928 case ISD::SETULE: Swap = true;
4929 case ISD::SETUGE: SSECC = 5; break;
4930 case ISD::SETULT: Swap = true;
4931 case ISD::SETUGT: SSECC = 6; break;
4932 case ISD::SETO: SSECC = 7; break;
4933 }
4934 if (Swap)
4935 std::swap(Op0, Op1);
4936
Nate Begeman6357f9d2008-07-25 19:05:58 +00004937 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004938 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004939 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004940 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004941 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4942 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4943 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4944 }
4945 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004946 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004947 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4948 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4949 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4950 }
4951 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004952 }
4953 // Handle all other FP comparisons here.
4954 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4955 }
4956
4957 // We are handling one of the integer comparisons here. Since SSE only has
4958 // GT and EQ comparisons for integer, swapping operands and multiple
4959 // operations may be required for some comparisons.
4960 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4961 bool Swap = false, Invert = false, FlipSigns = false;
4962
4963 switch (VT.getSimpleVT()) {
4964 default: break;
4965 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4966 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4967 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4968 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4969 }
4970
4971 switch (SetCCOpcode) {
4972 default: break;
4973 case ISD::SETNE: Invert = true;
4974 case ISD::SETEQ: Opc = EQOpc; break;
4975 case ISD::SETLT: Swap = true;
4976 case ISD::SETGT: Opc = GTOpc; break;
4977 case ISD::SETGE: Swap = true;
4978 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4979 case ISD::SETULT: Swap = true;
4980 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4981 case ISD::SETUGE: Swap = true;
4982 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4983 }
4984 if (Swap)
4985 std::swap(Op0, Op1);
4986
4987 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4988 // bits of the inputs before performing those operations.
4989 if (FlipSigns) {
4990 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004991 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4992 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4993 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004994 SignBits.size());
4995 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4996 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4997 }
4998
Dan Gohman8181bd12008-07-27 21:46:04 +00004999 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005000
5001 // If the logical-not of the result is required, perform that now.
5002 if (Invert) {
5003 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005004 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
5005 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
5006 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005007 NegOnes.size());
5008 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
5009 }
5010 return Result;
5011}
Evan Cheng950aac02007-09-25 01:57:46 +00005012
Dan Gohman8181bd12008-07-27 21:46:04 +00005013SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005014 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005015 SDValue Cond = Op.getOperand(0);
5016 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005017
5018 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005019 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005020
Evan Cheng50d37ab2007-10-08 22:16:29 +00005021 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5022 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005023 if (Cond.getOpcode() == X86ISD::SETCC) {
5024 CC = Cond.getOperand(0);
5025
Dan Gohman8181bd12008-07-27 21:46:04 +00005026 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005027 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005028 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005029
Evan Cheng50d37ab2007-10-08 22:16:29 +00005030 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005031 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005032 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005033 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005034
Evan Cheng621216e2007-09-29 00:00:36 +00005035 if ((Opc == X86ISD::CMP ||
5036 Opc == X86ISD::COMI ||
5037 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005038 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005039 addTest = false;
5040 }
5041 }
5042
5043 if (addTest) {
5044 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00005045 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005046 }
5047
Duncan Sands92c43912008-06-06 12:08:01 +00005048 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005049 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005050 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005051 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5052 // condition is true.
5053 Ops.push_back(Op.getOperand(2));
5054 Ops.push_back(Op.getOperand(1));
5055 Ops.push_back(CC);
5056 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00005057 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005058}
5059
Dan Gohman8181bd12008-07-27 21:46:04 +00005060SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005061 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005062 SDValue Chain = Op.getOperand(0);
5063 SDValue Cond = Op.getOperand(1);
5064 SDValue Dest = Op.getOperand(2);
5065 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005066
5067 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005068 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005069
Evan Cheng50d37ab2007-10-08 22:16:29 +00005070 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5071 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005072 if (Cond.getOpcode() == X86ISD::SETCC) {
5073 CC = Cond.getOperand(0);
5074
Dan Gohman8181bd12008-07-27 21:46:04 +00005075 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005076 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00005077 if (Opc == X86ISD::CMP ||
5078 Opc == X86ISD::COMI ||
5079 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005080 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005081 addTest = false;
5082 }
5083 }
5084
5085 if (addTest) {
5086 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005087 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005088 }
Evan Cheng621216e2007-09-29 00:00:36 +00005089 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005090 Chain, Op.getOperand(2), CC, Cond);
5091}
5092
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005093
5094// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5095// Calls to _alloca is needed to probe the stack when allocating more than 4k
5096// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5097// that the guard pages used by the OS virtual memory manager are allocated in
5098// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005099SDValue
5100X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005101 SelectionDAG &DAG) {
5102 assert(Subtarget->isTargetCygMing() &&
5103 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005104
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005105 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005106 SDValue Chain = Op.getOperand(0);
5107 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005108 // FIXME: Ensure alignment here
5109
Dan Gohman8181bd12008-07-27 21:46:04 +00005110 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005111
Duncan Sands92c43912008-06-06 12:08:01 +00005112 MVT IntPtr = getPointerTy();
5113 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005114
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005115 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
5116
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005117 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5118 Flag = Chain.getValue(1);
5119
5120 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005121 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005122 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005123 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005124 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005125 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005126 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005127 Flag = Chain.getValue(1);
5128
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005129 Chain = DAG.getCALLSEQ_END(Chain,
5130 DAG.getIntPtrConstant(0),
5131 DAG.getIntPtrConstant(0),
5132 Flag);
5133
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005134 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005135
Dan Gohman8181bd12008-07-27 21:46:04 +00005136 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005137 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005138}
5139
Dan Gohman8181bd12008-07-27 21:46:04 +00005140SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005141X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005142 SDValue Chain,
5143 SDValue Dst, SDValue Src,
5144 SDValue Size, unsigned Align,
5145 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005146 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005147 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005148
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005149 // If not DWORD aligned or size is more than the threshold, call the library.
5150 // The libc version is likely to be faster for these cases. It can use the
5151 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005152 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005153 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005154 ConstantSize->getZExtValue() >
5155 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005156 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005157
5158 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005159 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005160
Bill Wendling4b2e3782008-10-01 00:59:58 +00005161 if (const char *bzeroEntry = V &&
5162 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5163 MVT IntPtr = getPointerTy();
5164 const Type *IntPtrTy = TD->getIntPtrType();
5165 TargetLowering::ArgListTy Args;
5166 TargetLowering::ArgListEntry Entry;
5167 Entry.Node = Dst;
5168 Entry.Ty = IntPtrTy;
5169 Args.push_back(Entry);
5170 Entry.Node = Size;
5171 Args.push_back(Entry);
5172 std::pair<SDValue,SDValue> CallResult =
5173 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5174 CallingConv::C, false,
5175 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG);
5176 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005177 }
5178
Dan Gohmane8b391e2008-04-12 04:36:06 +00005179 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005180 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005181 }
5182
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005183 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005184 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005185 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005186 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005187 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005188 unsigned BytesLeft = 0;
5189 bool TwoRepStos = false;
5190 if (ValC) {
5191 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005192 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005193
5194 // If the value is a constant, then we can potentially use larger sets.
5195 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005196 case 2: // WORD aligned
5197 AVT = MVT::i16;
5198 ValReg = X86::AX;
5199 Val = (Val << 8) | Val;
5200 break;
5201 case 0: // DWORD aligned
5202 AVT = MVT::i32;
5203 ValReg = X86::EAX;
5204 Val = (Val << 8) | Val;
5205 Val = (Val << 16) | Val;
5206 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5207 AVT = MVT::i64;
5208 ValReg = X86::RAX;
5209 Val = (Val << 32) | Val;
5210 }
5211 break;
5212 default: // Byte aligned
5213 AVT = MVT::i8;
5214 ValReg = X86::AL;
5215 Count = DAG.getIntPtrConstant(SizeVal);
5216 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005217 }
5218
Duncan Sandsec142ee2008-06-08 20:54:56 +00005219 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005220 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005221 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5222 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005223 }
5224
5225 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5226 InFlag);
5227 InFlag = Chain.getValue(1);
5228 } else {
5229 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005230 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005231 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005232 InFlag = Chain.getValue(1);
5233 }
5234
5235 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5236 Count, InFlag);
5237 InFlag = Chain.getValue(1);
5238 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005239 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005240 InFlag = Chain.getValue(1);
5241
5242 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005243 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005244 Ops.push_back(Chain);
5245 Ops.push_back(DAG.getValueType(AVT));
5246 Ops.push_back(InFlag);
5247 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5248
5249 if (TwoRepStos) {
5250 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005251 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005252 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005253 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005254 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5255 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5256 Left, InFlag);
5257 InFlag = Chain.getValue(1);
5258 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5259 Ops.clear();
5260 Ops.push_back(Chain);
5261 Ops.push_back(DAG.getValueType(MVT::i8));
5262 Ops.push_back(InFlag);
5263 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5264 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005265 // Handle the last 1 - 7 bytes.
5266 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005267 MVT AddrVT = Dst.getValueType();
5268 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005269
5270 Chain = DAG.getMemset(Chain,
5271 DAG.getNode(ISD::ADD, AddrVT, Dst,
5272 DAG.getConstant(Offset, AddrVT)),
5273 Src,
5274 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005275 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005276 }
5277
Dan Gohmane8b391e2008-04-12 04:36:06 +00005278 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005279 return Chain;
5280}
5281
Dan Gohman8181bd12008-07-27 21:46:04 +00005282SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005283X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005284 SDValue Chain, SDValue Dst, SDValue Src,
5285 SDValue Size, unsigned Align,
5286 bool AlwaysInline,
5287 const Value *DstSV, uint64_t DstSVOff,
5288 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005289 // This requires the copy size to be a constant, preferrably
5290 // within a subtarget-specific limit.
5291 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5292 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005293 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005294 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005295 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005296 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005297
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005298 /// If not DWORD aligned, call the library.
5299 if ((Align & 3) != 0)
5300 return SDValue();
5301
5302 // DWORD aligned
5303 MVT AVT = MVT::i32;
5304 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005305 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005306
Duncan Sands92c43912008-06-06 12:08:01 +00005307 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005308 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005309 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005310 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005311
Dan Gohman8181bd12008-07-27 21:46:04 +00005312 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005313 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5314 Count, InFlag);
5315 InFlag = Chain.getValue(1);
5316 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005317 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005318 InFlag = Chain.getValue(1);
5319 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005320 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005321 InFlag = Chain.getValue(1);
5322
5323 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005324 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325 Ops.push_back(Chain);
5326 Ops.push_back(DAG.getValueType(AVT));
5327 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005328 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005329
Dan Gohman8181bd12008-07-27 21:46:04 +00005330 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005331 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005332 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005333 // Handle the last 1 - 7 bytes.
5334 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005335 MVT DstVT = Dst.getValueType();
5336 MVT SrcVT = Src.getValueType();
5337 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005338 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005339 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005340 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005341 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005342 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005343 DAG.getConstant(BytesLeft, SizeVT),
5344 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005345 DstSV, DstSVOff + Offset,
5346 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005347 }
5348
Dan Gohmane8b391e2008-04-12 04:36:06 +00005349 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005350}
5351
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005352/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5353SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005354 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005355 SDValue TheChain = N->getOperand(0);
5356 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005357 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005358 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5359 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005360 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005361 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005362 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005363 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005364 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005365 };
5366
Gabor Greif1c80d112008-08-28 21:40:38 +00005367 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005368 }
5369
Dan Gohman8181bd12008-07-27 21:46:04 +00005370 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5371 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005372 MVT::i32, eax.getValue(2));
5373 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005374 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005375 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5376
5377 // Use a MERGE_VALUES to return the value and chain.
5378 Ops[1] = edx.getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00005379 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005380}
5381
Dan Gohman8181bd12008-07-27 21:46:04 +00005382SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005383 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005384
5385 if (!Subtarget->is64Bit()) {
5386 // vastart just stores the address of the VarArgsFrameIndex slot into the
5387 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005388 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005389 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005390 }
5391
5392 // __va_list_tag:
5393 // gp_offset (0 - 6 * 8)
5394 // fp_offset (48 - 48 + 8 * 16)
5395 // overflow_arg_area (point to parameters coming in memory).
5396 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005397 SmallVector<SDValue, 8> MemOps;
5398 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005399 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005400 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005401 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005402 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005403 MemOps.push_back(Store);
5404
5405 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005406 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005407 Store = DAG.getStore(Op.getOperand(0),
5408 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005409 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005410 MemOps.push_back(Store);
5411
5412 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005413 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005414 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005415 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005416 MemOps.push_back(Store);
5417
5418 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005419 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005420 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005421 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005422 MemOps.push_back(Store);
5423 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5424}
5425
Dan Gohman8181bd12008-07-27 21:46:04 +00005426SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005427 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5428 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005429 SDValue Chain = Op.getOperand(0);
5430 SDValue SrcPtr = Op.getOperand(1);
5431 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005432
5433 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5434 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005435 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005436}
5437
Dan Gohman8181bd12008-07-27 21:46:04 +00005438SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005439 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005440 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005441 SDValue Chain = Op.getOperand(0);
5442 SDValue DstPtr = Op.getOperand(1);
5443 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005444 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5445 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005446
Dan Gohman840ff5c2008-04-18 20:55:41 +00005447 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5448 DAG.getIntPtrConstant(24), 8, false,
5449 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005450}
5451
Dan Gohman8181bd12008-07-27 21:46:04 +00005452SDValue
5453X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005454 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005455 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005456 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005457 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005458 case Intrinsic::x86_sse_comieq_ss:
5459 case Intrinsic::x86_sse_comilt_ss:
5460 case Intrinsic::x86_sse_comile_ss:
5461 case Intrinsic::x86_sse_comigt_ss:
5462 case Intrinsic::x86_sse_comige_ss:
5463 case Intrinsic::x86_sse_comineq_ss:
5464 case Intrinsic::x86_sse_ucomieq_ss:
5465 case Intrinsic::x86_sse_ucomilt_ss:
5466 case Intrinsic::x86_sse_ucomile_ss:
5467 case Intrinsic::x86_sse_ucomigt_ss:
5468 case Intrinsic::x86_sse_ucomige_ss:
5469 case Intrinsic::x86_sse_ucomineq_ss:
5470 case Intrinsic::x86_sse2_comieq_sd:
5471 case Intrinsic::x86_sse2_comilt_sd:
5472 case Intrinsic::x86_sse2_comile_sd:
5473 case Intrinsic::x86_sse2_comigt_sd:
5474 case Intrinsic::x86_sse2_comige_sd:
5475 case Intrinsic::x86_sse2_comineq_sd:
5476 case Intrinsic::x86_sse2_ucomieq_sd:
5477 case Intrinsic::x86_sse2_ucomilt_sd:
5478 case Intrinsic::x86_sse2_ucomile_sd:
5479 case Intrinsic::x86_sse2_ucomigt_sd:
5480 case Intrinsic::x86_sse2_ucomige_sd:
5481 case Intrinsic::x86_sse2_ucomineq_sd: {
5482 unsigned Opc = 0;
5483 ISD::CondCode CC = ISD::SETCC_INVALID;
5484 switch (IntNo) {
5485 default: break;
5486 case Intrinsic::x86_sse_comieq_ss:
5487 case Intrinsic::x86_sse2_comieq_sd:
5488 Opc = X86ISD::COMI;
5489 CC = ISD::SETEQ;
5490 break;
5491 case Intrinsic::x86_sse_comilt_ss:
5492 case Intrinsic::x86_sse2_comilt_sd:
5493 Opc = X86ISD::COMI;
5494 CC = ISD::SETLT;
5495 break;
5496 case Intrinsic::x86_sse_comile_ss:
5497 case Intrinsic::x86_sse2_comile_sd:
5498 Opc = X86ISD::COMI;
5499 CC = ISD::SETLE;
5500 break;
5501 case Intrinsic::x86_sse_comigt_ss:
5502 case Intrinsic::x86_sse2_comigt_sd:
5503 Opc = X86ISD::COMI;
5504 CC = ISD::SETGT;
5505 break;
5506 case Intrinsic::x86_sse_comige_ss:
5507 case Intrinsic::x86_sse2_comige_sd:
5508 Opc = X86ISD::COMI;
5509 CC = ISD::SETGE;
5510 break;
5511 case Intrinsic::x86_sse_comineq_ss:
5512 case Intrinsic::x86_sse2_comineq_sd:
5513 Opc = X86ISD::COMI;
5514 CC = ISD::SETNE;
5515 break;
5516 case Intrinsic::x86_sse_ucomieq_ss:
5517 case Intrinsic::x86_sse2_ucomieq_sd:
5518 Opc = X86ISD::UCOMI;
5519 CC = ISD::SETEQ;
5520 break;
5521 case Intrinsic::x86_sse_ucomilt_ss:
5522 case Intrinsic::x86_sse2_ucomilt_sd:
5523 Opc = X86ISD::UCOMI;
5524 CC = ISD::SETLT;
5525 break;
5526 case Intrinsic::x86_sse_ucomile_ss:
5527 case Intrinsic::x86_sse2_ucomile_sd:
5528 Opc = X86ISD::UCOMI;
5529 CC = ISD::SETLE;
5530 break;
5531 case Intrinsic::x86_sse_ucomigt_ss:
5532 case Intrinsic::x86_sse2_ucomigt_sd:
5533 Opc = X86ISD::UCOMI;
5534 CC = ISD::SETGT;
5535 break;
5536 case Intrinsic::x86_sse_ucomige_ss:
5537 case Intrinsic::x86_sse2_ucomige_sd:
5538 Opc = X86ISD::UCOMI;
5539 CC = ISD::SETGE;
5540 break;
5541 case Intrinsic::x86_sse_ucomineq_ss:
5542 case Intrinsic::x86_sse2_ucomineq_sd:
5543 Opc = X86ISD::UCOMI;
5544 CC = ISD::SETNE;
5545 break;
5546 }
5547
5548 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005549 SDValue LHS = Op.getOperand(1);
5550 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005551 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5552
Dan Gohman8181bd12008-07-27 21:46:04 +00005553 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5554 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005555 DAG.getConstant(X86CC, MVT::i8), Cond);
5556 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005557 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005558
5559 // Fix vector shift instructions where the last operand is a non-immediate
5560 // i32 value.
5561 case Intrinsic::x86_sse2_pslli_w:
5562 case Intrinsic::x86_sse2_pslli_d:
5563 case Intrinsic::x86_sse2_pslli_q:
5564 case Intrinsic::x86_sse2_psrli_w:
5565 case Intrinsic::x86_sse2_psrli_d:
5566 case Intrinsic::x86_sse2_psrli_q:
5567 case Intrinsic::x86_sse2_psrai_w:
5568 case Intrinsic::x86_sse2_psrai_d:
5569 case Intrinsic::x86_mmx_pslli_w:
5570 case Intrinsic::x86_mmx_pslli_d:
5571 case Intrinsic::x86_mmx_pslli_q:
5572 case Intrinsic::x86_mmx_psrli_w:
5573 case Intrinsic::x86_mmx_psrli_d:
5574 case Intrinsic::x86_mmx_psrli_q:
5575 case Intrinsic::x86_mmx_psrai_w:
5576 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005577 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005578 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005579 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005580
5581 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005582 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005583 switch (IntNo) {
5584 case Intrinsic::x86_sse2_pslli_w:
5585 NewIntNo = Intrinsic::x86_sse2_psll_w;
5586 break;
5587 case Intrinsic::x86_sse2_pslli_d:
5588 NewIntNo = Intrinsic::x86_sse2_psll_d;
5589 break;
5590 case Intrinsic::x86_sse2_pslli_q:
5591 NewIntNo = Intrinsic::x86_sse2_psll_q;
5592 break;
5593 case Intrinsic::x86_sse2_psrli_w:
5594 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5595 break;
5596 case Intrinsic::x86_sse2_psrli_d:
5597 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5598 break;
5599 case Intrinsic::x86_sse2_psrli_q:
5600 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5601 break;
5602 case Intrinsic::x86_sse2_psrai_w:
5603 NewIntNo = Intrinsic::x86_sse2_psra_w;
5604 break;
5605 case Intrinsic::x86_sse2_psrai_d:
5606 NewIntNo = Intrinsic::x86_sse2_psra_d;
5607 break;
5608 default: {
5609 ShAmtVT = MVT::v2i32;
5610 switch (IntNo) {
5611 case Intrinsic::x86_mmx_pslli_w:
5612 NewIntNo = Intrinsic::x86_mmx_psll_w;
5613 break;
5614 case Intrinsic::x86_mmx_pslli_d:
5615 NewIntNo = Intrinsic::x86_mmx_psll_d;
5616 break;
5617 case Intrinsic::x86_mmx_pslli_q:
5618 NewIntNo = Intrinsic::x86_mmx_psll_q;
5619 break;
5620 case Intrinsic::x86_mmx_psrli_w:
5621 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5622 break;
5623 case Intrinsic::x86_mmx_psrli_d:
5624 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5625 break;
5626 case Intrinsic::x86_mmx_psrli_q:
5627 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5628 break;
5629 case Intrinsic::x86_mmx_psrai_w:
5630 NewIntNo = Intrinsic::x86_mmx_psra_w;
5631 break;
5632 case Intrinsic::x86_mmx_psrai_d:
5633 NewIntNo = Intrinsic::x86_mmx_psra_d;
5634 break;
5635 default: abort(); // Can't reach here.
5636 }
5637 break;
5638 }
5639 }
Duncan Sands92c43912008-06-06 12:08:01 +00005640 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005641 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5642 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5643 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5644 DAG.getConstant(NewIntNo, MVT::i32),
5645 Op.getOperand(1), ShAmt);
5646 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005647 }
5648}
5649
Dan Gohman8181bd12008-07-27 21:46:04 +00005650SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005651 // Depths > 0 not supported yet!
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005652 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005653 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005654
5655 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005656 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005657 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5658}
5659
Dan Gohman8181bd12008-07-27 21:46:04 +00005660SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00005661 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
5662 MFI->setFrameAddressIsTaken(true);
5663 MVT VT = Op.getValueType();
5664 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5665 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
5666 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), FrameReg, VT);
5667 while (Depth--)
5668 FrameAddr = DAG.getLoad(VT, DAG.getEntryNode(), FrameAddr, NULL, 0);
5669 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005670}
5671
Dan Gohman8181bd12008-07-27 21:46:04 +00005672SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005673 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005674 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005675}
5676
Dan Gohman8181bd12008-07-27 21:46:04 +00005677SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005678{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005679 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005680 SDValue Chain = Op.getOperand(0);
5681 SDValue Offset = Op.getOperand(1);
5682 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005683
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005684 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5685 getPointerTy());
5686 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005687
Dan Gohman8181bd12008-07-27 21:46:04 +00005688 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005689 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005690 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5691 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005692 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5693 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005694
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005695 return DAG.getNode(X86ISD::EH_RETURN,
5696 MVT::Other,
5697 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005698}
5699
Dan Gohman8181bd12008-07-27 21:46:04 +00005700SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005701 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005702 SDValue Root = Op.getOperand(0);
5703 SDValue Trmp = Op.getOperand(1); // trampoline
5704 SDValue FPtr = Op.getOperand(2); // nested function
5705 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005706
Dan Gohman12a9c082008-02-06 22:27:42 +00005707 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005708
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005709 const X86InstrInfo *TII =
5710 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5711
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005712 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005713 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005714
5715 // Large code-model.
5716
5717 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5718 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5719
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005720 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5721 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005722
5723 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5724
5725 // Load the pointer to the nested function into R11.
5726 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005727 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005728 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005729 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005730
5731 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005732 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005733
5734 // Load the 'nest' parameter value into R10.
5735 // R10 is specified in X86CallingConv.td
5736 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5737 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5738 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005739 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005740
5741 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005742 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005743
5744 // Jump to the nested function.
5745 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5746 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5747 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005748 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005749
5750 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5751 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5752 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005753 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005754
Dan Gohman8181bd12008-07-27 21:46:04 +00005755 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005756 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005757 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005758 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005759 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005760 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5761 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005762 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005763
5764 switch (CC) {
5765 default:
5766 assert(0 && "Unsupported calling convention");
5767 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005768 case CallingConv::X86_StdCall: {
5769 // Pass 'nest' parameter in ECX.
5770 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005771 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005772
5773 // Check that ECX wasn't needed by an 'inreg' parameter.
5774 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00005775 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005776
Chris Lattner1c8733e2008-03-12 17:45:29 +00005777 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005778 unsigned InRegCount = 0;
5779 unsigned Idx = 1;
5780
5781 for (FunctionType::param_iterator I = FTy->param_begin(),
5782 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00005783 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005784 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005785 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005786
5787 if (InRegCount > 2) {
5788 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5789 abort();
5790 }
5791 }
5792 break;
5793 }
5794 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00005795 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005796 // Pass 'nest' parameter in EAX.
5797 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005798 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005799 break;
5800 }
5801
Dan Gohman8181bd12008-07-27 21:46:04 +00005802 SDValue OutChains[4];
5803 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005804
5805 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5806 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5807
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005808 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005809 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005810 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005811 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005812
5813 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005814 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005815
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005816 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005817 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5818 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005819 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005820
5821 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005822 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005823
Dan Gohman8181bd12008-07-27 21:46:04 +00005824 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005825 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005826 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005827 }
5828}
5829
Dan Gohman8181bd12008-07-27 21:46:04 +00005830SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005831 /*
5832 The rounding mode is in bits 11:10 of FPSR, and has the following
5833 settings:
5834 00 Round to nearest
5835 01 Round to -inf
5836 10 Round to +inf
5837 11 Round to 0
5838
5839 FLT_ROUNDS, on the other hand, expects the following:
5840 -1 Undefined
5841 0 Round to 0
5842 1 Round to nearest
5843 2 Round to +inf
5844 3 Round to -inf
5845
5846 To perform the conversion, we do:
5847 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5848 */
5849
5850 MachineFunction &MF = DAG.getMachineFunction();
5851 const TargetMachine &TM = MF.getTarget();
5852 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5853 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005854 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005855
5856 // Save FP Control Word to stack slot
5857 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005858 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005859
Dan Gohman8181bd12008-07-27 21:46:04 +00005860 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00005861 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005862
5863 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005864 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005865
5866 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005867 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005868 DAG.getNode(ISD::SRL, MVT::i16,
5869 DAG.getNode(ISD::AND, MVT::i16,
5870 CWD, DAG.getConstant(0x800, MVT::i16)),
5871 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005872 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005873 DAG.getNode(ISD::SRL, MVT::i16,
5874 DAG.getNode(ISD::AND, MVT::i16,
5875 CWD, DAG.getConstant(0x400, MVT::i16)),
5876 DAG.getConstant(9, MVT::i8));
5877
Dan Gohman8181bd12008-07-27 21:46:04 +00005878 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005879 DAG.getNode(ISD::AND, MVT::i16,
5880 DAG.getNode(ISD::ADD, MVT::i16,
5881 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5882 DAG.getConstant(1, MVT::i16)),
5883 DAG.getConstant(3, MVT::i16));
5884
5885
Duncan Sands92c43912008-06-06 12:08:01 +00005886 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005887 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5888}
5889
Dan Gohman8181bd12008-07-27 21:46:04 +00005890SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005891 MVT VT = Op.getValueType();
5892 MVT OpVT = VT;
5893 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005894
5895 Op = Op.getOperand(0);
5896 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005897 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005898 OpVT = MVT::i32;
5899 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5900 }
Evan Cheng48679f42007-12-14 02:13:44 +00005901
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005902 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5903 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5904 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5905
5906 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005907 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005908 Ops.push_back(Op);
5909 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5910 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5911 Ops.push_back(Op.getValue(1));
5912 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5913
5914 // Finally xor with NumBits-1.
5915 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5916
Evan Cheng48679f42007-12-14 02:13:44 +00005917 if (VT == MVT::i8)
5918 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5919 return Op;
5920}
5921
Dan Gohman8181bd12008-07-27 21:46:04 +00005922SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005923 MVT VT = Op.getValueType();
5924 MVT OpVT = VT;
5925 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005926
5927 Op = Op.getOperand(0);
5928 if (VT == MVT::i8) {
5929 OpVT = MVT::i32;
5930 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5931 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005932
5933 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5934 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5935 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5936
5937 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005938 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005939 Ops.push_back(Op);
5940 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5941 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5942 Ops.push_back(Op.getValue(1));
5943 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5944
Evan Cheng48679f42007-12-14 02:13:44 +00005945 if (VT == MVT::i8)
5946 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5947 return Op;
5948}
5949
Dan Gohman8181bd12008-07-27 21:46:04 +00005950SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005951 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005952 unsigned Reg = 0;
5953 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005954 switch(T.getSimpleVT()) {
5955 default:
5956 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005957 case MVT::i8: Reg = X86::AL; size = 1; break;
5958 case MVT::i16: Reg = X86::AX; size = 2; break;
5959 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005960 case MVT::i64:
5961 if (Subtarget->is64Bit()) {
5962 Reg = X86::RAX; size = 8;
5963 } else //Should go away when LowerType stuff lands
Gabor Greif1c80d112008-08-28 21:40:38 +00005964 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005965 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005966 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005967 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00005968 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00005969 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00005970 Op.getOperand(1),
5971 Op.getOperand(3),
5972 DAG.getTargetConstant(size, MVT::i8),
5973 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005974 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005975 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5976 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005977 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5978 return cpOut;
5979}
5980
Gabor Greif825aa892008-08-28 23:19:51 +00005981SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
5982 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005983 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005984 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005985 SDValue cpInL, cpInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00005986 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00005987 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00005988 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00005989 DAG.getConstant(1, MVT::i32));
5990 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00005991 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00005992 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5993 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005994 SDValue swapInL, swapInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00005995 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00005996 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00005997 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00005998 DAG.getConstant(1, MVT::i32));
5999 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
6000 swapInL, cpInH.getValue(1));
6001 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
6002 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00006003 SDValue Ops[] = { swapInH.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006004 Op->getOperand(1),
6005 swapInH.getValue(1) };
Andrew Lenharth81580822008-03-05 01:15:49 +00006006 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006007 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
6008 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00006009 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00006010 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00006011 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00006012 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
6013 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
6014 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greif1c80d112008-08-28 21:40:38 +00006015 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharth81580822008-03-05 01:15:49 +00006016}
6017
Dale Johannesenf160d802008-10-02 18:53:47 +00006018SDValue X86TargetLowering::LowerATOMIC_BINARY_64(SDValue Op,
6019 SelectionDAG &DAG,
6020 unsigned NewOp) {
6021 SDNode *Node = Op.getNode();
6022 MVT T = Node->getValueType(0);
6023 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6024
6025 SDValue Chain = Node->getOperand(0);
6026 SDValue In1 = Node->getOperand(1);
6027 assert(Node->getOperand(2).getNode()->getOpcode()==ISD::BUILD_PAIR);
6028 SDValue In2L = Node->getOperand(2).getNode()->getOperand(0);
6029 SDValue In2H = Node->getOperand(2).getNode()->getOperand(1);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006030 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6031 // have a MemOperand. Pass the info through as a normal operand.
6032 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6033 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
Dale Johannesenf160d802008-10-02 18:53:47 +00006034 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006035 SDValue Result = DAG.getNode(NewOp, Tys, Ops, 5);
Dale Johannesenf160d802008-10-02 18:53:47 +00006036 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
6037 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
6038 SDValue Vals[2] = { ResultVal, Result.getValue(2) };
6039 return SDValue(DAG.getMergeValues(Vals, 2).getNode(), 0);
6040}
6041
Dale Johannesen9011d872008-09-29 22:25:26 +00006042SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6043 SDNode *Node = Op.getNode();
6044 MVT T = Node->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006045 SDValue negOp = DAG.getNode(ISD::SUB, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006046 DAG.getConstant(0, T), Node->getOperand(2));
6047 return DAG.getAtomic((Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_8 ?
6048 ISD::ATOMIC_LOAD_ADD_8 :
6049 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_16 ?
6050 ISD::ATOMIC_LOAD_ADD_16 :
6051 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_32 ?
6052 ISD::ATOMIC_LOAD_ADD_32 :
6053 ISD::ATOMIC_LOAD_ADD_64),
6054 Node->getOperand(0),
6055 Node->getOperand(1), negOp,
6056 cast<AtomicSDNode>(Node)->getSrcValue(),
6057 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006058}
6059
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006060/// LowerOperation - Provide custom lowering hooks for some operations.
6061///
Dan Gohman8181bd12008-07-27 21:46:04 +00006062SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006063 switch (Op.getOpcode()) {
6064 default: assert(0 && "Should not custom lower this!");
Dale Johannesenf160d802008-10-02 18:53:47 +00006065 case ISD::ATOMIC_CMP_SWAP_8:
6066 case ISD::ATOMIC_CMP_SWAP_16:
6067 case ISD::ATOMIC_CMP_SWAP_32:
Dale Johannesenbc187662008-08-28 02:44:49 +00006068 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006069 case ISD::ATOMIC_LOAD_SUB_8:
6070 case ISD::ATOMIC_LOAD_SUB_16:
Dale Johannesen9011d872008-09-29 22:25:26 +00006071 case ISD::ATOMIC_LOAD_SUB_32: return LowerLOAD_SUB(Op,DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006072 case ISD::ATOMIC_LOAD_SUB_64: return (Subtarget->is64Bit()) ?
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006073 LowerLOAD_SUB(Op,DAG) :
6074 LowerATOMIC_BINARY_64(Op,DAG,
Dale Johannesenf160d802008-10-02 18:53:47 +00006075 X86ISD::ATOMSUB64_DAG);
6076 case ISD::ATOMIC_LOAD_AND_64: return LowerATOMIC_BINARY_64(Op,DAG,
6077 X86ISD::ATOMAND64_DAG);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006078 case ISD::ATOMIC_LOAD_OR_64: return LowerATOMIC_BINARY_64(Op, DAG,
Dale Johannesenf160d802008-10-02 18:53:47 +00006079 X86ISD::ATOMOR64_DAG);
6080 case ISD::ATOMIC_LOAD_XOR_64: return LowerATOMIC_BINARY_64(Op,DAG,
6081 X86ISD::ATOMXOR64_DAG);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006082 case ISD::ATOMIC_LOAD_NAND_64:return LowerATOMIC_BINARY_64(Op,DAG,
Dale Johannesenf160d802008-10-02 18:53:47 +00006083 X86ISD::ATOMNAND64_DAG);
6084 case ISD::ATOMIC_LOAD_ADD_64: return LowerATOMIC_BINARY_64(Op,DAG,
6085 X86ISD::ATOMADD64_DAG);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006086 case ISD::ATOMIC_SWAP_64: return LowerATOMIC_BINARY_64(Op,DAG,
6087 X86ISD::ATOMSWAP64_DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006088 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6089 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6090 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6091 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6092 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6093 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6094 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6095 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006096 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006097 case ISD::SHL_PARTS:
6098 case ISD::SRA_PARTS:
6099 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6100 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
6101 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6102 case ISD::FABS: return LowerFABS(Op, DAG);
6103 case ISD::FNEG: return LowerFNEG(Op, DAG);
6104 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006105 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006106 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006107 case ISD::SELECT: return LowerSELECT(Op, DAG);
6108 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006109 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6110 case ISD::CALL: return LowerCALL(Op, DAG);
6111 case ISD::RET: return LowerRET(Op, DAG);
6112 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006113 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006114 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006115 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6116 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6117 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6118 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6119 case ISD::FRAME_TO_ARGS_OFFSET:
6120 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6121 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6122 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006123 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006124 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006125 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6126 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006127
6128 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6129 case ISD::READCYCLECOUNTER:
Gabor Greif1c80d112008-08-28 21:40:38 +00006130 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006131 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006132}
6133
Duncan Sandsac496a12008-07-04 11:47:58 +00006134/// ReplaceNodeResults - Replace a node with an illegal result type
6135/// with a new node built out of custom code.
6136SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006137 switch (N->getOpcode()) {
6138 default: assert(0 && "Should not custom lower this!");
6139 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6140 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesenbc187662008-08-28 02:44:49 +00006141 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006142 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006143}
6144
6145const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6146 switch (Opcode) {
6147 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006148 case X86ISD::BSF: return "X86ISD::BSF";
6149 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006150 case X86ISD::SHLD: return "X86ISD::SHLD";
6151 case X86ISD::SHRD: return "X86ISD::SHRD";
6152 case X86ISD::FAND: return "X86ISD::FAND";
6153 case X86ISD::FOR: return "X86ISD::FOR";
6154 case X86ISD::FXOR: return "X86ISD::FXOR";
6155 case X86ISD::FSRL: return "X86ISD::FSRL";
6156 case X86ISD::FILD: return "X86ISD::FILD";
6157 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6158 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6159 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6160 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6161 case X86ISD::FLD: return "X86ISD::FLD";
6162 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006163 case X86ISD::CALL: return "X86ISD::CALL";
6164 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6165 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6166 case X86ISD::CMP: return "X86ISD::CMP";
6167 case X86ISD::COMI: return "X86ISD::COMI";
6168 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6169 case X86ISD::SETCC: return "X86ISD::SETCC";
6170 case X86ISD::CMOV: return "X86ISD::CMOV";
6171 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6172 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6173 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6174 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006175 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6176 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006177 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006178 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006179 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6180 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006181 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6182 case X86ISD::FMAX: return "X86ISD::FMAX";
6183 case X86ISD::FMIN: return "X86ISD::FMIN";
6184 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6185 case X86ISD::FRCP: return "X86ISD::FRCP";
6186 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6187 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6188 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006189 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006190 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006191 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6192 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006193 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6194 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6195 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6196 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6197 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6198 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006199 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6200 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006201 case X86ISD::VSHL: return "X86ISD::VSHL";
6202 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006203 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6204 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6205 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6206 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6207 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6208 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6209 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6210 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6211 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6212 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006213 }
6214}
6215
6216// isLegalAddressingMode - Return true if the addressing mode represented
6217// by AM is legal for this target, for a load/store of the specified type.
6218bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6219 const Type *Ty) const {
6220 // X86 supports extremely general addressing modes.
6221
6222 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6223 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6224 return false;
6225
6226 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006227 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006228 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6229 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006230
6231 // X86-64 only supports addr of globals in small code model.
6232 if (Subtarget->is64Bit()) {
6233 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6234 return false;
6235 // If lower 4G is not available, then we must use rip-relative addressing.
6236 if (AM.BaseOffs || AM.Scale > 1)
6237 return false;
6238 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006239 }
6240
6241 switch (AM.Scale) {
6242 case 0:
6243 case 1:
6244 case 2:
6245 case 4:
6246 case 8:
6247 // These scales always work.
6248 break;
6249 case 3:
6250 case 5:
6251 case 9:
6252 // These scales are formed with basereg+scalereg. Only accept if there is
6253 // no basereg yet.
6254 if (AM.HasBaseReg)
6255 return false;
6256 break;
6257 default: // Other stuff never works.
6258 return false;
6259 }
6260
6261 return true;
6262}
6263
6264
Evan Cheng27a820a2007-10-26 01:56:11 +00006265bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6266 if (!Ty1->isInteger() || !Ty2->isInteger())
6267 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006268 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6269 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006270 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006271 return false;
6272 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006273}
6274
Duncan Sands92c43912008-06-06 12:08:01 +00006275bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6276 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006277 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006278 unsigned NumBits1 = VT1.getSizeInBits();
6279 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006280 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006281 return false;
6282 return Subtarget->is64Bit() || NumBits1 < 64;
6283}
Evan Cheng27a820a2007-10-26 01:56:11 +00006284
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006285/// isShuffleMaskLegal - Targets can use this to indicate that they only
6286/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6287/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6288/// are assumed to be legal.
6289bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006290X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006291 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006292 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006293 return (Mask.getNode()->getNumOperands() <= 4 ||
6294 isIdentityMask(Mask.getNode()) ||
6295 isIdentityMask(Mask.getNode(), true) ||
6296 isSplatMask(Mask.getNode()) ||
6297 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6298 X86::isUNPCKLMask(Mask.getNode()) ||
6299 X86::isUNPCKHMask(Mask.getNode()) ||
6300 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6301 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006302}
6303
Dan Gohman48d5f062008-04-09 20:09:42 +00006304bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006305X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006306 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006307 unsigned NumElts = BVOps.size();
6308 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006309 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006310 if (NumElts == 2) return true;
6311 if (NumElts == 4) {
6312 return (isMOVLMask(&BVOps[0], 4) ||
6313 isCommutedMOVL(&BVOps[0], 4, true) ||
6314 isSHUFPMask(&BVOps[0], 4) ||
6315 isCommutedSHUFP(&BVOps[0], 4));
6316 }
6317 return false;
6318}
6319
6320//===----------------------------------------------------------------------===//
6321// X86 Scheduler Hooks
6322//===----------------------------------------------------------------------===//
6323
Mon P Wang078a62d2008-05-05 19:05:59 +00006324// private utility function
6325MachineBasicBlock *
6326X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6327 MachineBasicBlock *MBB,
6328 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006329 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006330 unsigned LoadOpc,
6331 unsigned CXchgOpc,
6332 unsigned copyOpc,
6333 unsigned notOpc,
6334 unsigned EAXreg,
6335 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006336 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006337 // For the atomic bitwise operator, we generate
6338 // thisMBB:
6339 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006340 // ld t1 = [bitinstr.addr]
6341 // op t2 = t1, [bitinstr.val]
6342 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006343 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6344 // bz newMBB
6345 // fallthrough -->nextMBB
6346 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6347 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006348 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006349 ++MBBIter;
6350
6351 /// First build the CFG
6352 MachineFunction *F = MBB->getParent();
6353 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006354 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6355 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6356 F->insert(MBBIter, newMBB);
6357 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006358
6359 // Move all successors to thisMBB to nextMBB
6360 nextMBB->transferSuccessors(thisMBB);
6361
6362 // Update thisMBB to fall through to newMBB
6363 thisMBB->addSuccessor(newMBB);
6364
6365 // newMBB jumps to itself and fall through to nextMBB
6366 newMBB->addSuccessor(nextMBB);
6367 newMBB->addSuccessor(newMBB);
6368
6369 // Insert instructions into newMBB based on incoming instruction
6370 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6371 MachineOperand& destOper = bInstr->getOperand(0);
6372 MachineOperand* argOpers[6];
6373 int numArgs = bInstr->getNumOperands() - 1;
6374 for (int i=0; i < numArgs; ++i)
6375 argOpers[i] = &bInstr->getOperand(i+1);
6376
6377 // x86 address has 4 operands: base, index, scale, and displacement
6378 int lastAddrIndx = 3; // [0,3]
6379 int valArgIndx = 4;
6380
Dale Johannesend20e4452008-08-19 18:47:28 +00006381 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6382 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006383 for (int i=0; i <= lastAddrIndx; ++i)
6384 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006385
Dale Johannesend20e4452008-08-19 18:47:28 +00006386 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006387 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006388 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006389 }
6390 else
6391 tt = t1;
6392
Dale Johannesend20e4452008-08-19 18:47:28 +00006393 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006394 assert((argOpers[valArgIndx]->isReg() ||
6395 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006396 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006397 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006398 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6399 else
6400 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006401 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006402 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006403
Dale Johannesend20e4452008-08-19 18:47:28 +00006404 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006405 MIB.addReg(t1);
6406
Dale Johannesend20e4452008-08-19 18:47:28 +00006407 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006408 for (int i=0; i <= lastAddrIndx; ++i)
6409 (*MIB).addOperand(*argOpers[i]);
6410 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006411 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6412 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6413
Dale Johannesend20e4452008-08-19 18:47:28 +00006414 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6415 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006416
6417 // insert branch
6418 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6419
Dan Gohman221a4372008-07-07 23:14:23 +00006420 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006421 return nextMBB;
6422}
6423
Dale Johannesen44eb5372008-10-03 19:41:08 +00006424// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00006425MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00006426X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
6427 MachineBasicBlock *MBB,
6428 unsigned regOpcL,
6429 unsigned regOpcH,
6430 unsigned immOpcL,
6431 unsigned immOpcH,
6432 bool invSrc) {
6433 // For the atomic bitwise operator, we generate
6434 // thisMBB (instructions are in pairs, except cmpxchg8b)
6435 // ld t1,t2 = [bitinstr.addr]
6436 // newMBB:
6437 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
6438 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006439 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00006440 // mov ECX, EBX <- t5, t6
6441 // mov EAX, EDX <- t1, t2
6442 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
6443 // mov t3, t4 <- EAX, EDX
6444 // bz newMBB
6445 // result in out1, out2
6446 // fallthrough -->nextMBB
6447
6448 const TargetRegisterClass *RC = X86::GR32RegisterClass;
6449 const unsigned LoadOpc = X86::MOV32rm;
6450 const unsigned copyOpc = X86::MOV32rr;
6451 const unsigned NotOpc = X86::NOT32r;
6452 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6453 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6454 MachineFunction::iterator MBBIter = MBB;
6455 ++MBBIter;
6456
6457 /// First build the CFG
6458 MachineFunction *F = MBB->getParent();
6459 MachineBasicBlock *thisMBB = MBB;
6460 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6461 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6462 F->insert(MBBIter, newMBB);
6463 F->insert(MBBIter, nextMBB);
6464
6465 // Move all successors to thisMBB to nextMBB
6466 nextMBB->transferSuccessors(thisMBB);
6467
6468 // Update thisMBB to fall through to newMBB
6469 thisMBB->addSuccessor(newMBB);
6470
6471 // newMBB jumps to itself and fall through to nextMBB
6472 newMBB->addSuccessor(nextMBB);
6473 newMBB->addSuccessor(newMBB);
6474
6475 // Insert instructions into newMBB based on incoming instruction
6476 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
6477 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
6478 MachineOperand& dest1Oper = bInstr->getOperand(0);
6479 MachineOperand& dest2Oper = bInstr->getOperand(1);
6480 MachineOperand* argOpers[6];
6481 for (int i=0; i < 6; ++i)
6482 argOpers[i] = &bInstr->getOperand(i+2);
6483
6484 // x86 address has 4 operands: base, index, scale, and displacement
6485 int lastAddrIndx = 3; // [0,3]
6486
6487 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6488 MachineInstrBuilder MIB = BuildMI(thisMBB, TII->get(LoadOpc), t1);
6489 for (int i=0; i <= lastAddrIndx; ++i)
6490 (*MIB).addOperand(*argOpers[i]);
6491 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
6492 MIB = BuildMI(thisMBB, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006493 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00006494 for (int i=0; i <= lastAddrIndx-1; ++i)
6495 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006496 MachineOperand newOp3 = *(argOpers[3]);
6497 if (newOp3.isImm())
6498 newOp3.setImm(newOp3.getImm()+4);
6499 else
6500 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00006501 (*MIB).addOperand(newOp3);
6502
6503 // t3/4 are defined later, at the bottom of the loop
6504 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
6505 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
6506 BuildMI(newMBB, TII->get(X86::PHI), dest1Oper.getReg())
6507 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
6508 BuildMI(newMBB, TII->get(X86::PHI), dest2Oper.getReg())
6509 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
6510
6511 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
6512 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
6513 if (invSrc) {
6514 MIB = BuildMI(newMBB, TII->get(NotOpc), tt1).addReg(t1);
6515 MIB = BuildMI(newMBB, TII->get(NotOpc), tt2).addReg(t2);
6516 } else {
6517 tt1 = t1;
6518 tt2 = t2;
6519 }
6520
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006521 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00006522 "invalid operand");
6523 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
6524 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006525 if (argOpers[4]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006526 MIB = BuildMI(newMBB, TII->get(regOpcL), t5);
6527 else
6528 MIB = BuildMI(newMBB, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006529 if (regOpcL != X86::MOV32rr)
6530 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00006531 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006532 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
6533 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
6534 if (argOpers[5]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006535 MIB = BuildMI(newMBB, TII->get(regOpcH), t6);
6536 else
6537 MIB = BuildMI(newMBB, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006538 if (regOpcH != X86::MOV32rr)
6539 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006540 (*MIB).addOperand(*argOpers[5]);
6541
6542 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EAX);
6543 MIB.addReg(t1);
6544 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EDX);
6545 MIB.addReg(t2);
6546
6547 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EBX);
6548 MIB.addReg(t5);
6549 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::ECX);
6550 MIB.addReg(t6);
6551
6552 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG8B));
6553 for (int i=0; i <= lastAddrIndx; ++i)
6554 (*MIB).addOperand(*argOpers[i]);
6555
6556 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6557 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6558
6559 MIB = BuildMI(newMBB, TII->get(copyOpc), t3);
6560 MIB.addReg(X86::EAX);
6561 MIB = BuildMI(newMBB, TII->get(copyOpc), t4);
6562 MIB.addReg(X86::EDX);
6563
6564 // insert branch
6565 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6566
6567 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
6568 return nextMBB;
6569}
6570
6571// private utility function
6572MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00006573X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6574 MachineBasicBlock *MBB,
6575 unsigned cmovOpc) {
6576 // For the atomic min/max operator, we generate
6577 // thisMBB:
6578 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006579 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006580 // mov t2 = [min/max.val]
6581 // cmp t1, t2
6582 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006583 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006584 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6585 // bz newMBB
6586 // fallthrough -->nextMBB
6587 //
6588 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6589 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006590 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006591 ++MBBIter;
6592
6593 /// First build the CFG
6594 MachineFunction *F = MBB->getParent();
6595 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006596 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6597 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6598 F->insert(MBBIter, newMBB);
6599 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006600
6601 // Move all successors to thisMBB to nextMBB
6602 nextMBB->transferSuccessors(thisMBB);
6603
6604 // Update thisMBB to fall through to newMBB
6605 thisMBB->addSuccessor(newMBB);
6606
6607 // newMBB jumps to newMBB and fall through to nextMBB
6608 newMBB->addSuccessor(nextMBB);
6609 newMBB->addSuccessor(newMBB);
6610
6611 // Insert instructions into newMBB based on incoming instruction
6612 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6613 MachineOperand& destOper = mInstr->getOperand(0);
6614 MachineOperand* argOpers[6];
6615 int numArgs = mInstr->getNumOperands() - 1;
6616 for (int i=0; i < numArgs; ++i)
6617 argOpers[i] = &mInstr->getOperand(i+1);
6618
6619 // x86 address has 4 operands: base, index, scale, and displacement
6620 int lastAddrIndx = 3; // [0,3]
6621 int valArgIndx = 4;
6622
Mon P Wang318b0372008-05-05 22:56:23 +00006623 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6624 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006625 for (int i=0; i <= lastAddrIndx; ++i)
6626 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006627
Mon P Wang078a62d2008-05-05 19:05:59 +00006628 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006629 assert((argOpers[valArgIndx]->isReg() ||
6630 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006631 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00006632
6633 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006634 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006635 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6636 else
6637 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6638 (*MIB).addOperand(*argOpers[valArgIndx]);
6639
Mon P Wang318b0372008-05-05 22:56:23 +00006640 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6641 MIB.addReg(t1);
6642
Mon P Wang078a62d2008-05-05 19:05:59 +00006643 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6644 MIB.addReg(t1);
6645 MIB.addReg(t2);
6646
6647 // Generate movc
6648 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6649 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6650 MIB.addReg(t2);
6651 MIB.addReg(t1);
6652
6653 // Cmp and exchange if none has modified the memory location
6654 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6655 for (int i=0; i <= lastAddrIndx; ++i)
6656 (*MIB).addOperand(*argOpers[i]);
6657 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006658 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6659 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006660
6661 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6662 MIB.addReg(X86::EAX);
6663
6664 // insert branch
6665 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6666
Dan Gohman221a4372008-07-07 23:14:23 +00006667 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006668 return nextMBB;
6669}
6670
6671
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006672MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006673X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6674 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006675 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6676 switch (MI->getOpcode()) {
6677 default: assert(false && "Unexpected instr type to insert");
6678 case X86::CMOV_FR32:
6679 case X86::CMOV_FR64:
6680 case X86::CMOV_V4F32:
6681 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006682 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006683 // To "insert" a SELECT_CC instruction, we actually have to insert the
6684 // diamond control-flow pattern. The incoming instruction knows the
6685 // destination vreg to set, the condition code register to branch on, the
6686 // true/false values to select between, and a branch opcode to use.
6687 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006688 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006689 ++It;
6690
6691 // thisMBB:
6692 // ...
6693 // TrueVal = ...
6694 // cmpTY ccX, r1, r2
6695 // bCC copy1MBB
6696 // fallthrough --> copy0MBB
6697 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006698 MachineFunction *F = BB->getParent();
6699 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6700 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006701 unsigned Opc =
6702 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6703 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006704 F->insert(It, copy0MBB);
6705 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006706 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006707 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006708 sinkMBB->transferSuccessors(BB);
6709
6710 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006711 BB->addSuccessor(copy0MBB);
6712 BB->addSuccessor(sinkMBB);
6713
6714 // copy0MBB:
6715 // %FalseValue = ...
6716 // # fallthrough to sinkMBB
6717 BB = copy0MBB;
6718
6719 // Update machine-CFG edges
6720 BB->addSuccessor(sinkMBB);
6721
6722 // sinkMBB:
6723 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6724 // ...
6725 BB = sinkMBB;
6726 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6727 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6728 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6729
Dan Gohman221a4372008-07-07 23:14:23 +00006730 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006731 return BB;
6732 }
6733
6734 case X86::FP32_TO_INT16_IN_MEM:
6735 case X86::FP32_TO_INT32_IN_MEM:
6736 case X86::FP32_TO_INT64_IN_MEM:
6737 case X86::FP64_TO_INT16_IN_MEM:
6738 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006739 case X86::FP64_TO_INT64_IN_MEM:
6740 case X86::FP80_TO_INT16_IN_MEM:
6741 case X86::FP80_TO_INT32_IN_MEM:
6742 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006743 // Change the floating point control register to use "round towards zero"
6744 // mode when truncating to an integer value.
6745 MachineFunction *F = BB->getParent();
6746 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6747 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6748
6749 // Load the old value of the high byte of the control word...
6750 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006751 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006752 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6753
6754 // Set the high part to be round to zero...
6755 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6756 .addImm(0xC7F);
6757
6758 // Reload the modified control word now...
6759 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6760
6761 // Restore the memory image of control word to original value
6762 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6763 .addReg(OldCW);
6764
6765 // Get the X86 opcode to use.
6766 unsigned Opc;
6767 switch (MI->getOpcode()) {
6768 default: assert(0 && "illegal opcode!");
6769 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6770 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6771 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6772 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6773 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6774 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006775 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6776 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6777 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006778 }
6779
6780 X86AddressMode AM;
6781 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006782 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006783 AM.BaseType = X86AddressMode::RegBase;
6784 AM.Base.Reg = Op.getReg();
6785 } else {
6786 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006787 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006788 }
6789 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006790 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006791 AM.Scale = Op.getImm();
6792 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006793 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006794 AM.IndexReg = Op.getImm();
6795 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006796 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006797 AM.GV = Op.getGlobal();
6798 } else {
6799 AM.Disp = Op.getImm();
6800 }
6801 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6802 .addReg(MI->getOperand(4).getReg());
6803
6804 // Reload the original control word now.
6805 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6806
Dan Gohman221a4372008-07-07 23:14:23 +00006807 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006808 return BB;
6809 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006810 case X86::ATOMAND32:
6811 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006812 X86::AND32ri, X86::MOV32rm,
6813 X86::LCMPXCHG32, X86::MOV32rr,
6814 X86::NOT32r, X86::EAX,
6815 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006816 case X86::ATOMOR32:
6817 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006818 X86::OR32ri, X86::MOV32rm,
6819 X86::LCMPXCHG32, X86::MOV32rr,
6820 X86::NOT32r, X86::EAX,
6821 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006822 case X86::ATOMXOR32:
6823 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006824 X86::XOR32ri, X86::MOV32rm,
6825 X86::LCMPXCHG32, X86::MOV32rr,
6826 X86::NOT32r, X86::EAX,
6827 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006828 case X86::ATOMNAND32:
6829 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006830 X86::AND32ri, X86::MOV32rm,
6831 X86::LCMPXCHG32, X86::MOV32rr,
6832 X86::NOT32r, X86::EAX,
6833 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006834 case X86::ATOMMIN32:
6835 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6836 case X86::ATOMMAX32:
6837 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6838 case X86::ATOMUMIN32:
6839 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6840 case X86::ATOMUMAX32:
6841 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006842
6843 case X86::ATOMAND16:
6844 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6845 X86::AND16ri, X86::MOV16rm,
6846 X86::LCMPXCHG16, X86::MOV16rr,
6847 X86::NOT16r, X86::AX,
6848 X86::GR16RegisterClass);
6849 case X86::ATOMOR16:
6850 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6851 X86::OR16ri, X86::MOV16rm,
6852 X86::LCMPXCHG16, X86::MOV16rr,
6853 X86::NOT16r, X86::AX,
6854 X86::GR16RegisterClass);
6855 case X86::ATOMXOR16:
6856 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6857 X86::XOR16ri, X86::MOV16rm,
6858 X86::LCMPXCHG16, X86::MOV16rr,
6859 X86::NOT16r, X86::AX,
6860 X86::GR16RegisterClass);
6861 case X86::ATOMNAND16:
6862 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6863 X86::AND16ri, X86::MOV16rm,
6864 X86::LCMPXCHG16, X86::MOV16rr,
6865 X86::NOT16r, X86::AX,
6866 X86::GR16RegisterClass, true);
6867 case X86::ATOMMIN16:
6868 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6869 case X86::ATOMMAX16:
6870 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6871 case X86::ATOMUMIN16:
6872 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6873 case X86::ATOMUMAX16:
6874 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6875
6876 case X86::ATOMAND8:
6877 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6878 X86::AND8ri, X86::MOV8rm,
6879 X86::LCMPXCHG8, X86::MOV8rr,
6880 X86::NOT8r, X86::AL,
6881 X86::GR8RegisterClass);
6882 case X86::ATOMOR8:
6883 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6884 X86::OR8ri, X86::MOV8rm,
6885 X86::LCMPXCHG8, X86::MOV8rr,
6886 X86::NOT8r, X86::AL,
6887 X86::GR8RegisterClass);
6888 case X86::ATOMXOR8:
6889 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6890 X86::XOR8ri, X86::MOV8rm,
6891 X86::LCMPXCHG8, X86::MOV8rr,
6892 X86::NOT8r, X86::AL,
6893 X86::GR8RegisterClass);
6894 case X86::ATOMNAND8:
6895 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6896 X86::AND8ri, X86::MOV8rm,
6897 X86::LCMPXCHG8, X86::MOV8rr,
6898 X86::NOT8r, X86::AL,
6899 X86::GR8RegisterClass, true);
6900 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00006901 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006902 case X86::ATOMAND64:
6903 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6904 X86::AND64ri32, X86::MOV64rm,
6905 X86::LCMPXCHG64, X86::MOV64rr,
6906 X86::NOT64r, X86::RAX,
6907 X86::GR64RegisterClass);
6908 case X86::ATOMOR64:
6909 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6910 X86::OR64ri32, X86::MOV64rm,
6911 X86::LCMPXCHG64, X86::MOV64rr,
6912 X86::NOT64r, X86::RAX,
6913 X86::GR64RegisterClass);
6914 case X86::ATOMXOR64:
6915 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6916 X86::XOR64ri32, X86::MOV64rm,
6917 X86::LCMPXCHG64, X86::MOV64rr,
6918 X86::NOT64r, X86::RAX,
6919 X86::GR64RegisterClass);
6920 case X86::ATOMNAND64:
6921 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6922 X86::AND64ri32, X86::MOV64rm,
6923 X86::LCMPXCHG64, X86::MOV64rr,
6924 X86::NOT64r, X86::RAX,
6925 X86::GR64RegisterClass, true);
6926 case X86::ATOMMIN64:
6927 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6928 case X86::ATOMMAX64:
6929 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6930 case X86::ATOMUMIN64:
6931 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6932 case X86::ATOMUMAX64:
6933 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00006934
6935 // This group does 64-bit operations on a 32-bit host.
6936 case X86::ATOMAND6432:
6937 return EmitAtomicBit6432WithCustomInserter(MI, BB,
6938 X86::AND32rr, X86::AND32rr,
6939 X86::AND32ri, X86::AND32ri,
6940 false);
6941 case X86::ATOMOR6432:
6942 return EmitAtomicBit6432WithCustomInserter(MI, BB,
6943 X86::OR32rr, X86::OR32rr,
6944 X86::OR32ri, X86::OR32ri,
6945 false);
6946 case X86::ATOMXOR6432:
6947 return EmitAtomicBit6432WithCustomInserter(MI, BB,
6948 X86::XOR32rr, X86::XOR32rr,
6949 X86::XOR32ri, X86::XOR32ri,
6950 false);
6951 case X86::ATOMNAND6432:
6952 return EmitAtomicBit6432WithCustomInserter(MI, BB,
6953 X86::AND32rr, X86::AND32rr,
6954 X86::AND32ri, X86::AND32ri,
6955 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00006956 case X86::ATOMADD6432:
6957 return EmitAtomicBit6432WithCustomInserter(MI, BB,
6958 X86::ADD32rr, X86::ADC32rr,
6959 X86::ADD32ri, X86::ADC32ri,
6960 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00006961 case X86::ATOMSUB6432:
6962 return EmitAtomicBit6432WithCustomInserter(MI, BB,
6963 X86::SUB32rr, X86::SBB32rr,
6964 X86::SUB32ri, X86::SBB32ri,
6965 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006966 case X86::ATOMSWAP6432:
6967 return EmitAtomicBit6432WithCustomInserter(MI, BB,
6968 X86::MOV32rr, X86::MOV32rr,
6969 X86::MOV32ri, X86::MOV32ri,
6970 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006971 }
6972}
6973
6974//===----------------------------------------------------------------------===//
6975// X86 Optimization Hooks
6976//===----------------------------------------------------------------------===//
6977
Dan Gohman8181bd12008-07-27 21:46:04 +00006978void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006979 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006980 APInt &KnownZero,
6981 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006982 const SelectionDAG &DAG,
6983 unsigned Depth) const {
6984 unsigned Opc = Op.getOpcode();
6985 assert((Opc >= ISD::BUILTIN_OP_END ||
6986 Opc == ISD::INTRINSIC_WO_CHAIN ||
6987 Opc == ISD::INTRINSIC_W_CHAIN ||
6988 Opc == ISD::INTRINSIC_VOID) &&
6989 "Should use MaskedValueIsZero if you don't know whether Op"
6990 " is a target node!");
6991
Dan Gohman1d79e432008-02-13 23:07:24 +00006992 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006993 switch (Opc) {
6994 default: break;
6995 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006996 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6997 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006998 break;
6999 }
7000}
7001
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007002/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007003/// node is a GlobalAddress + offset.
7004bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7005 GlobalValue* &GA, int64_t &Offset) const{
7006 if (N->getOpcode() == X86ISD::Wrapper) {
7007 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007008 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
7009 return true;
7010 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007011 }
Evan Chengef7be082008-05-12 19:56:52 +00007012 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007013}
7014
Evan Chengef7be082008-05-12 19:56:52 +00007015static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7016 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007017 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007018 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007019 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007020 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007021 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007022 return false;
7023}
7024
Dan Gohman8181bd12008-07-27 21:46:04 +00007025static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007026 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007027 SDNode *&Base,
7028 SelectionDAG &DAG, MachineFrameInfo *MFI,
7029 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007030 Base = NULL;
7031 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007032 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007033 if (Idx.getOpcode() == ISD::UNDEF) {
7034 if (!Base)
7035 return false;
7036 continue;
7037 }
7038
Dan Gohman8181bd12008-07-27 21:46:04 +00007039 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007040 if (!Elt.getNode() ||
7041 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007042 return false;
7043 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007044 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007045 if (Base->getOpcode() == ISD::UNDEF)
7046 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007047 continue;
7048 }
7049 if (Elt.getOpcode() == ISD::UNDEF)
7050 continue;
7051
Gabor Greif1c80d112008-08-28 21:40:38 +00007052 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007053 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007054 return false;
7055 }
7056 return true;
7057}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007058
7059/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7060/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7061/// if the load addresses are consecutive, non-overlapping, and in the right
7062/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007063static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007064 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007065 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00007066 MVT VT = N->getValueType(0);
7067 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007068 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007069 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007070 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007071 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7072 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007073 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007074
Dan Gohman11821702007-07-27 17:16:43 +00007075 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007076 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007077 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00007078 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00007079 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
7080 LD->getSrcValueOffset(), LD->isVolatile(),
7081 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007082}
7083
Evan Chengb6290462008-05-12 23:04:07 +00007084/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007085static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng6617eed2008-09-24 23:26:36 +00007086 const X86Subtarget *Subtarget,
7087 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007088 unsigned NumOps = N->getNumOperands();
7089
Evan Chenge9b9c672008-05-09 21:53:03 +00007090 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007091 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007092 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007093
Duncan Sands92c43912008-06-06 12:08:01 +00007094 MVT VT = N->getValueType(0);
7095 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007096 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7097 // We are looking for load i64 and zero extend. We want to transform
7098 // it before legalizer has a chance to expand it. Also look for i64
7099 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007100 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007101 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007102 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007103 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007104 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007105
7106 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007107 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007108 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007109 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007110 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007111 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007112 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007113 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007114 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007115
7116 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007117 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007118
7119 // Load must not be an extload.
7120 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007121 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00007122
Evan Cheng6617eed2008-09-24 23:26:36 +00007123 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7124 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7125 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
7126 DAG.ReplaceAllUsesOfValueWith(SDValue(Base, 1), ResNode.getValue(1));
7127 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007128}
7129
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007130/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007131static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007132 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007133 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007134
7135 // If we have SSE[12] support, try to form min/max nodes.
7136 if (Subtarget->hasSSE2() &&
7137 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7138 if (Cond.getOpcode() == ISD::SETCC) {
7139 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007140 SDValue LHS = N->getOperand(1);
7141 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007142 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7143
7144 unsigned Opcode = 0;
7145 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7146 switch (CC) {
7147 default: break;
7148 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7149 case ISD::SETULE:
7150 case ISD::SETLE:
7151 if (!UnsafeFPMath) break;
7152 // FALL THROUGH.
7153 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7154 case ISD::SETLT:
7155 Opcode = X86ISD::FMIN;
7156 break;
7157
7158 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7159 case ISD::SETUGT:
7160 case ISD::SETGT:
7161 if (!UnsafeFPMath) break;
7162 // FALL THROUGH.
7163 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7164 case ISD::SETGE:
7165 Opcode = X86ISD::FMAX;
7166 break;
7167 }
7168 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7169 switch (CC) {
7170 default: break;
7171 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7172 case ISD::SETUGT:
7173 case ISD::SETGT:
7174 if (!UnsafeFPMath) break;
7175 // FALL THROUGH.
7176 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7177 case ISD::SETGE:
7178 Opcode = X86ISD::FMIN;
7179 break;
7180
7181 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7182 case ISD::SETULE:
7183 case ISD::SETLE:
7184 if (!UnsafeFPMath) break;
7185 // FALL THROUGH.
7186 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7187 case ISD::SETLT:
7188 Opcode = X86ISD::FMAX;
7189 break;
7190 }
7191 }
7192
7193 if (Opcode)
7194 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
7195 }
7196
7197 }
7198
Dan Gohman8181bd12008-07-27 21:46:04 +00007199 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007200}
7201
Chris Lattnerce84ae42008-02-22 02:09:43 +00007202/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007203static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007204 const X86Subtarget *Subtarget) {
7205 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7206 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007207 // A preferable solution to the general problem is to figure out the right
7208 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007209 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007210 if (St->getValue().getValueType().isVector() &&
7211 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007212 isa<LoadSDNode>(St->getValue()) &&
7213 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7214 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007215 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007216 LoadSDNode *Ld = 0;
7217 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007218 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007219 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007220 // Must be a store of a load. We currently handle two cases: the load
7221 // is a direct child, and it's under an intervening TokenFactor. It is
7222 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007223 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007224 Ld = cast<LoadSDNode>(St->getChain());
7225 else if (St->getValue().hasOneUse() &&
7226 ChainVal->getOpcode() == ISD::TokenFactor) {
7227 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007228 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007229 TokenFactorIndex = i;
7230 Ld = cast<LoadSDNode>(St->getValue());
7231 } else
7232 Ops.push_back(ChainVal->getOperand(i));
7233 }
7234 }
7235 if (Ld) {
7236 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7237 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007238 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007239 Ld->getBasePtr(), Ld->getSrcValue(),
7240 Ld->getSrcValueOffset(), Ld->isVolatile(),
7241 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007242 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007243 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00007244 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00007245 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7246 Ops.size());
7247 }
7248 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
7249 St->getSrcValue(), St->getSrcValueOffset(),
7250 St->isVolatile(), St->getAlignment());
7251 }
7252
7253 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00007254 SDValue LoAddr = Ld->getBasePtr();
7255 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007256 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007257
Dan Gohman8181bd12008-07-27 21:46:04 +00007258 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007259 Ld->getSrcValue(), Ld->getSrcValueOffset(),
7260 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007261 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007262 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
7263 Ld->isVolatile(),
7264 MinAlign(Ld->getAlignment(), 4));
7265
Dan Gohman8181bd12008-07-27 21:46:04 +00007266 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007267 if (TokenFactorIndex != -1) {
7268 Ops.push_back(LoLd);
7269 Ops.push_back(HiLd);
7270 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7271 Ops.size());
7272 }
7273
7274 LoAddr = St->getBasePtr();
7275 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007276 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007277
Dan Gohman8181bd12008-07-27 21:46:04 +00007278 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007279 St->getSrcValue(), St->getSrcValueOffset(),
7280 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007281 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00007282 St->getSrcValue(),
7283 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00007284 St->isVolatile(),
7285 MinAlign(St->getAlignment(), 4));
7286 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00007287 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00007288 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007289 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00007290}
7291
Chris Lattner470d5dc2008-01-25 06:14:17 +00007292/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7293/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007294static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00007295 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7296 // F[X]OR(0.0, x) -> x
7297 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00007298 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7299 if (C->getValueAPF().isPosZero())
7300 return N->getOperand(1);
7301 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7302 if (C->getValueAPF().isPosZero())
7303 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007304 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007305}
7306
7307/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007308static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007309 // FAND(0.0, x) -> 0.0
7310 // FAND(x, 0.0) -> 0.0
7311 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7312 if (C->getValueAPF().isPosZero())
7313 return N->getOperand(0);
7314 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7315 if (C->getValueAPF().isPosZero())
7316 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007317 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007318}
7319
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007320
Dan Gohman8181bd12008-07-27 21:46:04 +00007321SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007322 DAGCombinerInfo &DCI) const {
7323 SelectionDAG &DAG = DCI.DAG;
7324 switch (N->getOpcode()) {
7325 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007326 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7327 case ISD::BUILD_VECTOR:
7328 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007329 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007330 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007331 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007332 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7333 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007334 }
7335
Dan Gohman8181bd12008-07-27 21:46:04 +00007336 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007337}
7338
7339//===----------------------------------------------------------------------===//
7340// X86 Inline Assembly Support
7341//===----------------------------------------------------------------------===//
7342
7343/// getConstraintType - Given a constraint letter, return the type of
7344/// constraint it is for this target.
7345X86TargetLowering::ConstraintType
7346X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7347 if (Constraint.size() == 1) {
7348 switch (Constraint[0]) {
7349 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00007350 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007351 case 'r':
7352 case 'R':
7353 case 'l':
7354 case 'q':
7355 case 'Q':
7356 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007357 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007358 case 'Y':
7359 return C_RegisterClass;
7360 default:
7361 break;
7362 }
7363 }
7364 return TargetLowering::getConstraintType(Constraint);
7365}
7366
Dale Johannesene99fc902008-01-29 02:21:21 +00007367/// LowerXConstraint - try to replace an X constraint, which matches anything,
7368/// with another that has more specific requirements based on the type of the
7369/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007370const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007371LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007372 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7373 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007374 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007375 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007376 return "Y";
7377 if (Subtarget->hasSSE1())
7378 return "x";
7379 }
7380
7381 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007382}
7383
Chris Lattnera531abc2007-08-25 00:47:38 +00007384/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7385/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007386void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007387 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00007388 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00007389 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007390 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007391 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007392
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007393 switch (Constraint) {
7394 default: break;
7395 case 'I':
7396 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007397 if (C->getZExtValue() <= 31) {
7398 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007399 break;
7400 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007401 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007402 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00007403 case 'J':
7404 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
7405 if (C->getZExtValue() <= 63) {
7406 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
7407 break;
7408 }
7409 }
7410 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007411 case 'N':
7412 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007413 if (C->getZExtValue() <= 255) {
7414 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007415 break;
7416 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007417 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007418 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007419 case 'i': {
7420 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007421 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007422 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007423 break;
7424 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007425
7426 // If we are in non-pic codegen mode, we allow the address of a global (with
7427 // an optional displacement) to be used with 'i'.
7428 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7429 int64_t Offset = 0;
7430
7431 // Match either (GA) or (GA+C)
7432 if (GA) {
7433 Offset = GA->getOffset();
7434 } else if (Op.getOpcode() == ISD::ADD) {
7435 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7436 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7437 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007438 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007439 } else {
7440 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7441 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7442 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007443 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007444 else
7445 C = 0, GA = 0;
7446 }
7447 }
7448
7449 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00007450 if (hasMemory)
7451 Op = LowerGlobalAddress(GA->getGlobal(), DAG);
7452 else
7453 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7454 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007455 Result = Op;
7456 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007457 }
7458
7459 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007460 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007461 }
7462 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007463
Gabor Greif1c80d112008-08-28 21:40:38 +00007464 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007465 Ops.push_back(Result);
7466 return;
7467 }
Evan Cheng7f250d62008-09-24 00:05:32 +00007468 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
7469 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007470}
7471
7472std::vector<unsigned> X86TargetLowering::
7473getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007474 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007475 if (Constraint.size() == 1) {
7476 // FIXME: not handling fp-stack yet!
7477 switch (Constraint[0]) { // GCC X86 Constraint Letters
7478 default: break; // Unknown constraint letter
7479 case 'A': // EAX/EDX
7480 if (VT == MVT::i32 || VT == MVT::i64)
7481 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7482 break;
7483 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7484 case 'Q': // Q_REGS
7485 if (VT == MVT::i32)
7486 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7487 else if (VT == MVT::i16)
7488 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7489 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007490 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007491 else if (VT == MVT::i64)
7492 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7493 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007494 }
7495 }
7496
7497 return std::vector<unsigned>();
7498}
7499
7500std::pair<unsigned, const TargetRegisterClass*>
7501X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007502 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007503 // First, see if this is a constraint that directly corresponds to an LLVM
7504 // register class.
7505 if (Constraint.size() == 1) {
7506 // GCC Constraint Letters
7507 switch (Constraint[0]) {
7508 default: break;
7509 case 'r': // GENERAL_REGS
7510 case 'R': // LEGACY_REGS
7511 case 'l': // INDEX_REGS
7512 if (VT == MVT::i64 && Subtarget->is64Bit())
7513 return std::make_pair(0U, X86::GR64RegisterClass);
7514 if (VT == MVT::i32)
7515 return std::make_pair(0U, X86::GR32RegisterClass);
7516 else if (VT == MVT::i16)
7517 return std::make_pair(0U, X86::GR16RegisterClass);
7518 else if (VT == MVT::i8)
7519 return std::make_pair(0U, X86::GR8RegisterClass);
7520 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007521 case 'f': // FP Stack registers.
7522 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7523 // value to the correct fpstack register class.
7524 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7525 return std::make_pair(0U, X86::RFP32RegisterClass);
7526 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7527 return std::make_pair(0U, X86::RFP64RegisterClass);
7528 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007529 case 'y': // MMX_REGS if MMX allowed.
7530 if (!Subtarget->hasMMX()) break;
7531 return std::make_pair(0U, X86::VR64RegisterClass);
7532 break;
7533 case 'Y': // SSE_REGS if SSE2 allowed
7534 if (!Subtarget->hasSSE2()) break;
7535 // FALL THROUGH.
7536 case 'x': // SSE_REGS if SSE1 allowed
7537 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007538
7539 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007540 default: break;
7541 // Scalar SSE types.
7542 case MVT::f32:
7543 case MVT::i32:
7544 return std::make_pair(0U, X86::FR32RegisterClass);
7545 case MVT::f64:
7546 case MVT::i64:
7547 return std::make_pair(0U, X86::FR64RegisterClass);
7548 // Vector types.
7549 case MVT::v16i8:
7550 case MVT::v8i16:
7551 case MVT::v4i32:
7552 case MVT::v2i64:
7553 case MVT::v4f32:
7554 case MVT::v2f64:
7555 return std::make_pair(0U, X86::VR128RegisterClass);
7556 }
7557 break;
7558 }
7559 }
7560
7561 // Use the default implementation in TargetLowering to convert the register
7562 // constraint into a member of a register class.
7563 std::pair<unsigned, const TargetRegisterClass*> Res;
7564 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7565
7566 // Not found as a standard register?
7567 if (Res.second == 0) {
7568 // GCC calls "st(0)" just plain "st".
7569 if (StringsEqualNoCase("{st}", Constraint)) {
7570 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007571 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007572 }
7573
7574 return Res;
7575 }
7576
7577 // Otherwise, check to see if this is a register class of the wrong value
7578 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7579 // turn into {ax},{dx}.
7580 if (Res.second->hasType(VT))
7581 return Res; // Correct type already, nothing to do.
7582
7583 // All of the single-register GCC register classes map their values onto
7584 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7585 // really want an 8-bit or 32-bit register, map to the appropriate register
7586 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007587 if (Res.second == X86::GR16RegisterClass) {
7588 if (VT == MVT::i8) {
7589 unsigned DestReg = 0;
7590 switch (Res.first) {
7591 default: break;
7592 case X86::AX: DestReg = X86::AL; break;
7593 case X86::DX: DestReg = X86::DL; break;
7594 case X86::CX: DestReg = X86::CL; break;
7595 case X86::BX: DestReg = X86::BL; break;
7596 }
7597 if (DestReg) {
7598 Res.first = DestReg;
7599 Res.second = Res.second = X86::GR8RegisterClass;
7600 }
7601 } else if (VT == MVT::i32) {
7602 unsigned DestReg = 0;
7603 switch (Res.first) {
7604 default: break;
7605 case X86::AX: DestReg = X86::EAX; break;
7606 case X86::DX: DestReg = X86::EDX; break;
7607 case X86::CX: DestReg = X86::ECX; break;
7608 case X86::BX: DestReg = X86::EBX; break;
7609 case X86::SI: DestReg = X86::ESI; break;
7610 case X86::DI: DestReg = X86::EDI; break;
7611 case X86::BP: DestReg = X86::EBP; break;
7612 case X86::SP: DestReg = X86::ESP; break;
7613 }
7614 if (DestReg) {
7615 Res.first = DestReg;
7616 Res.second = Res.second = X86::GR32RegisterClass;
7617 }
7618 } else if (VT == MVT::i64) {
7619 unsigned DestReg = 0;
7620 switch (Res.first) {
7621 default: break;
7622 case X86::AX: DestReg = X86::RAX; break;
7623 case X86::DX: DestReg = X86::RDX; break;
7624 case X86::CX: DestReg = X86::RCX; break;
7625 case X86::BX: DestReg = X86::RBX; break;
7626 case X86::SI: DestReg = X86::RSI; break;
7627 case X86::DI: DestReg = X86::RDI; break;
7628 case X86::BP: DestReg = X86::RBP; break;
7629 case X86::SP: DestReg = X86::RSP; break;
7630 }
7631 if (DestReg) {
7632 Res.first = DestReg;
7633 Res.second = Res.second = X86::GR64RegisterClass;
7634 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007635 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007636 } else if (Res.second == X86::FR32RegisterClass ||
7637 Res.second == X86::FR64RegisterClass ||
7638 Res.second == X86::VR128RegisterClass) {
7639 // Handle references to XMM physical registers that got mapped into the
7640 // wrong class. This can happen with constraints like {xmm0} where the
7641 // target independent register mapper will just pick the first match it can
7642 // find, ignoring the required type.
7643 if (VT == MVT::f32)
7644 Res.second = X86::FR32RegisterClass;
7645 else if (VT == MVT::f64)
7646 Res.second = X86::FR64RegisterClass;
7647 else if (X86::VR128RegisterClass->hasType(VT))
7648 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007649 }
7650
7651 return Res;
7652}