blob: 1841de2dc874deacf78a1d8eb2d653aee321bbe4 [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
Evan Cheng08c171a2008-10-14 21:26:46 +000087 setLoadExtAction(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 {
Dale Johannesen6e547b42008-10-09 23:00:39 +0000485 bool ignored;
Chris Lattnerdd867392008-01-27 06:19:31 +0000486 APFloat TmpFlt(+0.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000487 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
488 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000489 addLegalFPImmediate(TmpFlt); // FLD0
490 TmpFlt.changeSign();
491 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
492 APFloat TmpFlt2(+1.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000493 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
494 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000495 addLegalFPImmediate(TmpFlt2); // FLD1
496 TmpFlt2.changeSign();
497 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
498 }
499
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000500 if (!UnsafeFPMath) {
501 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
502 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
503 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000504
Dan Gohman2f7b1982007-10-11 23:21:31 +0000505 // Always use a library call for pow.
506 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
507 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
508 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
509
Dale Johannesen92b33082008-09-04 00:47:13 +0000510 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000511 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000512 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000513 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000514 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
515
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 // First set operation action for all vector types to expand. Then we
517 // will selectively turn on ones that can be effectively codegen'd.
518 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
519 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000520 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
522 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
524 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
525 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
526 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif825aa892008-08-28 23:19:51 +0000533 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
535 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands92c43912008-06-06 12:08:01 +0000536 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesen177edff2008-09-10 17:31:40 +0000558 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 }
564
565 if (Subtarget->hasMMX()) {
566 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
567 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
568 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000569 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
571
572 // FIXME: add MMX packed arithmetics
573
574 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
575 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
576 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
577 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
578
579 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
580 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
581 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000582 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583
584 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
585 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
586
587 setOperationAction(ISD::AND, MVT::v8i8, Promote);
588 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
589 setOperationAction(ISD::AND, MVT::v4i16, Promote);
590 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
591 setOperationAction(ISD::AND, MVT::v2i32, Promote);
592 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
593 setOperationAction(ISD::AND, MVT::v1i64, Legal);
594
595 setOperationAction(ISD::OR, MVT::v8i8, Promote);
596 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
597 setOperationAction(ISD::OR, MVT::v4i16, Promote);
598 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
599 setOperationAction(ISD::OR, MVT::v2i32, Promote);
600 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
601 setOperationAction(ISD::OR, MVT::v1i64, Legal);
602
603 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
604 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
605 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
606 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
607 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
608 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
609 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
610
611 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
612 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
613 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
614 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
615 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
616 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000617 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
618 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
620
621 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
622 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
623 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000624 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
626
627 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
628 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
629 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
630 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
631
Evan Cheng759fe022008-07-22 18:39:19 +0000632 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
634 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000636
637 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638 }
639
640 if (Subtarget->hasSSE1()) {
641 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
642
643 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
644 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
645 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
646 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
647 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
648 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
650 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
651 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
652 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
653 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000654 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655 }
656
657 if (Subtarget->hasSSE2()) {
658 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
659 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
660 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
661 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
662 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
663
664 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
665 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
666 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
667 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
668 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
669 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
670 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
671 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
672 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
673 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
674 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
675 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
676 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
677 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
678 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000679
Nate Begeman03605a02008-07-17 16:51:19 +0000680 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
681 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
682 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
683 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000684
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
686 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
687 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
688 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
690
691 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000692 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
693 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000694 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000695 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000696 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000697 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
698 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
699 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 }
701 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
702 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
703 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
704 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000705 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000707 if (Subtarget->is64Bit()) {
708 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000709 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000710 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711
712 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
713 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000714 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
715 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
716 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
717 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
718 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
719 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
720 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
721 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
722 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
723 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 }
725
Chris Lattner3bc08502008-01-17 19:59:44 +0000726 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000727
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000728 // Custom lower v2i64 and v2f64 selects.
729 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
730 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
731 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
732 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000733
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000735
736 if (Subtarget->hasSSE41()) {
737 // FIXME: Do we need to handle scalar-to-vector here?
738 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000739 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000740
741 // i8 and i16 vectors are custom , because the source register and source
742 // source memory operand types are not the same width. f32 vectors are
743 // custom since the immediate controlling the insert encodes additional
744 // information.
745 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
746 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
747 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
748 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
749
750 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
751 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
752 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000753 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000754
755 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000756 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
757 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000758 }
759 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760
Nate Begeman03605a02008-07-17 16:51:19 +0000761 if (Subtarget->hasSSE42()) {
762 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
763 }
764
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765 // We want to custom lower some of our intrinsics.
766 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
767
768 // We have target-specific dag combine patterns for the following nodes:
769 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000770 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000771 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000772 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773
774 computeRegisterProperties();
775
776 // FIXME: These should be based on subtarget info. Plus, the values should
777 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000778 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
779 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
780 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000781 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000782 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000783}
784
Scott Michel502151f2008-03-10 15:42:14 +0000785
Dan Gohman8181bd12008-07-27 21:46:04 +0000786MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000787 return MVT::i8;
788}
789
790
Evan Cheng5a67b812008-01-23 23:17:41 +0000791/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
792/// the desired ByVal argument alignment.
793static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
794 if (MaxAlign == 16)
795 return;
796 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
797 if (VTy->getBitWidth() == 128)
798 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000799 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
800 unsigned EltAlign = 0;
801 getMaxByValAlign(ATy->getElementType(), EltAlign);
802 if (EltAlign > MaxAlign)
803 MaxAlign = EltAlign;
804 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
805 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
806 unsigned EltAlign = 0;
807 getMaxByValAlign(STy->getElementType(i), EltAlign);
808 if (EltAlign > MaxAlign)
809 MaxAlign = EltAlign;
810 if (MaxAlign == 16)
811 break;
812 }
813 }
814 return;
815}
816
817/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
818/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000819/// that contain SSE vectors are placed at 16-byte boundaries while the rest
820/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000821unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000822 if (Subtarget->is64Bit()) {
823 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000824 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000825 if (TyAlign > 8)
826 return TyAlign;
827 return 8;
828 }
829
Evan Cheng5a67b812008-01-23 23:17:41 +0000830 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000831 if (Subtarget->hasSSE1())
832 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000833 return Align;
834}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835
Evan Cheng8c590372008-05-15 08:39:06 +0000836/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000837/// and store operations as a result of memset, memcpy, and memmove
838/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000839/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000840MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000841X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
842 bool isSrcConst, bool isSrcStr) const {
843 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
844 return MVT::v4i32;
845 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
846 return MVT::v4f32;
847 if (Subtarget->is64Bit() && Size >= 8)
848 return MVT::i64;
849 return MVT::i32;
850}
851
852
Evan Cheng6fb06762007-11-09 01:32:10 +0000853/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
854/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000855SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000856 SelectionDAG &DAG) const {
857 if (usesGlobalOffsetTable())
858 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
859 if (!Subtarget->isPICStyleRIPRel())
860 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
861 return Table;
862}
863
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864//===----------------------------------------------------------------------===//
865// Return Value Calling Convention Implementation
866//===----------------------------------------------------------------------===//
867
868#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000869
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000871SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
873
874 SmallVector<CCValAssign, 16> RVLocs;
875 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
876 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
877 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000878 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000879
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880 // If this is the first return lowered for this function, add the regs to the
881 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000882 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 for (unsigned i = 0; i != RVLocs.size(); ++i)
884 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000885 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000887 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000889 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000890 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000891 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000892 SDValue TailCall = Chain;
893 SDValue TargetAddress = TailCall.getOperand(1);
894 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000895 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000896 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000897 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000898 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000899 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
900 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000901 assert(StackAdjustment.getOpcode() == ISD::Constant &&
902 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000903
Dan Gohman8181bd12008-07-27 21:46:04 +0000904 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000905 Operands.push_back(Chain.getOperand(0));
906 Operands.push_back(TargetAddress);
907 Operands.push_back(StackAdjustment);
908 // Copy registers used by the call. Last operand is a flag so it is not
909 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000910 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000911 Operands.push_back(Chain.getOperand(i));
912 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000913 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
914 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000915 }
916
917 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000918 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000919
Dan Gohman8181bd12008-07-27 21:46:04 +0000920 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000921 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
922 // Operand #1 = Bytes To Pop
923 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
924
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000925 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000926 for (unsigned i = 0; i != RVLocs.size(); ++i) {
927 CCValAssign &VA = RVLocs[i];
928 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000929 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930
Chris Lattnerb56cc342008-03-11 03:23:40 +0000931 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
932 // the RET instruction and handled by the FP Stackifier.
933 if (RVLocs[i].getLocReg() == X86::ST0 ||
934 RVLocs[i].getLocReg() == X86::ST1) {
935 // If this is a copy from an xmm register to ST(0), use an FPExtend to
936 // change the value to the FP stack register class.
937 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
938 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
939 RetOps.push_back(ValToCopy);
940 // Don't emit a copytoreg.
941 continue;
942 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000943
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000944 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000945 Flag = Chain.getValue(1);
946 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000947
948 // The x86-64 ABI for returning structs by value requires that we copy
949 // the sret argument into %rax for the return. We saved the argument into
950 // a virtual register in the entry block, so now we copy the value out
951 // and into %rax.
952 if (Subtarget->is64Bit() &&
953 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
954 MachineFunction &MF = DAG.getMachineFunction();
955 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
956 unsigned Reg = FuncInfo->getSRetReturnReg();
957 if (!Reg) {
958 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
959 FuncInfo->setSRetReturnReg(Reg);
960 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000961 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000962
963 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
964 Flag = Chain.getValue(1);
965 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966
Chris Lattnerb56cc342008-03-11 03:23:40 +0000967 RetOps[0] = Chain; // Update chain.
968
969 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +0000970 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +0000971 RetOps.push_back(Flag);
972
973 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000974}
975
976
977/// LowerCallResult - Lower the result values of an ISD::CALL into the
978/// appropriate copies out of appropriate physical registers. This assumes that
979/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
980/// being lowered. The returns a SDNode with the same number of values as the
981/// ISD::CALL.
982SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +0000983LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 unsigned CallingConv, SelectionDAG &DAG) {
985
986 // Assign locations to each value returned by this call.
987 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +0000988 bool isVarArg = TheCall->isVarArg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
990 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
991
Dan Gohman8181bd12008-07-27 21:46:04 +0000992 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993
994 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000995 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +0000996 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000997
998 // If this is a call to a function that returns an fp value on the floating
999 // point stack, but where we prefer to use the value in xmm registers, copy
1000 // 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 +00001001 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1002 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001003 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1004 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001005 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001007 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1008 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001009 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001010 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001011
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001012 if (CopyVT != RVLocs[i].getValVT()) {
1013 // Round the F80 the right size, which also moves to the appropriate xmm
1014 // register.
1015 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1016 // This truncation won't change the value.
1017 DAG.getIntPtrConstant(1));
1018 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001019
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001020 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001021 }
Duncan Sands698842f2008-07-02 17:40:58 +00001022
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001023 // Merge everything together with a MERGE_VALUES node.
1024 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +00001025 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
Gabor Greif1c80d112008-08-28 21:40:38 +00001026 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001027}
1028
1029
1030//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001031// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032//===----------------------------------------------------------------------===//
1033// StdCall calling convention seems to be standard for many Windows' API
1034// routines and around. It differs from C calling convention just a little:
1035// callee should clean up the stack, not caller. Symbols should be also
1036// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001037// For info on fast calling convention see Fast Calling Convention (tail call)
1038// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039
1040/// AddLiveIn - This helper function adds the specified physical register to the
1041/// MachineFunction as a live in value. It also creates a corresponding virtual
1042/// register for it.
1043static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1044 const TargetRegisterClass *RC) {
1045 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001046 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1047 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001048 return VReg;
1049}
1050
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001051/// CallIsStructReturn - Determines whether a CALL node uses struct return
1052/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001053static bool CallIsStructReturn(CallSDNode *TheCall) {
1054 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001055 if (!NumOps)
1056 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001057
Dan Gohman705e3f72008-09-13 01:54:27 +00001058 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001059}
1060
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001061/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1062/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001063static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001064 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001065 if (!NumArgs)
1066 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001067
1068 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001069}
1070
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001071/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1072/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001073/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001074bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001075 if (IsVarArg)
1076 return false;
1077
Dan Gohman705e3f72008-09-13 01:54:27 +00001078 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001079 default:
1080 return false;
1081 case CallingConv::X86_StdCall:
1082 return !Subtarget->is64Bit();
1083 case CallingConv::X86_FastCall:
1084 return !Subtarget->is64Bit();
1085 case CallingConv::Fast:
1086 return PerformTailCallOpt;
1087 }
1088}
1089
Dan Gohman705e3f72008-09-13 01:54:27 +00001090/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1091/// given CallingConvention value.
1092CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001093 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001094 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001095 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001096 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1097 return CC_X86_64_TailCall;
1098 else
1099 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001100 }
1101
Gordon Henriksen18ace102008-01-05 16:56:59 +00001102 if (CC == CallingConv::X86_FastCall)
1103 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001104 else if (CC == CallingConv::Fast)
1105 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001106 else
1107 return CC_X86_32_C;
1108}
1109
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001110/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1111/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001112NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001113X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001114 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001115 if (CC == CallingConv::X86_FastCall)
1116 return FastCall;
1117 else if (CC == CallingConv::X86_StdCall)
1118 return StdCall;
1119 return None;
1120}
1121
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001122
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001123/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1124/// in a register before calling.
1125bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1126 return !IsTailCall && !Is64Bit &&
1127 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1128 Subtarget->isPICStyleGOT();
1129}
1130
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001131/// CallRequiresFnAddressInReg - Check whether the call requires the function
1132/// address to be loaded in a register.
1133bool
1134X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1135 return !Is64Bit && IsTailCall &&
1136 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1137 Subtarget->isPICStyleGOT();
1138}
1139
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001140/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1141/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001142/// the specific parameter attribute. The copy will be passed as a byval
1143/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001144static SDValue
1145CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001146 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001147 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001148 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001149 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001150}
1151
Dan Gohman8181bd12008-07-27 21:46:04 +00001152SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001153 const CCValAssign &VA,
1154 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001155 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001156 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001157 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001158 ISD::ArgFlagsTy Flags =
1159 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001160 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001161 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001162
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001163 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1164 // changed with more analysis.
1165 // In case of tail call optimization mark all arguments mutable. Since they
1166 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001167 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001168 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001169 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001170 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001171 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001172 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001173 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001174}
1175
Dan Gohman8181bd12008-07-27 21:46:04 +00001176SDValue
1177X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001178 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001179 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1180
1181 const Function* Fn = MF.getFunction();
1182 if (Fn->hasExternalLinkage() &&
1183 Subtarget->isTargetCygMing() &&
1184 Fn->getName() == "main")
1185 FuncInfo->setForceFramePointer(true);
1186
1187 // Decorate the function name.
1188 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1189
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001190 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001191 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001192 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001193 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001194 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001195 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001196
1197 assert(!(isVarArg && CC == CallingConv::Fast) &&
1198 "Var args not supported with calling convention fastcc");
1199
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001200 // Assign locations to all of the incoming arguments.
1201 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001202 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001203 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001204
Dan Gohman8181bd12008-07-27 21:46:04 +00001205 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001206 unsigned LastVal = ~0U;
1207 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1208 CCValAssign &VA = ArgLocs[i];
1209 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1210 // places.
1211 assert(VA.getValNo() != LastVal &&
1212 "Don't support value assigned to multiple locs yet");
1213 LastVal = VA.getValNo();
1214
1215 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001216 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001217 TargetRegisterClass *RC;
1218 if (RegVT == MVT::i32)
1219 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001220 else if (Is64Bit && RegVT == MVT::i64)
1221 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001222 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001223 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001224 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001225 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001226 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001227 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001228 else if (RegVT.isVector()) {
1229 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001230 if (!Is64Bit)
1231 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1232 else {
1233 // Darwin calling convention passes MMX values in either GPRs or
1234 // XMMs in x86-64. Other targets pass them in memory.
1235 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1236 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1237 RegVT = MVT::v2i64;
1238 } else {
1239 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1240 RegVT = MVT::i64;
1241 }
1242 }
1243 } else {
1244 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001245 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001246
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001248 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001249
1250 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1251 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1252 // right size.
1253 if (VA.getLocInfo() == CCValAssign::SExt)
1254 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1255 DAG.getValueType(VA.getValVT()));
1256 else if (VA.getLocInfo() == CCValAssign::ZExt)
1257 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1258 DAG.getValueType(VA.getValVT()));
1259
1260 if (VA.getLocInfo() != CCValAssign::Full)
1261 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1262
Gordon Henriksen18ace102008-01-05 16:56:59 +00001263 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001264 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001265 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001266 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1267 else if (RC == X86::VR128RegisterClass) {
1268 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1269 DAG.getConstant(0, MVT::i64));
1270 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1271 }
1272 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001273
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 ArgValues.push_back(ArgValue);
1275 } else {
1276 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001277 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001278 }
1279 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001280
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001281 // The x86-64 ABI for returning structs by value requires that we copy
1282 // the sret argument into %rax for the return. Save the argument into
1283 // a virtual register so that we can access it from the return points.
1284 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1285 MachineFunction &MF = DAG.getMachineFunction();
1286 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1287 unsigned Reg = FuncInfo->getSRetReturnReg();
1288 if (!Reg) {
1289 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1290 FuncInfo->setSRetReturnReg(Reg);
1291 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001292 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001293 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1294 }
1295
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001296 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001297 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001298 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001299 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001300
1301 // If the function takes variable number of arguments, make a frame index for
1302 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001303 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001304 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1305 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1306 }
1307 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001308 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1309
1310 // FIXME: We should really autogenerate these arrays
1311 static const unsigned GPR64ArgRegsWin64[] = {
1312 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001313 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001314 static const unsigned XMMArgRegsWin64[] = {
1315 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1316 };
1317 static const unsigned GPR64ArgRegs64Bit[] = {
1318 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1319 };
1320 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001321 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1322 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1323 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001324 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1325
1326 if (IsWin64) {
1327 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1328 GPR64ArgRegs = GPR64ArgRegsWin64;
1329 XMMArgRegs = XMMArgRegsWin64;
1330 } else {
1331 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1332 GPR64ArgRegs = GPR64ArgRegs64Bit;
1333 XMMArgRegs = XMMArgRegs64Bit;
1334 }
1335 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1336 TotalNumIntRegs);
1337 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1338 TotalNumXMMRegs);
1339
Gordon Henriksen18ace102008-01-05 16:56:59 +00001340 // For X86-64, if there are vararg parameters that are passed via
1341 // registers, then we must store them to their spots on the stack so they
1342 // may be loaded by deferencing the result of va_next.
1343 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001344 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1345 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1346 TotalNumXMMRegs * 16, 16);
1347
Gordon Henriksen18ace102008-01-05 16:56:59 +00001348 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001349 SmallVector<SDValue, 8> MemOps;
1350 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1351 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001352 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001353 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001354 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1355 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001356 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1357 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001358 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001359 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001360 MemOps.push_back(Store);
1361 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001362 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001363 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001364
Gordon Henriksen18ace102008-01-05 16:56:59 +00001365 // Now store the XMM (fp + vector) parameter registers.
1366 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001367 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001368 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001369 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1370 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001371 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1372 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001373 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001374 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001375 MemOps.push_back(Store);
1376 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001377 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001378 }
1379 if (!MemOps.empty())
1380 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1381 &MemOps[0], MemOps.size());
1382 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001383 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001384
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001385 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001386
Gordon Henriksen18ace102008-01-05 16:56:59 +00001387 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001388 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001389 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390 BytesCallerReserves = 0;
1391 } else {
1392 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001394 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001395 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001396 BytesCallerReserves = StackSize;
1397 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001398
Gordon Henriksen18ace102008-01-05 16:56:59 +00001399 if (!Is64Bit) {
1400 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1401 if (CC == CallingConv::X86_FastCall)
1402 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1403 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001404
Anton Korobeynikove844e472007-08-15 17:12:32 +00001405 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001406
1407 // Return the new list of results.
Gabor Greif1c80d112008-08-28 21:40:38 +00001408 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif46bf5472008-08-26 22:36:50 +00001409 ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001410}
1411
Dan Gohman8181bd12008-07-27 21:46:04 +00001412SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001413X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001414 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001415 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001416 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001417 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001418 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001419 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001420 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001421 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001422 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001423 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001424 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001425 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001426}
1427
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001428/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1429/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001430SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001431X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001432 SDValue &OutRetAddr,
1433 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001434 bool IsTailCall,
1435 bool Is64Bit,
1436 int FPDiff) {
1437 if (!IsTailCall || FPDiff==0) return Chain;
1438
1439 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001440 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001441 OutRetAddr = getReturnAddressFrameIndex(DAG);
1442 // Load the "old" Return address.
1443 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001444 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001445}
1446
1447/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1448/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001449static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001450EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001451 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001452 bool Is64Bit, int FPDiff) {
1453 // Store the return address to the appropriate stack slot.
1454 if (!FPDiff) return Chain;
1455 // Calculate the new stack slot for the return address.
1456 int SlotSize = Is64Bit ? 8 : 4;
1457 int NewReturnAddrFI =
1458 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001459 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001460 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001461 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001462 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001463 return Chain;
1464}
1465
Dan Gohman8181bd12008-07-27 21:46:04 +00001466SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001467 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001468 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1469 SDValue Chain = TheCall->getChain();
1470 unsigned CC = TheCall->getCallingConv();
1471 bool isVarArg = TheCall->isVarArg();
1472 bool IsTailCall = TheCall->isTailCall() &&
1473 CC == CallingConv::Fast && PerformTailCallOpt;
1474 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001475 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001476 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001477
1478 assert(!(isVarArg && CC == CallingConv::Fast) &&
1479 "Var args not supported with calling convention fastcc");
1480
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481 // Analyze operands of the call, assigning locations to each operand.
1482 SmallVector<CCValAssign, 16> ArgLocs;
1483 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001484 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001485
1486 // Get a count of how many bytes are to be pushed on the stack.
1487 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001488 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001489 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001490
Gordon Henriksen18ace102008-01-05 16:56:59 +00001491 int FPDiff = 0;
1492 if (IsTailCall) {
1493 // Lower arguments at fp - stackoffset + fpdiff.
1494 unsigned NumBytesCallerPushed =
1495 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1496 FPDiff = NumBytesCallerPushed - NumBytes;
1497
1498 // Set the delta of movement of the returnaddr stackslot.
1499 // But only set if delta is greater than previous delta.
1500 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1501 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1502 }
1503
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001504 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001505
Dan Gohman8181bd12008-07-27 21:46:04 +00001506 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001507 // Load return adress for tail calls.
1508 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1509 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001510
Dan Gohman8181bd12008-07-27 21:46:04 +00001511 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1512 SmallVector<SDValue, 8> MemOpChains;
1513 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001514
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001515 // Walk the register/memloc assignments, inserting copies/loads. In the case
1516 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001517 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1518 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001519 SDValue Arg = TheCall->getArg(i);
1520 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1521 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001522
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001523 // Promote the value if needed.
1524 switch (VA.getLocInfo()) {
1525 default: assert(0 && "Unknown loc info!");
1526 case CCValAssign::Full: break;
1527 case CCValAssign::SExt:
1528 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1529 break;
1530 case CCValAssign::ZExt:
1531 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1532 break;
1533 case CCValAssign::AExt:
1534 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1535 break;
1536 }
1537
1538 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001539 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001540 MVT RegVT = VA.getLocVT();
1541 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001542 switch (VA.getLocReg()) {
1543 default:
1544 break;
1545 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1546 case X86::R8: {
1547 // Special case: passing MMX values in GPR registers.
1548 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1549 break;
1550 }
1551 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1552 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1553 // Special case: passing MMX values in XMM registers.
1554 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1555 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1556 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1557 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1558 getMOVLMask(2, DAG));
1559 break;
1560 }
1561 }
1562 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001563 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1564 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001565 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001566 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001567 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001568 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1569
Dan Gohman705e3f72008-09-13 01:54:27 +00001570 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1571 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001572 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001573 }
1574 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001575
1576 if (!MemOpChains.empty())
1577 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1578 &MemOpChains[0], MemOpChains.size());
1579
1580 // Build a sequence of copy-to-reg nodes chained together with token chain
1581 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001582 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001583 // Tail call byval lowering might overwrite argument registers so in case of
1584 // tail call optimization the copies to registers are lowered later.
1585 if (!IsTailCall)
1586 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1587 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1588 InFlag);
1589 InFlag = Chain.getValue(1);
1590 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001591
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001592 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001593 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001594 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1595 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1596 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1597 InFlag);
1598 InFlag = Chain.getValue(1);
1599 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001600 // If we are tail calling and generating PIC/GOT style code load the address
1601 // of the callee into ecx. The value in ecx is used as target of the tail
1602 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1603 // calls on PIC/GOT architectures. Normally we would just put the address of
1604 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1605 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001606 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001607 // Note: The actual moving to ecx is done further down.
1608 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001609 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001610 !G->getGlobal()->hasProtectedVisibility())
1611 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001612 else if (isa<ExternalSymbolSDNode>(Callee))
1613 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001614 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001615
Gordon Henriksen18ace102008-01-05 16:56:59 +00001616 if (Is64Bit && isVarArg) {
1617 // From AMD64 ABI document:
1618 // For calls that may call functions that use varargs or stdargs
1619 // (prototype-less calls or calls to functions containing ellipsis (...) in
1620 // the declaration) %al is used as hidden argument to specify the number
1621 // of SSE registers used. The contents of %al do not need to match exactly
1622 // the number of registers, but must be an ubound on the number of SSE
1623 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001624
1625 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001626 // Count the number of XMM registers allocated.
1627 static const unsigned XMMArgRegs[] = {
1628 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1629 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1630 };
1631 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1632
1633 Chain = DAG.getCopyToReg(Chain, X86::AL,
1634 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1635 InFlag = Chain.getValue(1);
1636 }
1637
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001638
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001639 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001640 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001641 SmallVector<SDValue, 8> MemOpChains2;
1642 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001643 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001644 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001645 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001646 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1647 CCValAssign &VA = ArgLocs[i];
1648 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001649 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001650 SDValue Arg = TheCall->getArg(i);
1651 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001652 // Create frame index.
1653 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001654 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001655 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001656 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001657
Duncan Sandsc93fae32008-03-21 09:14:45 +00001658 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001659 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001660 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001661 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001662 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1663 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1664
1665 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001666 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001667 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001668 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001669 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001670 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001671 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001672 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001673 }
1674 }
1675
1676 if (!MemOpChains2.empty())
1677 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001678 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001679
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001680 // Copy arguments to their registers.
1681 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1682 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1683 InFlag);
1684 InFlag = Chain.getValue(1);
1685 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001686 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001687
Gordon Henriksen18ace102008-01-05 16:56:59 +00001688 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001689 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1690 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001691 }
1692
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001693 // If the callee is a GlobalAddress node (quite common, every direct call is)
1694 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1695 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1696 // We should use extra load for direct calls to dllimported functions in
1697 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001698 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1699 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001700 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bill Wendlingfef06052008-09-16 21:48:12 +00001701 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1702 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001703 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001704 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001705
1706 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001707 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001708 Callee,InFlag);
1709 Callee = DAG.getRegister(Opc, getPointerTy());
1710 // Add register as live out.
1711 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001712 }
1713
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001714 // Returns a chain & a flag for retval copy to use.
1715 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001716 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001717
1718 if (IsTailCall) {
1719 Ops.push_back(Chain);
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001720 Ops.push_back(DAG.getIntPtrConstant(NumBytes, true));
1721 Ops.push_back(DAG.getIntPtrConstant(0, true));
Gabor Greif1c80d112008-08-28 21:40:38 +00001722 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001723 Ops.push_back(InFlag);
1724 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1725 InFlag = Chain.getValue(1);
1726
1727 // Returns a chain & a flag for retval copy to use.
1728 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1729 Ops.clear();
1730 }
1731
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001732 Ops.push_back(Chain);
1733 Ops.push_back(Callee);
1734
Gordon Henriksen18ace102008-01-05 16:56:59 +00001735 if (IsTailCall)
1736 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001737
Gordon Henriksen18ace102008-01-05 16:56:59 +00001738 // Add argument registers to the end of the list so that they are known live
1739 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001740 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1741 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1742 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001743
Evan Cheng8ba45e62008-03-18 23:36:35 +00001744 // Add an implicit use GOT pointer in EBX.
1745 if (!IsTailCall && !Is64Bit &&
1746 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1747 Subtarget->isPICStyleGOT())
1748 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1749
1750 // Add an implicit use of AL for x86 vararg functions.
1751 if (Is64Bit && isVarArg)
1752 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1753
Gabor Greif1c80d112008-08-28 21:40:38 +00001754 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001755 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001756
Gordon Henriksen18ace102008-01-05 16:56:59 +00001757 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001758 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001759 "Flag must be set. Depend on flag being set in LowerRET");
1760 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman705e3f72008-09-13 01:54:27 +00001761 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001762
Gabor Greif1c80d112008-08-28 21:40:38 +00001763 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001764 }
1765
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001766 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001767 InFlag = Chain.getValue(1);
1768
1769 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001770 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001771 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001772 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001773 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001774 // If this is is a call to a struct-return function, the callee
1775 // pops the hidden struct pointer, so we have to push it back.
1776 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001777 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001778 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001779 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001780
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001781 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001782 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001783 DAG.getIntPtrConstant(NumBytes, true),
1784 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1785 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001786 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001787 InFlag = Chain.getValue(1);
1788
1789 // Handle result values, copying them out of physregs into vregs that we
1790 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001791 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001792 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001793}
1794
1795
1796//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001797// Fast Calling Convention (tail call) implementation
1798//===----------------------------------------------------------------------===//
1799
1800// Like std call, callee cleans arguments, convention except that ECX is
1801// reserved for storing the tail called function address. Only 2 registers are
1802// free for argument passing (inreg). Tail call optimization is performed
1803// provided:
1804// * tailcallopt is enabled
1805// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001806// On X86_64 architecture with GOT-style position independent code only local
1807// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001808// To keep the stack aligned according to platform abi the function
1809// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1810// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001811// If a tail called function callee has more arguments than the caller the
1812// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001813// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001814// original REtADDR, but before the saved framepointer or the spilled registers
1815// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1816// stack layout:
1817// arg1
1818// arg2
1819// RETADDR
1820// [ new RETADDR
1821// move area ]
1822// (possible EBP)
1823// ESI
1824// EDI
1825// local1 ..
1826
1827/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1828/// for a 16 byte align requirement.
1829unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1830 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001831 MachineFunction &MF = DAG.getMachineFunction();
1832 const TargetMachine &TM = MF.getTarget();
1833 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1834 unsigned StackAlignment = TFI.getStackAlignment();
1835 uint64_t AlignMask = StackAlignment - 1;
1836 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001837 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001838 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1839 // Number smaller than 12 so just add the difference.
1840 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1841 } else {
1842 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1843 Offset = ((~AlignMask) & Offset) + StackAlignment +
1844 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001845 }
Evan Chengded8f902008-09-07 09:07:23 +00001846 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001847}
1848
1849/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001850/// following the call is a return. A function is eligible if caller/callee
1851/// calling conventions match, currently only fastcc supports tail calls, and
1852/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001853bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001854 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001855 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001856 if (!PerformTailCallOpt)
1857 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001858
Dan Gohman705e3f72008-09-13 01:54:27 +00001859 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001860 MachineFunction &MF = DAG.getMachineFunction();
1861 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001862 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001863 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001864 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001865 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001866 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001867 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001868 return true;
1869
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001870 // Can only do local tail calls (in same module, hidden or protected) on
1871 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001872 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1873 return G->getGlobal()->hasHiddenVisibility()
1874 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001875 }
1876 }
Evan Chenge7a87392007-11-02 01:26:22 +00001877
1878 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001879}
1880
Dan Gohmanca4857a2008-09-03 23:12:08 +00001881FastISel *
1882X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001883 MachineModuleInfo *mmo,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001884 DenseMap<const Value *, unsigned> &vm,
1885 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001886 MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00001887 DenseMap<const AllocaInst *, int> &am
1888#ifndef NDEBUG
1889 , SmallSet<Instruction*, 8> &cil
1890#endif
1891 ) {
1892 return X86::createFastISel(mf, mmo, vm, bm, am
1893#ifndef NDEBUG
1894 , cil
1895#endif
1896 );
Dan Gohman97805ee2008-08-19 21:32:53 +00001897}
1898
1899
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001900//===----------------------------------------------------------------------===//
1901// Other Lowering Hooks
1902//===----------------------------------------------------------------------===//
1903
1904
Dan Gohman8181bd12008-07-27 21:46:04 +00001905SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001906 MachineFunction &MF = DAG.getMachineFunction();
1907 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1908 int ReturnAddrIndex = FuncInfo->getRAIndex();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001909 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikove844e472007-08-15 17:12:32 +00001910
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001911 if (ReturnAddrIndex == 0) {
1912 // Set up a frame object for the return address.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001913 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001914 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001915 }
1916
1917 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1918}
1919
1920
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001921/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1922/// specific condition code. It returns a false if it cannot do a direct
1923/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1924/// needed.
1925static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001926 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001927 SelectionDAG &DAG) {
1928 X86CC = X86::COND_INVALID;
1929 if (!isFP) {
1930 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1931 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1932 // X > -1 -> X == 0, jump !sign.
1933 RHS = DAG.getConstant(0, RHS.getValueType());
1934 X86CC = X86::COND_NS;
1935 return true;
1936 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1937 // X < 0 -> X == 0, jump on sign.
1938 X86CC = X86::COND_S;
1939 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001940 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00001941 // X < 1 -> X <= 0
1942 RHS = DAG.getConstant(0, RHS.getValueType());
1943 X86CC = X86::COND_LE;
1944 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001945 }
1946 }
1947
1948 switch (SetCCOpcode) {
1949 default: break;
1950 case ISD::SETEQ: X86CC = X86::COND_E; break;
1951 case ISD::SETGT: X86CC = X86::COND_G; break;
1952 case ISD::SETGE: X86CC = X86::COND_GE; break;
1953 case ISD::SETLT: X86CC = X86::COND_L; break;
1954 case ISD::SETLE: X86CC = X86::COND_LE; break;
1955 case ISD::SETNE: X86CC = X86::COND_NE; break;
1956 case ISD::SETULT: X86CC = X86::COND_B; break;
1957 case ISD::SETUGT: X86CC = X86::COND_A; break;
1958 case ISD::SETULE: X86CC = X86::COND_BE; break;
1959 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1960 }
1961 } else {
Evan Chengb488ca32008-08-29 23:22:12 +00001962 // First determine if it requires or is profitable to flip the operands.
1963 bool Flip = false;
1964 switch (SetCCOpcode) {
1965 default: break;
1966 case ISD::SETOLT:
1967 case ISD::SETOLE:
1968 case ISD::SETUGT:
1969 case ISD::SETUGE:
1970 Flip = true;
1971 break;
1972 }
1973
1974 // If LHS is a foldable load, but RHS is not, flip the condition.
1975 if (!Flip &&
1976 (ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1977 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1978 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1979 Flip = true;
1980 }
1981 if (Flip)
1982 std::swap(LHS, RHS);
1983
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001984 // On a floating point condition, the flags are set as follows:
1985 // ZF PF CF op
1986 // 0 | 0 | 0 | X > Y
1987 // 0 | 0 | 1 | X < Y
1988 // 1 | 0 | 0 | X == Y
1989 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001990 switch (SetCCOpcode) {
1991 default: break;
1992 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00001993 case ISD::SETEQ:
1994 X86CC = X86::COND_E;
1995 break;
1996 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001997 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00001998 case ISD::SETGT:
1999 X86CC = X86::COND_A;
2000 break;
2001 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002002 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00002003 case ISD::SETGE:
2004 X86CC = X86::COND_AE;
2005 break;
2006 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002007 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00002008 case ISD::SETLT:
2009 X86CC = X86::COND_B;
2010 break;
2011 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002012 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00002013 case ISD::SETLE:
2014 X86CC = X86::COND_BE;
2015 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002016 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00002017 case ISD::SETNE:
2018 X86CC = X86::COND_NE;
2019 break;
2020 case ISD::SETUO:
2021 X86CC = X86::COND_P;
2022 break;
2023 case ISD::SETO:
2024 X86CC = X86::COND_NP;
2025 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002026 }
Evan Chengfc937c92008-08-28 23:48:31 +00002027 }
2028
Evan Chengc6162692008-08-29 22:13:21 +00002029 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002030}
2031
2032/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2033/// code. Current x86 isa includes the following FP cmov instructions:
2034/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2035static bool hasFPCMov(unsigned X86CC) {
2036 switch (X86CC) {
2037 default:
2038 return false;
2039 case X86::COND_B:
2040 case X86::COND_BE:
2041 case X86::COND_E:
2042 case X86::COND_P:
2043 case X86::COND_A:
2044 case X86::COND_AE:
2045 case X86::COND_NE:
2046 case X86::COND_NP:
2047 return true;
2048 }
2049}
2050
2051/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2052/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002053static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002054 if (Op.getOpcode() == ISD::UNDEF)
2055 return true;
2056
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002057 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002058 return (Val >= Low && Val < Hi);
2059}
2060
2061/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2062/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002063static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002064 if (Op.getOpcode() == ISD::UNDEF)
2065 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002066 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002067}
2068
2069/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2070/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2071bool X86::isPSHUFDMask(SDNode *N) {
2072 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2073
Dan Gohman7dc19012007-08-02 21:17:01 +00002074 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002075 return false;
2076
2077 // Check if the value doesn't reference the second vector.
2078 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002079 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002080 if (Arg.getOpcode() == ISD::UNDEF) continue;
2081 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002082 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002083 return false;
2084 }
2085
2086 return true;
2087}
2088
2089/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2090/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2091bool X86::isPSHUFHWMask(SDNode *N) {
2092 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2093
2094 if (N->getNumOperands() != 8)
2095 return false;
2096
2097 // Lower quadword copied in order.
2098 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002099 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002100 if (Arg.getOpcode() == ISD::UNDEF) continue;
2101 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002102 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002103 return false;
2104 }
2105
2106 // Upper quadword shuffled.
2107 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002108 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002109 if (Arg.getOpcode() == ISD::UNDEF) continue;
2110 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002111 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002112 if (Val < 4 || Val > 7)
2113 return false;
2114 }
2115
2116 return true;
2117}
2118
2119/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2120/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2121bool X86::isPSHUFLWMask(SDNode *N) {
2122 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2123
2124 if (N->getNumOperands() != 8)
2125 return false;
2126
2127 // Upper quadword copied in order.
2128 for (unsigned i = 4; i != 8; ++i)
2129 if (!isUndefOrEqual(N->getOperand(i), i))
2130 return false;
2131
2132 // Lower quadword shuffled.
2133 for (unsigned i = 0; i != 4; ++i)
2134 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2135 return false;
2136
2137 return true;
2138}
2139
2140/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2141/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002142static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002143 if (NumElems != 2 && NumElems != 4) return false;
2144
2145 unsigned Half = NumElems / 2;
2146 for (unsigned i = 0; i < Half; ++i)
2147 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2148 return false;
2149 for (unsigned i = Half; i < NumElems; ++i)
2150 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2151 return false;
2152
2153 return true;
2154}
2155
2156bool X86::isSHUFPMask(SDNode *N) {
2157 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2158 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2159}
2160
2161/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2162/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2163/// half elements to come from vector 1 (which would equal the dest.) and
2164/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002165static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002166 if (NumOps != 2 && NumOps != 4) return false;
2167
2168 unsigned Half = NumOps / 2;
2169 for (unsigned i = 0; i < Half; ++i)
2170 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2171 return false;
2172 for (unsigned i = Half; i < NumOps; ++i)
2173 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2174 return false;
2175 return true;
2176}
2177
2178static bool isCommutedSHUFP(SDNode *N) {
2179 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2180 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2181}
2182
2183/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2184/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2185bool X86::isMOVHLPSMask(SDNode *N) {
2186 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2187
2188 if (N->getNumOperands() != 4)
2189 return false;
2190
2191 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2192 return isUndefOrEqual(N->getOperand(0), 6) &&
2193 isUndefOrEqual(N->getOperand(1), 7) &&
2194 isUndefOrEqual(N->getOperand(2), 2) &&
2195 isUndefOrEqual(N->getOperand(3), 3);
2196}
2197
2198/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2199/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2200/// <2, 3, 2, 3>
2201bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2202 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2203
2204 if (N->getNumOperands() != 4)
2205 return false;
2206
2207 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2208 return isUndefOrEqual(N->getOperand(0), 2) &&
2209 isUndefOrEqual(N->getOperand(1), 3) &&
2210 isUndefOrEqual(N->getOperand(2), 2) &&
2211 isUndefOrEqual(N->getOperand(3), 3);
2212}
2213
2214/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2215/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2216bool X86::isMOVLPMask(SDNode *N) {
2217 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2218
2219 unsigned NumElems = N->getNumOperands();
2220 if (NumElems != 2 && NumElems != 4)
2221 return false;
2222
2223 for (unsigned i = 0; i < NumElems/2; ++i)
2224 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2225 return false;
2226
2227 for (unsigned i = NumElems/2; i < NumElems; ++i)
2228 if (!isUndefOrEqual(N->getOperand(i), i))
2229 return false;
2230
2231 return true;
2232}
2233
2234/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2235/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2236/// and MOVLHPS.
2237bool X86::isMOVHPMask(SDNode *N) {
2238 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2239
2240 unsigned NumElems = N->getNumOperands();
2241 if (NumElems != 2 && NumElems != 4)
2242 return false;
2243
2244 for (unsigned i = 0; i < NumElems/2; ++i)
2245 if (!isUndefOrEqual(N->getOperand(i), i))
2246 return false;
2247
2248 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002249 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002250 if (!isUndefOrEqual(Arg, i + NumElems))
2251 return false;
2252 }
2253
2254 return true;
2255}
2256
2257/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2258/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002259bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002260 bool V2IsSplat = false) {
2261 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2262 return false;
2263
2264 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002265 SDValue BitI = Elts[i];
2266 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002267 if (!isUndefOrEqual(BitI, j))
2268 return false;
2269 if (V2IsSplat) {
2270 if (isUndefOrEqual(BitI1, NumElts))
2271 return false;
2272 } else {
2273 if (!isUndefOrEqual(BitI1, j + NumElts))
2274 return false;
2275 }
2276 }
2277
2278 return true;
2279}
2280
2281bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2282 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2283 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2284}
2285
2286/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2287/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002288bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002289 bool V2IsSplat = false) {
2290 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2291 return false;
2292
2293 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002294 SDValue BitI = Elts[i];
2295 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002296 if (!isUndefOrEqual(BitI, j + NumElts/2))
2297 return false;
2298 if (V2IsSplat) {
2299 if (isUndefOrEqual(BitI1, NumElts))
2300 return false;
2301 } else {
2302 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2303 return false;
2304 }
2305 }
2306
2307 return true;
2308}
2309
2310bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2311 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2312 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2313}
2314
2315/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2316/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2317/// <0, 0, 1, 1>
2318bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2319 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2320
2321 unsigned NumElems = N->getNumOperands();
2322 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2323 return false;
2324
2325 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002326 SDValue BitI = N->getOperand(i);
2327 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002328
2329 if (!isUndefOrEqual(BitI, j))
2330 return false;
2331 if (!isUndefOrEqual(BitI1, j))
2332 return false;
2333 }
2334
2335 return true;
2336}
2337
2338/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2339/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2340/// <2, 2, 3, 3>
2341bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2342 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2343
2344 unsigned NumElems = N->getNumOperands();
2345 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2346 return false;
2347
2348 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002349 SDValue BitI = N->getOperand(i);
2350 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002351
2352 if (!isUndefOrEqual(BitI, j))
2353 return false;
2354 if (!isUndefOrEqual(BitI1, j))
2355 return false;
2356 }
2357
2358 return true;
2359}
2360
2361/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2362/// specifies a shuffle of elements that is suitable for input to MOVSS,
2363/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002364static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002365 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002366 return false;
2367
2368 if (!isUndefOrEqual(Elts[0], NumElts))
2369 return false;
2370
2371 for (unsigned i = 1; i < NumElts; ++i) {
2372 if (!isUndefOrEqual(Elts[i], i))
2373 return false;
2374 }
2375
2376 return true;
2377}
2378
2379bool X86::isMOVLMask(SDNode *N) {
2380 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2381 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2382}
2383
2384/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2385/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2386/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002387static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002388 bool V2IsSplat = false,
2389 bool V2IsUndef = false) {
2390 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2391 return false;
2392
2393 if (!isUndefOrEqual(Ops[0], 0))
2394 return false;
2395
2396 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002397 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002398 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2399 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2400 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2401 return false;
2402 }
2403
2404 return true;
2405}
2406
2407static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2408 bool V2IsUndef = false) {
2409 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2410 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2411 V2IsSplat, V2IsUndef);
2412}
2413
2414/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2415/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2416bool X86::isMOVSHDUPMask(SDNode *N) {
2417 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2418
2419 if (N->getNumOperands() != 4)
2420 return false;
2421
2422 // Expect 1, 1, 3, 3
2423 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002424 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002425 if (Arg.getOpcode() == ISD::UNDEF) continue;
2426 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002427 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002428 if (Val != 1) return false;
2429 }
2430
2431 bool HasHi = false;
2432 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002433 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002434 if (Arg.getOpcode() == ISD::UNDEF) continue;
2435 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002436 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002437 if (Val != 3) return false;
2438 HasHi = true;
2439 }
2440
2441 // Don't use movshdup if it can be done with a shufps.
2442 return HasHi;
2443}
2444
2445/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2446/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2447bool X86::isMOVSLDUPMask(SDNode *N) {
2448 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2449
2450 if (N->getNumOperands() != 4)
2451 return false;
2452
2453 // Expect 0, 0, 2, 2
2454 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002455 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002456 if (Arg.getOpcode() == ISD::UNDEF) continue;
2457 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002458 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002459 if (Val != 0) return false;
2460 }
2461
2462 bool HasHi = false;
2463 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002464 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002465 if (Arg.getOpcode() == ISD::UNDEF) continue;
2466 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002467 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002468 if (Val != 2) return false;
2469 HasHi = true;
2470 }
2471
2472 // Don't use movshdup if it can be done with a shufps.
2473 return HasHi;
2474}
2475
2476/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2477/// specifies a identity operation on the LHS or RHS.
2478static bool isIdentityMask(SDNode *N, bool RHS = false) {
2479 unsigned NumElems = N->getNumOperands();
2480 for (unsigned i = 0; i < NumElems; ++i)
2481 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2482 return false;
2483 return true;
2484}
2485
2486/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2487/// a splat of a single element.
2488static bool isSplatMask(SDNode *N) {
2489 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2490
2491 // This is a splat operation if each element of the permute is the same, and
2492 // if the value doesn't reference the second vector.
2493 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002494 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002495 unsigned i = 0;
2496 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002497 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002498 if (isa<ConstantSDNode>(Elt)) {
2499 ElementBase = Elt;
2500 break;
2501 }
2502 }
2503
Gabor Greif1c80d112008-08-28 21:40:38 +00002504 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002505 return false;
2506
2507 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002508 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002509 if (Arg.getOpcode() == ISD::UNDEF) continue;
2510 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2511 if (Arg != ElementBase) return false;
2512 }
2513
2514 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002515 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002516}
2517
2518/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2519/// a splat of a single element and it's a 2 or 4 element mask.
2520bool X86::isSplatMask(SDNode *N) {
2521 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2522
2523 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2524 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2525 return false;
2526 return ::isSplatMask(N);
2527}
2528
2529/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2530/// specifies a splat of zero element.
2531bool X86::isSplatLoMask(SDNode *N) {
2532 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2533
2534 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2535 if (!isUndefOrEqual(N->getOperand(i), 0))
2536 return false;
2537 return true;
2538}
2539
Evan Chenga2497eb2008-09-25 20:50:48 +00002540/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2541/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2542bool X86::isMOVDDUPMask(SDNode *N) {
2543 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2544
2545 unsigned e = N->getNumOperands() / 2;
2546 for (unsigned i = 0; i < e; ++i)
2547 if (!isUndefOrEqual(N->getOperand(i), i))
2548 return false;
2549 for (unsigned i = 0; i < e; ++i)
2550 if (!isUndefOrEqual(N->getOperand(e+i), i))
2551 return false;
2552 return true;
2553}
2554
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002555/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2556/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2557/// instructions.
2558unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2559 unsigned NumOperands = N->getNumOperands();
2560 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2561 unsigned Mask = 0;
2562 for (unsigned i = 0; i < NumOperands; ++i) {
2563 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002564 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002565 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002566 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002567 if (Val >= NumOperands) Val -= NumOperands;
2568 Mask |= Val;
2569 if (i != NumOperands - 1)
2570 Mask <<= Shift;
2571 }
2572
2573 return Mask;
2574}
2575
2576/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2577/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2578/// instructions.
2579unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2580 unsigned Mask = 0;
2581 // 8 nodes, but we only care about the last 4.
2582 for (unsigned i = 7; i >= 4; --i) {
2583 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002584 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002585 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002586 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002587 Mask |= (Val - 4);
2588 if (i != 4)
2589 Mask <<= 2;
2590 }
2591
2592 return Mask;
2593}
2594
2595/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2596/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2597/// instructions.
2598unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2599 unsigned Mask = 0;
2600 // 8 nodes, but we only care about the first 4.
2601 for (int i = 3; i >= 0; --i) {
2602 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002603 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002604 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002605 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002606 Mask |= Val;
2607 if (i != 0)
2608 Mask <<= 2;
2609 }
2610
2611 return Mask;
2612}
2613
2614/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2615/// specifies a 8 element shuffle that can be broken into a pair of
2616/// PSHUFHW and PSHUFLW.
2617static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2618 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2619
2620 if (N->getNumOperands() != 8)
2621 return false;
2622
2623 // Lower quadword shuffled.
2624 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002625 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002626 if (Arg.getOpcode() == ISD::UNDEF) continue;
2627 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002628 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002629 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002630 return false;
2631 }
2632
2633 // Upper quadword shuffled.
2634 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002635 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002636 if (Arg.getOpcode() == ISD::UNDEF) continue;
2637 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002638 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002639 if (Val < 4 || Val > 7)
2640 return false;
2641 }
2642
2643 return true;
2644}
2645
Chris Lattnere6aa3862007-11-25 00:24:49 +00002646/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002647/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002648static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2649 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002650 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002651 MVT VT = Op.getValueType();
2652 MVT MaskVT = Mask.getValueType();
2653 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002654 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002655 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002656
2657 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002658 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002659 if (Arg.getOpcode() == ISD::UNDEF) {
2660 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2661 continue;
2662 }
2663 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002664 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665 if (Val < NumElems)
2666 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2667 else
2668 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2669 }
2670
2671 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002672 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002673 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2674}
2675
Evan Chenga6769df2007-12-07 21:30:01 +00002676/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2677/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002678static
Dan Gohman8181bd12008-07-27 21:46:04 +00002679SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002680 MVT MaskVT = Mask.getValueType();
2681 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002682 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002683 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002684 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002685 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002686 if (Arg.getOpcode() == ISD::UNDEF) {
2687 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2688 continue;
2689 }
2690 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002691 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002692 if (Val < NumElems)
2693 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2694 else
2695 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2696 }
2697 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2698}
2699
2700
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002701/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2702/// match movhlps. The lower half elements should come from upper half of
2703/// V1 (and in order), and the upper half elements should come from the upper
2704/// half of V2 (and in order).
2705static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2706 unsigned NumElems = Mask->getNumOperands();
2707 if (NumElems != 4)
2708 return false;
2709 for (unsigned i = 0, e = 2; i != e; ++i)
2710 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2711 return false;
2712 for (unsigned i = 2; i != 4; ++i)
2713 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2714 return false;
2715 return true;
2716}
2717
2718/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002719/// is promoted to a vector. It also returns the LoadSDNode by reference if
2720/// required.
2721static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002722 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2723 return false;
2724 N = N->getOperand(0).getNode();
2725 if (!ISD::isNON_EXTLoad(N))
2726 return false;
2727 if (LD)
2728 *LD = cast<LoadSDNode>(N);
2729 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002730}
2731
2732/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2733/// match movlp{s|d}. The lower half elements should come from lower half of
2734/// V1 (and in order), and the upper half elements should come from the upper
2735/// half of V2 (and in order). And since V1 will become the source of the
2736/// MOVLP, it must be either a vector load or a scalar load to vector.
2737static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2738 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2739 return false;
2740 // Is V2 is a vector load, don't do this transformation. We will try to use
2741 // load folding shufps op.
2742 if (ISD::isNON_EXTLoad(V2))
2743 return false;
2744
2745 unsigned NumElems = Mask->getNumOperands();
2746 if (NumElems != 2 && NumElems != 4)
2747 return false;
2748 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2749 if (!isUndefOrEqual(Mask->getOperand(i), i))
2750 return false;
2751 for (unsigned i = NumElems/2; i != NumElems; ++i)
2752 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2753 return false;
2754 return true;
2755}
2756
2757/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2758/// all the same.
2759static bool isSplatVector(SDNode *N) {
2760 if (N->getOpcode() != ISD::BUILD_VECTOR)
2761 return false;
2762
Dan Gohman8181bd12008-07-27 21:46:04 +00002763 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002764 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2765 if (N->getOperand(i) != SplatValue)
2766 return false;
2767 return true;
2768}
2769
2770/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2771/// to an undef.
2772static bool isUndefShuffle(SDNode *N) {
2773 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2774 return false;
2775
Dan Gohman8181bd12008-07-27 21:46:04 +00002776 SDValue V1 = N->getOperand(0);
2777 SDValue V2 = N->getOperand(1);
2778 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002779 unsigned NumElems = Mask.getNumOperands();
2780 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002781 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002782 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002783 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002784 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2785 return false;
2786 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2787 return false;
2788 }
2789 }
2790 return true;
2791}
2792
2793/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2794/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002795static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002796 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002797 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002798 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002799 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002800}
2801
2802/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2803/// to an zero vector.
2804static bool isZeroShuffle(SDNode *N) {
2805 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2806 return false;
2807
Dan Gohman8181bd12008-07-27 21:46:04 +00002808 SDValue V1 = N->getOperand(0);
2809 SDValue V2 = N->getOperand(1);
2810 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002811 unsigned NumElems = Mask.getNumOperands();
2812 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002813 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002814 if (Arg.getOpcode() == ISD::UNDEF)
2815 continue;
2816
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002817 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002818 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002819 unsigned Opc = V1.getNode()->getOpcode();
2820 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002821 continue;
2822 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002823 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002824 return false;
2825 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002826 unsigned Opc = V2.getNode()->getOpcode();
2827 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002828 continue;
2829 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002830 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002831 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002832 }
2833 }
2834 return true;
2835}
2836
2837/// getZeroVector - Returns a vector of specified type with all zero elements.
2838///
Dan Gohman8181bd12008-07-27 21:46:04 +00002839static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002840 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002841
2842 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2843 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002844 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002845 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002846 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002847 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002848 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002849 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002850 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002851 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002852 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002853 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2854 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002855 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002856}
2857
Chris Lattnere6aa3862007-11-25 00:24:49 +00002858/// getOnesVector - Returns a vector of specified type with all bits set.
2859///
Dan Gohman8181bd12008-07-27 21:46:04 +00002860static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002861 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002862
2863 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2864 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002865 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2866 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002867 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002868 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2869 else // SSE
2870 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2871 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2872}
2873
2874
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002875/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2876/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002877static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002878 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2879
2880 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002881 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002882 unsigned NumElems = Mask.getNumOperands();
2883 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002884 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002885 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002886 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002887 if (Val > NumElems) {
2888 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2889 Changed = true;
2890 }
2891 }
2892 MaskVec.push_back(Arg);
2893 }
2894
2895 if (Changed)
2896 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2897 &MaskVec[0], MaskVec.size());
2898 return Mask;
2899}
2900
2901/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2902/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002903static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002904 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2905 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002906
Dan Gohman8181bd12008-07-27 21:46:04 +00002907 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002908 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2909 for (unsigned i = 1; i != NumElems; ++i)
2910 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2911 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2912}
2913
2914/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2915/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002916static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002917 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2918 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002919 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002920 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2921 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2922 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2923 }
2924 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2925}
2926
2927/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2928/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002929static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002930 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2931 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002932 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002933 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002934 for (unsigned i = 0; i != Half; ++i) {
2935 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2936 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2937 }
2938 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2939}
2940
Chris Lattner2d91b962008-03-09 01:05:04 +00002941/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2942/// element #0 of a vector with the specified index, leaving the rest of the
2943/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002944static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002945 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002946 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2947 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002948 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002949 // Element #0 of the result gets the elt we are replacing.
2950 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2951 for (unsigned i = 1; i != NumElems; ++i)
2952 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2953 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2954}
2955
Evan Chengbf8b2c52008-04-05 00:30:36 +00002956/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002957static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002958 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2959 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002960 if (PVT == VT)
2961 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002962 SDValue V1 = Op.getOperand(0);
2963 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002964 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002965 // Special handling of v4f32 -> v4i32.
2966 if (VT != MVT::v4f32) {
2967 Mask = getUnpacklMask(NumElems, DAG);
2968 while (NumElems > 4) {
2969 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2970 NumElems >>= 1;
2971 }
Evan Cheng8c590372008-05-15 08:39:06 +00002972 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002973 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002974
Evan Chengbf8b2c52008-04-05 00:30:36 +00002975 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002976 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002977 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002978 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2979}
2980
Evan Chenga2497eb2008-09-25 20:50:48 +00002981/// isVectorLoad - Returns true if the node is a vector load, a scalar
2982/// load that's promoted to vector, or a load bitcasted.
2983static bool isVectorLoad(SDValue Op) {
2984 assert(Op.getValueType().isVector() && "Expected a vector type");
2985 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
2986 Op.getOpcode() == ISD::BIT_CONVERT) {
2987 return isa<LoadSDNode>(Op.getOperand(0));
2988 }
2989 return isa<LoadSDNode>(Op);
2990}
2991
2992
2993/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
2994///
2995static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
2996 SelectionDAG &DAG, bool HasSSE3) {
2997 // If we have sse3 and shuffle has more than one use or input is a load, then
2998 // use movddup. Otherwise, use movlhps.
2999 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3000 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3001 MVT VT = Op.getValueType();
3002 if (VT == PVT)
3003 return Op;
3004 unsigned NumElems = PVT.getVectorNumElements();
3005 if (NumElems == 2) {
3006 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3007 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
3008 } else {
3009 assert(NumElems == 4);
3010 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3011 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
3012 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst0, Cst1, Cst0, Cst1);
3013 }
3014
3015 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
3016 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
3017 DAG.getNode(ISD::UNDEF, PVT), Mask);
3018 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3019}
3020
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003021/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003022/// vector of zero or undef vector. This produces a shuffle where the low
3023/// element of V2 is swizzled into the zero/undef vector, landing at element
3024/// 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 +00003025static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003026 bool isZero, bool HasSSE2,
3027 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003028 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003029 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00003030 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003031 unsigned NumElems = V2.getValueType().getVectorNumElements();
3032 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3033 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003034 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003035 for (unsigned i = 0; i != NumElems; ++i)
3036 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3037 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3038 else
3039 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003040 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003041 &MaskVec[0], MaskVec.size());
3042 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3043}
3044
Evan Chengdea99362008-05-29 08:22:04 +00003045/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3046/// a shuffle that is zero.
3047static
Dan Gohman8181bd12008-07-27 21:46:04 +00003048unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003049 unsigned NumElems, bool Low,
3050 SelectionDAG &DAG) {
3051 unsigned NumZeros = 0;
3052 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003053 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003054 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003055 if (Idx.getOpcode() == ISD::UNDEF) {
3056 ++NumZeros;
3057 continue;
3058 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003059 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3060 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003061 ++NumZeros;
3062 else
3063 break;
3064 }
3065 return NumZeros;
3066}
3067
3068/// isVectorShift - Returns true if the shuffle can be implemented as a
3069/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003070static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3071 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003072 unsigned NumElems = Mask.getNumOperands();
3073
3074 isLeft = true;
3075 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3076 if (!NumZeros) {
3077 isLeft = false;
3078 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3079 if (!NumZeros)
3080 return false;
3081 }
3082
3083 bool SeenV1 = false;
3084 bool SeenV2 = false;
3085 for (unsigned i = NumZeros; i < NumElems; ++i) {
3086 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003087 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003088 if (Idx.getOpcode() == ISD::UNDEF)
3089 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003090 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003091 if (Index < NumElems)
3092 SeenV1 = true;
3093 else {
3094 Index -= NumElems;
3095 SeenV2 = true;
3096 }
3097 if (Index != Val)
3098 return false;
3099 }
3100 if (SeenV1 && SeenV2)
3101 return false;
3102
3103 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3104 ShAmt = NumZeros;
3105 return true;
3106}
3107
3108
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003109/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3110///
Dan Gohman8181bd12008-07-27 21:46:04 +00003111static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003112 unsigned NumNonZero, unsigned NumZero,
3113 SelectionDAG &DAG, TargetLowering &TLI) {
3114 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003115 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003116
Dan Gohman8181bd12008-07-27 21:46:04 +00003117 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003118 bool First = true;
3119 for (unsigned i = 0; i < 16; ++i) {
3120 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3121 if (ThisIsNonZero && First) {
3122 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003123 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003124 else
3125 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3126 First = false;
3127 }
3128
3129 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003130 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003131 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3132 if (LastIsNonZero) {
3133 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3134 }
3135 if (ThisIsNonZero) {
3136 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3137 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3138 ThisElt, DAG.getConstant(8, MVT::i8));
3139 if (LastIsNonZero)
3140 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3141 } else
3142 ThisElt = LastElt;
3143
Gabor Greif1c80d112008-08-28 21:40:38 +00003144 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003145 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003146 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003147 }
3148 }
3149
3150 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3151}
3152
3153/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3154///
Dan Gohman8181bd12008-07-27 21:46:04 +00003155static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003156 unsigned NumNonZero, unsigned NumZero,
3157 SelectionDAG &DAG, TargetLowering &TLI) {
3158 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003159 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003160
Dan Gohman8181bd12008-07-27 21:46:04 +00003161 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003162 bool First = true;
3163 for (unsigned i = 0; i < 8; ++i) {
3164 bool isNonZero = (NonZeros & (1 << i)) != 0;
3165 if (isNonZero) {
3166 if (First) {
3167 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003168 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003169 else
3170 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3171 First = false;
3172 }
3173 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003174 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003175 }
3176 }
3177
3178 return V;
3179}
3180
Evan Chengdea99362008-05-29 08:22:04 +00003181/// getVShift - Return a vector logical shift node.
3182///
Dan Gohman8181bd12008-07-27 21:46:04 +00003183static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003184 unsigned NumBits, SelectionDAG &DAG,
3185 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003186 bool isMMX = VT.getSizeInBits() == 64;
3187 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003188 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3189 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3190 return DAG.getNode(ISD::BIT_CONVERT, VT,
3191 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003192 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003193}
3194
Dan Gohman8181bd12008-07-27 21:46:04 +00003195SDValue
3196X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003197 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003198 if (ISD::isBuildVectorAllZeros(Op.getNode())
3199 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003200 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3201 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3202 // eliminated on x86-32 hosts.
3203 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3204 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003205
Gabor Greif1c80d112008-08-28 21:40:38 +00003206 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003207 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003208 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003209 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003210
Duncan Sands92c43912008-06-06 12:08:01 +00003211 MVT VT = Op.getValueType();
3212 MVT EVT = VT.getVectorElementType();
3213 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003214
3215 unsigned NumElems = Op.getNumOperands();
3216 unsigned NumZero = 0;
3217 unsigned NumNonZero = 0;
3218 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003219 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003220 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003221 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003222 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003223 if (Elt.getOpcode() == ISD::UNDEF)
3224 continue;
3225 Values.insert(Elt);
3226 if (Elt.getOpcode() != ISD::Constant &&
3227 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003228 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003229 if (isZeroNode(Elt))
3230 NumZero++;
3231 else {
3232 NonZeros |= (1 << i);
3233 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003234 }
3235 }
3236
3237 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003238 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3239 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003240 }
3241
Chris Lattner66a4dda2008-03-09 05:42:06 +00003242 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003243 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003244 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003245 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003246
Chris Lattner2d91b962008-03-09 01:05:04 +00003247 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3248 // the value are obviously zero, truncate the value to i32 and do the
3249 // insertion that way. Only do this if the value is non-constant or if the
3250 // value is a constant being inserted into element 0. It is cheaper to do
3251 // a constant pool load than it is to do a movd + shuffle.
3252 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3253 (!IsAllConstants || Idx == 0)) {
3254 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3255 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003256 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3257 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003258
3259 // Truncate the value (which may itself be a constant) to i32, and
3260 // convert it to a vector with movd (S2V+shuffle to zero extend).
3261 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3262 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003263 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3264 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003265
3266 // Now we have our 32-bit value zero extended in the low element of
3267 // a vector. If Idx != 0, swizzle it into place.
3268 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003269 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003270 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3271 getSwapEltZeroMask(VecElts, Idx, DAG)
3272 };
3273 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3274 }
3275 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3276 }
3277 }
3278
Chris Lattnerac914892008-03-08 22:59:52 +00003279 // If we have a constant or non-constant insertion into the low element of
3280 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3281 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3282 // depending on what the source datatype is. Because we can only get here
3283 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3284 if (Idx == 0 &&
3285 // Don't do this for i64 values on x86-32.
3286 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003287 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003288 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003289 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3290 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003291 }
Evan Chengdea99362008-05-29 08:22:04 +00003292
3293 // Is it a vector logical left shift?
3294 if (NumElems == 2 && Idx == 1 &&
3295 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003296 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003297 return getVShift(true, VT,
3298 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3299 NumBits/2, DAG, *this);
3300 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003301
3302 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003303 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003304
Chris Lattnerac914892008-03-08 22:59:52 +00003305 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3306 // is a non-constant being inserted into an element other than the low one,
3307 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3308 // movd/movss) to move this into the low element, then shuffle it into
3309 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003310 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003311 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3312
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003313 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003314 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3315 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003316 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3317 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003318 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003319 for (unsigned i = 0; i < NumElems; i++)
3320 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003321 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003322 &MaskVec[0], MaskVec.size());
3323 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3324 DAG.getNode(ISD::UNDEF, VT), Mask);
3325 }
3326 }
3327
Chris Lattner66a4dda2008-03-09 05:42:06 +00003328 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3329 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003330 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003331
Dan Gohman21463242007-07-24 22:55:08 +00003332 // A vector full of immediates; various special cases are already
3333 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003334 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003335 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003336
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003337 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003338 if (EVTBits == 64) {
3339 if (NumNonZero == 1) {
3340 // One half is zero or undef.
3341 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003342 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003343 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003344 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3345 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003346 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003347 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003348 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003349
3350 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3351 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003352 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003353 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003354 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003355 }
3356
3357 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003358 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003359 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003360 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003361 }
3362
3363 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003364 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003365 V.resize(NumElems);
3366 if (NumElems == 4 && NumZero > 0) {
3367 for (unsigned i = 0; i < 4; ++i) {
3368 bool isZero = !(NonZeros & (1 << i));
3369 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003370 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003371 else
3372 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3373 }
3374
3375 for (unsigned i = 0; i < 2; ++i) {
3376 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3377 default: break;
3378 case 0:
3379 V[i] = V[i*2]; // Must be a zero vector.
3380 break;
3381 case 1:
3382 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3383 getMOVLMask(NumElems, DAG));
3384 break;
3385 case 2:
3386 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3387 getMOVLMask(NumElems, DAG));
3388 break;
3389 case 3:
3390 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3391 getUnpacklMask(NumElems, DAG));
3392 break;
3393 }
3394 }
3395
Duncan Sands92c43912008-06-06 12:08:01 +00003396 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3397 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003398 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003399 bool Reverse = (NonZeros & 0x3) == 2;
3400 for (unsigned i = 0; i < 2; ++i)
3401 if (Reverse)
3402 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3403 else
3404 MaskVec.push_back(DAG.getConstant(i, EVT));
3405 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3406 for (unsigned i = 0; i < 2; ++i)
3407 if (Reverse)
3408 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3409 else
3410 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003411 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003412 &MaskVec[0], MaskVec.size());
3413 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3414 }
3415
3416 if (Values.size() > 2) {
3417 // Expand into a number of unpckl*.
3418 // e.g. for v4f32
3419 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3420 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3421 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003422 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003423 for (unsigned i = 0; i < NumElems; ++i)
3424 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3425 NumElems >>= 1;
3426 while (NumElems != 0) {
3427 for (unsigned i = 0; i < NumElems; ++i)
3428 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3429 UnpckMask);
3430 NumElems >>= 1;
3431 }
3432 return V[0];
3433 }
3434
Dan Gohman8181bd12008-07-27 21:46:04 +00003435 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003436}
3437
Evan Chengfca29242007-12-07 08:07:39 +00003438static
Dan Gohman8181bd12008-07-27 21:46:04 +00003439SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003440 SDValue PermMask, SelectionDAG &DAG,
3441 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003442 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003443 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3444 MVT MaskEVT = MaskVT.getVectorElementType();
3445 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003446 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3447 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003448
3449 // First record which half of which vector the low elements come from.
3450 SmallVector<unsigned, 4> LowQuad(4);
3451 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003452 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003453 if (Elt.getOpcode() == ISD::UNDEF)
3454 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003455 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003456 int QuadIdx = EltIdx / 4;
3457 ++LowQuad[QuadIdx];
3458 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003459
Evan Cheng75184a92007-12-11 01:46:18 +00003460 int BestLowQuad = -1;
3461 unsigned MaxQuad = 1;
3462 for (unsigned i = 0; i < 4; ++i) {
3463 if (LowQuad[i] > MaxQuad) {
3464 BestLowQuad = i;
3465 MaxQuad = LowQuad[i];
3466 }
Evan Chengfca29242007-12-07 08:07:39 +00003467 }
3468
Evan Cheng75184a92007-12-11 01:46:18 +00003469 // Record which half of which vector the high elements come from.
3470 SmallVector<unsigned, 4> HighQuad(4);
3471 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003472 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003473 if (Elt.getOpcode() == ISD::UNDEF)
3474 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003475 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003476 int QuadIdx = EltIdx / 4;
3477 ++HighQuad[QuadIdx];
3478 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003479
Evan Cheng75184a92007-12-11 01:46:18 +00003480 int BestHighQuad = -1;
3481 MaxQuad = 1;
3482 for (unsigned i = 0; i < 4; ++i) {
3483 if (HighQuad[i] > MaxQuad) {
3484 BestHighQuad = i;
3485 MaxQuad = HighQuad[i];
3486 }
3487 }
3488
3489 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3490 if (BestLowQuad != -1 || BestHighQuad != -1) {
3491 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003492 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003493
Evan Cheng75184a92007-12-11 01:46:18 +00003494 if (BestLowQuad != -1)
3495 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3496 else
3497 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003498
Evan Cheng75184a92007-12-11 01:46:18 +00003499 if (BestHighQuad != -1)
3500 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3501 else
3502 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003503
Dan Gohman8181bd12008-07-27 21:46:04 +00003504 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003505 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3506 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3507 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3508 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3509
3510 // Now sort high and low parts separately.
3511 BitVector InOrder(8);
3512 if (BestLowQuad != -1) {
3513 // Sort lower half in order using PSHUFLW.
3514 MaskVec.clear();
3515 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003516
Evan Cheng75184a92007-12-11 01:46:18 +00003517 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003518 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003519 if (Elt.getOpcode() == ISD::UNDEF) {
3520 MaskVec.push_back(Elt);
3521 InOrder.set(i);
3522 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003523 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003524 if (EltIdx != i)
3525 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003526
Evan Cheng75184a92007-12-11 01:46:18 +00003527 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003528
Evan Cheng75184a92007-12-11 01:46:18 +00003529 // If this element is in the right place after this shuffle, then
3530 // remember it.
3531 if ((int)(EltIdx / 4) == BestLowQuad)
3532 InOrder.set(i);
3533 }
3534 }
3535 if (AnyOutOrder) {
3536 for (unsigned i = 4; i != 8; ++i)
3537 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003538 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003539 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3540 }
3541 }
3542
3543 if (BestHighQuad != -1) {
3544 // Sort high half in order using PSHUFHW if possible.
3545 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003546
Evan Cheng75184a92007-12-11 01:46:18 +00003547 for (unsigned i = 0; i != 4; ++i)
3548 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003549
Evan Cheng75184a92007-12-11 01:46:18 +00003550 bool AnyOutOrder = false;
3551 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003552 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003553 if (Elt.getOpcode() == ISD::UNDEF) {
3554 MaskVec.push_back(Elt);
3555 InOrder.set(i);
3556 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003557 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003558 if (EltIdx != i)
3559 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003560
Evan Cheng75184a92007-12-11 01:46:18 +00003561 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003562
Evan Cheng75184a92007-12-11 01:46:18 +00003563 // If this element is in the right place after this shuffle, then
3564 // remember it.
3565 if ((int)(EltIdx / 4) == BestHighQuad)
3566 InOrder.set(i);
3567 }
3568 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003569
Evan Cheng75184a92007-12-11 01:46:18 +00003570 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003571 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003572 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3573 }
3574 }
3575
3576 // The other elements are put in the right place using pextrw and pinsrw.
3577 for (unsigned i = 0; i != 8; ++i) {
3578 if (InOrder[i])
3579 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003580 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003581 if (Elt.getOpcode() == ISD::UNDEF)
3582 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003583 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003584 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003585 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3586 DAG.getConstant(EltIdx, PtrVT))
3587 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3588 DAG.getConstant(EltIdx - 8, PtrVT));
3589 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3590 DAG.getConstant(i, PtrVT));
3591 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003592
Evan Cheng75184a92007-12-11 01:46:18 +00003593 return NewV;
3594 }
3595
Bill Wendling2c7cd592008-08-21 22:35:37 +00003596 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3597 // few as possible. First, let's find out how many elements are already in the
3598 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003599 unsigned V1InOrder = 0;
3600 unsigned V1FromV1 = 0;
3601 unsigned V2InOrder = 0;
3602 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003603 SmallVector<SDValue, 8> V1Elts;
3604 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003605 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003606 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003607 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003608 V1Elts.push_back(Elt);
3609 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003610 ++V1InOrder;
3611 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003612 continue;
3613 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003614 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003615 if (EltIdx == i) {
3616 V1Elts.push_back(Elt);
3617 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3618 ++V1InOrder;
3619 } else if (EltIdx == i+8) {
3620 V1Elts.push_back(Elt);
3621 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3622 ++V2InOrder;
3623 } else if (EltIdx < 8) {
3624 V1Elts.push_back(Elt);
3625 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003626 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003627 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3628 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003629 }
3630 }
3631
3632 if (V2InOrder > V1InOrder) {
3633 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3634 std::swap(V1, V2);
3635 std::swap(V1Elts, V2Elts);
3636 std::swap(V1FromV1, V2FromV2);
3637 }
3638
Evan Cheng75184a92007-12-11 01:46:18 +00003639 if ((V1FromV1 + V1InOrder) != 8) {
3640 // Some elements are from V2.
3641 if (V1FromV1) {
3642 // If there are elements that are from V1 but out of place,
3643 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003644 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003645 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003646 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003647 if (Elt.getOpcode() == ISD::UNDEF) {
3648 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3649 continue;
3650 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003651 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003652 if (EltIdx >= 8)
3653 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3654 else
3655 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3656 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003657 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003658 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003659 }
Evan Cheng75184a92007-12-11 01:46:18 +00003660
3661 NewV = V1;
3662 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003663 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003664 if (Elt.getOpcode() == ISD::UNDEF)
3665 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003666 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003667 if (EltIdx < 8)
3668 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003669 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003670 DAG.getConstant(EltIdx - 8, PtrVT));
3671 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3672 DAG.getConstant(i, PtrVT));
3673 }
3674 return NewV;
3675 } else {
3676 // All elements are from V1.
3677 NewV = V1;
3678 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003679 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003680 if (Elt.getOpcode() == ISD::UNDEF)
3681 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003682 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003683 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003684 DAG.getConstant(EltIdx, PtrVT));
3685 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3686 DAG.getConstant(i, PtrVT));
3687 }
3688 return NewV;
3689 }
3690}
3691
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003692/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3693/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3694/// done when every pair / quad of shuffle mask elements point to elements in
3695/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003696/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3697static
Dan Gohman8181bd12008-07-27 21:46:04 +00003698SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003699 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003700 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003701 TargetLowering &TLI) {
3702 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003703 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003704 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003705 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003706 MVT NewVT = MaskVT;
3707 switch (VT.getSimpleVT()) {
3708 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003709 case MVT::v4f32: NewVT = MVT::v2f64; break;
3710 case MVT::v4i32: NewVT = MVT::v2i64; break;
3711 case MVT::v8i16: NewVT = MVT::v4i32; break;
3712 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003713 }
3714
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003715 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003716 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003717 NewVT = MVT::v2i64;
3718 else
3719 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003720 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003721 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003722 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003723 for (unsigned i = 0; i < NumElems; i += Scale) {
3724 unsigned StartIdx = ~0U;
3725 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003726 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003727 if (Elt.getOpcode() == ISD::UNDEF)
3728 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003729 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003730 if (StartIdx == ~0U)
3731 StartIdx = EltIdx - (EltIdx % Scale);
3732 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003733 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003734 }
3735 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003736 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003737 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003738 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003739 }
3740
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003741 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3742 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3743 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3744 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3745 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003746}
3747
Evan Chenge9b9c672008-05-09 21:53:03 +00003748/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003749///
Dan Gohman8181bd12008-07-27 21:46:04 +00003750static SDValue getVZextMovL(MVT VT, MVT OpVT,
3751 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003752 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003753 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3754 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003755 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003756 LD = dyn_cast<LoadSDNode>(SrcOp);
3757 if (!LD) {
3758 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3759 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003760 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003761 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3762 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3763 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3764 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3765 // PR2108
3766 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3767 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003768 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003769 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003770 SrcOp.getOperand(0)
3771 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003772 }
3773 }
3774 }
3775
3776 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003777 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003778 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3779}
3780
Evan Chengf50554e2008-07-22 21:13:36 +00003781/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3782/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003783static SDValue
3784LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3785 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003786 MVT MaskVT = PermMask.getValueType();
3787 MVT MaskEVT = MaskVT.getVectorElementType();
3788 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003789 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003790 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003791 unsigned NumHi = 0;
3792 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003793 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003794 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003795 if (Elt.getOpcode() == ISD::UNDEF) {
3796 Locs[i] = std::make_pair(-1, -1);
3797 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003798 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003799 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003800 if (Val < 4) {
3801 Locs[i] = std::make_pair(0, NumLo);
3802 Mask1[NumLo] = Elt;
3803 NumLo++;
3804 } else {
3805 Locs[i] = std::make_pair(1, NumHi);
3806 if (2+NumHi < 4)
3807 Mask1[2+NumHi] = Elt;
3808 NumHi++;
3809 }
3810 }
3811 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003812
Evan Chengf50554e2008-07-22 21:13:36 +00003813 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003814 // If no more than two elements come from either vector. This can be
3815 // implemented with two shuffles. First shuffle gather the elements.
3816 // The second shuffle, which takes the first shuffle as both of its
3817 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003818 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3819 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3820 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003821
Dan Gohman8181bd12008-07-27 21:46:04 +00003822 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003823 for (unsigned i = 0; i != 4; ++i) {
3824 if (Locs[i].first == -1)
3825 continue;
3826 else {
3827 unsigned Idx = (i < 2) ? 0 : 4;
3828 Idx += Locs[i].first * 2 + Locs[i].second;
3829 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3830 }
3831 }
3832
3833 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3834 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3835 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003836 } else if (NumLo == 3 || NumHi == 3) {
3837 // Otherwise, we must have three elements from one vector, call it X, and
3838 // one element from the other, call it Y. First, use a shufps to build an
3839 // intermediate vector with the one element from Y and the element from X
3840 // that will be in the same half in the final destination (the indexes don't
3841 // matter). Then, use a shufps to build the final vector, taking the half
3842 // containing the element from Y from the intermediate, and the other half
3843 // from X.
3844 if (NumHi == 3) {
3845 // Normalize it so the 3 elements come from V1.
3846 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3847 std::swap(V1, V2);
3848 }
3849
3850 // Find the element from V2.
3851 unsigned HiIndex;
3852 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003853 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003854 if (Elt.getOpcode() == ISD::UNDEF)
3855 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003856 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003857 if (Val >= 4)
3858 break;
3859 }
3860
3861 Mask1[0] = PermMask.getOperand(HiIndex);
3862 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3863 Mask1[2] = PermMask.getOperand(HiIndex^1);
3864 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3865 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3866 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3867
3868 if (HiIndex >= 2) {
3869 Mask1[0] = PermMask.getOperand(0);
3870 Mask1[1] = PermMask.getOperand(1);
3871 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3872 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3873 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3874 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3875 } else {
3876 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3877 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3878 Mask1[2] = PermMask.getOperand(2);
3879 Mask1[3] = PermMask.getOperand(3);
3880 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003881 Mask1[2] =
3882 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3883 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003884 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003885 Mask1[3] =
3886 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3887 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003888 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3889 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3890 }
Evan Chengf50554e2008-07-22 21:13:36 +00003891 }
3892
3893 // Break it into (shuffle shuffle_hi, shuffle_lo).
3894 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003895 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3896 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3897 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003898 unsigned MaskIdx = 0;
3899 unsigned LoIdx = 0;
3900 unsigned HiIdx = 2;
3901 for (unsigned i = 0; i != 4; ++i) {
3902 if (i == 2) {
3903 MaskPtr = &HiMask;
3904 MaskIdx = 1;
3905 LoIdx = 0;
3906 HiIdx = 2;
3907 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003908 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003909 if (Elt.getOpcode() == ISD::UNDEF) {
3910 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003911 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003912 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3913 (*MaskPtr)[LoIdx] = Elt;
3914 LoIdx++;
3915 } else {
3916 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3917 (*MaskPtr)[HiIdx] = Elt;
3918 HiIdx++;
3919 }
3920 }
3921
Dan Gohman8181bd12008-07-27 21:46:04 +00003922 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003923 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3924 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003925 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003926 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3927 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003928 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003929 for (unsigned i = 0; i != 4; ++i) {
3930 if (Locs[i].first == -1) {
3931 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3932 } else {
3933 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3934 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3935 }
3936 }
3937 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3938 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3939 &MaskOps[0], MaskOps.size()));
3940}
3941
Dan Gohman8181bd12008-07-27 21:46:04 +00003942SDValue
3943X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3944 SDValue V1 = Op.getOperand(0);
3945 SDValue V2 = Op.getOperand(1);
3946 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003947 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003948 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003949 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003950 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3951 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3952 bool V1IsSplat = false;
3953 bool V2IsSplat = false;
3954
Gabor Greif1c80d112008-08-28 21:40:38 +00003955 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003956 return DAG.getNode(ISD::UNDEF, VT);
3957
Gabor Greif1c80d112008-08-28 21:40:38 +00003958 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003959 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003960
Gabor Greif1c80d112008-08-28 21:40:38 +00003961 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003962 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003963 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003964 return V2;
3965
Evan Chengae6c9212008-09-25 23:35:16 +00003966 // Canonicalize movddup shuffles.
3967 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Chengbdd9d9f2008-10-06 21:13:08 +00003968 VT.getSizeInBits() == 128 &&
Evan Chengae6c9212008-09-25 23:35:16 +00003969 X86::isMOVDDUPMask(PermMask.getNode()))
3970 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
3971
Gabor Greif1c80d112008-08-28 21:40:38 +00003972 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003973 if (isMMX || NumElems < 4) return Op;
3974 // Promote it to a v4{if}32 splat.
3975 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003976 }
3977
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003978 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3979 // do it!
3980 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003981 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003982 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003983 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3984 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3985 // FIXME: Figure out a cleaner way to do this.
3986 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00003987 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003988 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003989 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003990 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003991 SDValue NewV1 = NewOp.getOperand(0);
3992 SDValue NewV2 = NewOp.getOperand(1);
3993 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00003994 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003995 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003996 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003997 }
3998 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003999 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004000 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004001 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004002 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004003 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00004004 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004005 }
4006 }
4007
Evan Chengdea99362008-05-29 08:22:04 +00004008 // Check if this can be converted into a logical shift.
4009 bool isLeft = false;
4010 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004011 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00004012 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4013 if (isShift && ShVal.hasOneUse()) {
4014 // If the shifted value has multiple uses, it may be cheaper to use
4015 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004016 MVT EVT = VT.getVectorElementType();
4017 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004018 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4019 }
4020
Gabor Greif1c80d112008-08-28 21:40:38 +00004021 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004022 if (V1IsUndef)
4023 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004024 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004025 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004026 if (!isMMX)
4027 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004028 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004029
Gabor Greif1c80d112008-08-28 21:40:38 +00004030 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4031 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4032 X86::isMOVHLPSMask(PermMask.getNode()) ||
4033 X86::isMOVHPMask(PermMask.getNode()) ||
4034 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004035 return Op;
4036
Gabor Greif1c80d112008-08-28 21:40:38 +00004037 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4038 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004039 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4040
Evan Chengdea99362008-05-29 08:22:04 +00004041 if (isShift) {
4042 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004043 MVT EVT = VT.getVectorElementType();
4044 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004045 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4046 }
4047
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004048 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004049 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4050 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004051 V1IsSplat = isSplatVector(V1.getNode());
4052 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004053
4054 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004055 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4056 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4057 std::swap(V1IsSplat, V2IsSplat);
4058 std::swap(V1IsUndef, V2IsUndef);
4059 Commuted = true;
4060 }
4061
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004062 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004063 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004064 if (V2IsUndef) return V1;
4065 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4066 if (V2IsSplat) {
4067 // V2 is a splat, so the mask may be malformed. That is, it may point
4068 // to any V2 element. The instruction selectior won't like this. Get
4069 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004070 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004071 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004072 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4073 }
4074 return Op;
4075 }
4076
Gabor Greif1c80d112008-08-28 21:40:38 +00004077 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4078 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4079 X86::isUNPCKLMask(PermMask.getNode()) ||
4080 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004081 return Op;
4082
4083 if (V2IsSplat) {
4084 // Normalize mask so all entries that point to V2 points to its first
4085 // element then try to match unpck{h|l} again. If match, return a
4086 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004087 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004088 if (NewMask.getNode() != PermMask.getNode()) {
4089 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004090 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004091 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004092 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004093 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004094 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4095 }
4096 }
4097 }
4098
4099 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004100 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004101 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4102
4103 if (Commuted) {
4104 // Commute is back and try unpck* again.
4105 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004106 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4107 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4108 X86::isUNPCKLMask(PermMask.getNode()) ||
4109 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004110 return Op;
4111 }
4112
Evan Chengbf8b2c52008-04-05 00:30:36 +00004113 // Try PSHUF* first, then SHUFP*.
4114 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4115 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004116 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004117 if (V2.getOpcode() != ISD::UNDEF)
4118 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4119 DAG.getNode(ISD::UNDEF, VT), PermMask);
4120 return Op;
4121 }
4122
4123 if (!isMMX) {
4124 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004125 (X86::isPSHUFDMask(PermMask.getNode()) ||
4126 X86::isPSHUFHWMask(PermMask.getNode()) ||
4127 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004128 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004129 if (VT == MVT::v4f32) {
4130 RVT = MVT::v4i32;
4131 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4132 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4133 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4134 } else if (V2.getOpcode() != ISD::UNDEF)
4135 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4136 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4137 if (RVT != VT)
4138 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004139 return Op;
4140 }
4141
Evan Chengbf8b2c52008-04-05 00:30:36 +00004142 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004143 if (X86::isSHUFPMask(PermMask.getNode()) ||
4144 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004145 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004146 }
4147
Evan Cheng75184a92007-12-11 01:46:18 +00004148 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4149 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004150 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004151 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004152 return NewOp;
4153 }
4154
Evan Chengf50554e2008-07-22 21:13:36 +00004155 // Handle all 4 wide cases with a number of shuffles except for MMX.
4156 if (NumElems == 4 && !isMMX)
4157 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004158
Dan Gohman8181bd12008-07-27 21:46:04 +00004159 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004160}
4161
Dan Gohman8181bd12008-07-27 21:46:04 +00004162SDValue
4163X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004164 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004165 MVT VT = Op.getValueType();
4166 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004167 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004168 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004169 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004170 DAG.getValueType(VT));
4171 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004172 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004173 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004174 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004175 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004176 DAG.getValueType(VT));
4177 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004178 } else if (VT == MVT::f32) {
4179 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4180 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004181 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004182 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004183 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004184 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004185 if (User->getOpcode() != ISD::STORE &&
4186 (User->getOpcode() != ISD::BIT_CONVERT ||
4187 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004188 return SDValue();
4189 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004190 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4191 Op.getOperand(1));
4192 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004193 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004194 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004195}
4196
4197
Dan Gohman8181bd12008-07-27 21:46:04 +00004198SDValue
4199X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004200 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004201 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004202
Evan Cheng6c249332008-03-24 21:52:23 +00004203 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004204 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004205 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004206 return Res;
4207 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004208
Duncan Sands92c43912008-06-06 12:08:01 +00004209 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004210 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004211 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004212 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004213 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004214 if (Idx == 0)
4215 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4216 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4217 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4218 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004219 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004220 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004221 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004222 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004223 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004224 DAG.getValueType(VT));
4225 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004226 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004227 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004228 if (Idx == 0)
4229 return Op;
4230 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004231 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004232 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004233 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004234 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004235 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004236 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004237 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004238 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004239 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004240 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004241 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004242 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004243 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004244 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4245 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4246 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004247 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004248 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004249 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4250 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4251 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004252 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004253 if (Idx == 0)
4254 return Op;
4255
4256 // UNPCKHPD the element to the lowest double word, then movsd.
4257 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4258 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004259 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004260 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004261 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004262 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004263 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004264 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004265 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004266 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004267 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4268 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4269 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004270 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004271 }
4272
Dan Gohman8181bd12008-07-27 21:46:04 +00004273 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004274}
4275
Dan Gohman8181bd12008-07-27 21:46:04 +00004276SDValue
4277X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004278 MVT VT = Op.getValueType();
4279 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004280
Dan Gohman8181bd12008-07-27 21:46:04 +00004281 SDValue N0 = Op.getOperand(0);
4282 SDValue N1 = Op.getOperand(1);
4283 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004284
Dan Gohman5a7af042008-08-14 22:53:18 +00004285 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4286 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004287 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004288 : X86ISD::PINSRW;
4289 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4290 // argument.
4291 if (N1.getValueType() != MVT::i32)
4292 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4293 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004294 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begemand77e59e2008-02-11 04:19:36 +00004295 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004296 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004297 // Bits [7:6] of the constant are the source select. This will always be
4298 // zero here. The DAG Combiner may combine an extract_elt index into these
4299 // bits. For example (insert (extract, 3), 2) could be matched by putting
4300 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4301 // Bits [5:4] of the constant are the destination select. This is the
4302 // value of the incoming immediate.
4303 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4304 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004305 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begemand77e59e2008-02-11 04:19:36 +00004306 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4307 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004308 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004309}
4310
Dan Gohman8181bd12008-07-27 21:46:04 +00004311SDValue
4312X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004313 MVT VT = Op.getValueType();
4314 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004315
4316 if (Subtarget->hasSSE41())
4317 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4318
Evan Chenge12a7eb2007-12-12 07:55:34 +00004319 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004320 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004321
Dan Gohman8181bd12008-07-27 21:46:04 +00004322 SDValue N0 = Op.getOperand(0);
4323 SDValue N1 = Op.getOperand(1);
4324 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004325
Duncan Sands92c43912008-06-06 12:08:01 +00004326 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004327 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4328 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004329 if (N1.getValueType() != MVT::i32)
4330 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4331 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004332 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004333 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004334 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004335 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004336}
4337
Dan Gohman8181bd12008-07-27 21:46:04 +00004338SDValue
4339X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004340 if (Op.getValueType() == MVT::v2f32)
4341 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4342 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4343 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4344 Op.getOperand(0))));
4345
Dan Gohman8181bd12008-07-27 21:46:04 +00004346 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004347 MVT VT = MVT::v2i32;
4348 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004349 default: break;
4350 case MVT::v16i8:
4351 case MVT::v8i16:
4352 VT = MVT::v4i32;
4353 break;
4354 }
4355 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4356 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004357}
4358
Bill Wendlingfef06052008-09-16 21:48:12 +00004359// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4360// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4361// one of the above mentioned nodes. It has to be wrapped because otherwise
4362// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4363// be used to form addressing mode. These wrapped nodes will be selected
4364// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004365SDValue
4366X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004367 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004368 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004369 getPointerTy(),
4370 CP->getAlignment());
4371 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4372 // With PIC, the address is actually $g + Offset.
4373 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4374 !Subtarget->isPICStyleRIPRel()) {
4375 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4376 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4377 Result);
4378 }
4379
4380 return Result;
4381}
4382
Dan Gohman8181bd12008-07-27 21:46:04 +00004383SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004384X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
4385 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00004386 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004387 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4388 // With PIC, the address is actually $g + Offset.
4389 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4390 !Subtarget->isPICStyleRIPRel()) {
4391 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4392 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4393 Result);
4394 }
4395
4396 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4397 // load the value at address GV, not the value of GV itself. This means that
4398 // the GlobalAddress must be in the base or index register of the address, not
4399 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4400 // The same applies for external symbols during PIC codegen
4401 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004402 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004403 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004404
4405 return Result;
4406}
4407
Evan Cheng7f250d62008-09-24 00:05:32 +00004408SDValue
4409X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4410 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4411 return LowerGlobalAddress(GV, DAG);
4412}
4413
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004414// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004415static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004416LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004417 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004418 SDValue InFlag;
4419 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004420 DAG.getNode(X86ISD::GlobalBaseReg,
4421 PtrVT), InFlag);
4422 InFlag = Chain.getValue(1);
4423
4424 // emit leal symbol@TLSGD(,%ebx,1), %eax
4425 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004426 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004427 GA->getValueType(0),
4428 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004429 SDValue Ops[] = { Chain, TGA, InFlag };
4430 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004431 InFlag = Result.getValue(2);
4432 Chain = Result.getValue(1);
4433
4434 // call ___tls_get_addr. This function receives its argument in
4435 // the register EAX.
4436 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4437 InFlag = Chain.getValue(1);
4438
4439 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004440 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004441 DAG.getTargetExternalSymbol("___tls_get_addr",
4442 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004443 DAG.getRegister(X86::EAX, PtrVT),
4444 DAG.getRegister(X86::EBX, PtrVT),
4445 InFlag };
4446 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4447 InFlag = Chain.getValue(1);
4448
4449 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4450}
4451
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004452// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004453static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004454LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004455 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004456 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004457
4458 // emit leaq symbol@TLSGD(%rip), %rdi
4459 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004460 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004461 GA->getValueType(0),
4462 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004463 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4464 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004465 Chain = Result.getValue(1);
4466 InFlag = Result.getValue(2);
4467
aslb204cd52008-08-16 12:58:29 +00004468 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004469 // the register RDI.
4470 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4471 InFlag = Chain.getValue(1);
4472
4473 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004474 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004475 DAG.getTargetExternalSymbol("__tls_get_addr",
4476 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004477 DAG.getRegister(X86::RDI, PtrVT),
4478 InFlag };
4479 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4480 InFlag = Chain.getValue(1);
4481
4482 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4483}
4484
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4486// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004487static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004488 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004489 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004490 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004491 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4492 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004493 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004494 GA->getValueType(0),
4495 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004496 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004497
4498 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004499 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004500 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004501
4502 // The address of the thread local variable is the add of the thread
4503 // pointer with the offset of the variable.
4504 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4505}
4506
Dan Gohman8181bd12008-07-27 21:46:04 +00004507SDValue
4508X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004509 // TODO: implement the "local dynamic" model
4510 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004511 assert(Subtarget->isTargetELF() &&
4512 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004513 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4514 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4515 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004516 if (Subtarget->is64Bit()) {
4517 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4518 } else {
4519 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4520 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4521 else
4522 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4523 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004524}
4525
Dan Gohman8181bd12008-07-27 21:46:04 +00004526SDValue
4527X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004528 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4529 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004530 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4531 // With PIC, the address is actually $g + Offset.
4532 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4533 !Subtarget->isPICStyleRIPRel()) {
4534 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4535 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4536 Result);
4537 }
4538
4539 return Result;
4540}
4541
Dan Gohman8181bd12008-07-27 21:46:04 +00004542SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004543 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004544 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004545 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4546 // With PIC, the address is actually $g + Offset.
4547 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4548 !Subtarget->isPICStyleRIPRel()) {
4549 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4550 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4551 Result);
4552 }
4553
4554 return Result;
4555}
4556
Chris Lattner62814a32007-10-17 06:02:13 +00004557/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4558/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004559SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004560 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004561 MVT VT = Op.getValueType();
4562 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004563 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004564 SDValue ShOpLo = Op.getOperand(0);
4565 SDValue ShOpHi = Op.getOperand(1);
4566 SDValue ShAmt = Op.getOperand(2);
4567 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004568 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4569 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004570
Dan Gohman8181bd12008-07-27 21:46:04 +00004571 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004572 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004573 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4574 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004575 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004576 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4577 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004578 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004579
Dan Gohman8181bd12008-07-27 21:46:04 +00004580 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004581 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004582 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004583 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004584
Dan Gohman8181bd12008-07-27 21:46:04 +00004585 SDValue Hi, Lo;
4586 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4587 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4588 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004589
Chris Lattner62814a32007-10-17 06:02:13 +00004590 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004591 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4592 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004593 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004594 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4595 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004596 }
4597
Dan Gohman8181bd12008-07-27 21:46:04 +00004598 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004599 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004600}
4601
Dan Gohman8181bd12008-07-27 21:46:04 +00004602SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004603 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004604 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004605 "Unknown SINT_TO_FP to lower!");
4606
4607 // These are really Legal; caller falls through into that case.
4608 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004609 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004610 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4611 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004612 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004613
Duncan Sands92c43912008-06-06 12:08:01 +00004614 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004615 MachineFunction &MF = DAG.getMachineFunction();
4616 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004617 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4618 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004619 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004620 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004621
4622 // Build the FILD
4623 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004624 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004625 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004626 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4627 else
4628 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004629 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004630 Ops.push_back(Chain);
4631 Ops.push_back(StackSlot);
4632 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004633 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004634 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004635
Dale Johannesen2fc20782007-09-14 22:26:36 +00004636 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004637 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004638 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004639
4640 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4641 // shouldn't be necessary except that RFP cannot be live across
4642 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4643 MachineFunction &MF = DAG.getMachineFunction();
4644 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004645 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004646 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004647 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004648 Ops.push_back(Chain);
4649 Ops.push_back(Result);
4650 Ops.push_back(StackSlot);
4651 Ops.push_back(DAG.getValueType(Op.getValueType()));
4652 Ops.push_back(InFlag);
4653 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004654 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004655 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004656 }
4657
4658 return Result;
4659}
4660
Dan Gohman8181bd12008-07-27 21:46:04 +00004661std::pair<SDValue,SDValue> X86TargetLowering::
4662FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004663 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4664 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004665 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004666
Dale Johannesen2fc20782007-09-14 22:26:36 +00004667 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004668 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004669 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004670 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004671 if (Subtarget->is64Bit() &&
4672 Op.getValueType() == MVT::i64 &&
4673 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004674 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004675
Evan Cheng05441e62007-10-15 20:11:21 +00004676 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4677 // stack slot.
4678 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004679 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004680 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004681 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004682 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004683 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004684 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4685 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4686 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4687 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004688 }
4689
Dan Gohman8181bd12008-07-27 21:46:04 +00004690 SDValue Chain = DAG.getEntryNode();
4691 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004692 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004693 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004694 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004695 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004696 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004697 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004698 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4699 };
4700 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4701 Chain = Value.getValue(1);
4702 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4703 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4704 }
4705
4706 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004707 SDValue Ops[] = { Chain, Value, StackSlot };
4708 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004709
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004710 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004711}
4712
Dan Gohman8181bd12008-07-27 21:46:04 +00004713SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4714 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4715 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004716 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004717
4718 // Load the result.
4719 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4720}
4721
4722SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004723 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4724 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004725 if (FIST.getNode() == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004726
4727 MVT VT = N->getValueType(0);
4728
4729 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004730 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004731
Duncan Sands698842f2008-07-02 17:40:58 +00004732 // Use MERGE_VALUES to drop the chain result value and get a node with one
4733 // result. This requires turning off getMergeValues simplification, since
4734 // otherwise it will give us Res back.
Gabor Greif1c80d112008-08-28 21:40:38 +00004735 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf19591c2008-06-30 10:19:09 +00004736}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004737
Dan Gohman8181bd12008-07-27 21:46:04 +00004738SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004739 MVT VT = Op.getValueType();
4740 MVT EltVT = VT;
4741 if (VT.isVector())
4742 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004743 std::vector<Constant*> CV;
4744 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004745 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004746 CV.push_back(C);
4747 CV.push_back(C);
4748 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004749 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004750 CV.push_back(C);
4751 CV.push_back(C);
4752 CV.push_back(C);
4753 CV.push_back(C);
4754 }
Dan Gohman11821702007-07-27 17:16:43 +00004755 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004756 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4757 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004758 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004759 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004760 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4761}
4762
Dan Gohman8181bd12008-07-27 21:46:04 +00004763SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004764 MVT VT = Op.getValueType();
4765 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004766 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004767 if (VT.isVector()) {
4768 EltVT = VT.getVectorElementType();
4769 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004770 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004771 std::vector<Constant*> CV;
4772 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004773 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004774 CV.push_back(C);
4775 CV.push_back(C);
4776 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004777 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004778 CV.push_back(C);
4779 CV.push_back(C);
4780 CV.push_back(C);
4781 CV.push_back(C);
4782 }
Dan Gohman11821702007-07-27 17:16:43 +00004783 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004784 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4785 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004786 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004787 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004788 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004789 return DAG.getNode(ISD::BIT_CONVERT, VT,
4790 DAG.getNode(ISD::XOR, MVT::v2i64,
4791 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4792 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4793 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004794 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4795 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004796}
4797
Dan Gohman8181bd12008-07-27 21:46:04 +00004798SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4799 SDValue Op0 = Op.getOperand(0);
4800 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004801 MVT VT = Op.getValueType();
4802 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004803
4804 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004805 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004806 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4807 SrcVT = VT;
4808 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004809 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004810 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004811 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004812 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004813 }
4814
4815 // At this point the operands and the result should have the same
4816 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004817
4818 // First get the sign bit of second operand.
4819 std::vector<Constant*> CV;
4820 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004821 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4822 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004823 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004824 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4825 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4826 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4827 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004828 }
Dan Gohman11821702007-07-27 17:16:43 +00004829 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004830 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4831 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004832 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004833 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004834 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004835
4836 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004837 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004838 // Op0 is MVT::f32, Op1 is MVT::f64.
4839 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4840 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4841 DAG.getConstant(32, MVT::i32));
4842 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4843 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004844 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004845 }
4846
4847 // Clear first operand sign bit.
4848 CV.clear();
4849 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004850 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4851 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004852 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004853 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4854 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4855 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4856 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004857 }
Dan Gohman11821702007-07-27 17:16:43 +00004858 C = ConstantVector::get(CV);
4859 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004860 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004861 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004862 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004863 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004864
4865 // Or the value with the sign bit.
4866 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4867}
4868
Dan Gohman8181bd12008-07-27 21:46:04 +00004869SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004870 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004871 SDValue Cond;
4872 SDValue Op0 = Op.getOperand(0);
4873 SDValue Op1 = Op.getOperand(1);
4874 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004875 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004876 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004877 unsigned X86CC;
4878
Evan Cheng950aac02007-09-25 01:57:46 +00004879 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004880 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004881 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4882 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004883 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004884 }
Evan Cheng950aac02007-09-25 01:57:46 +00004885
4886 assert(isFP && "Illegal integer SetCC!");
4887
Evan Cheng621216e2007-09-29 00:00:36 +00004888 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004889 switch (SetCCOpcode) {
4890 default: assert(false && "Illegal floating point SetCC!");
4891 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004892 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004893 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004894 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004895 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4896 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4897 }
4898 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004899 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004900 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004901 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004902 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4903 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4904 }
4905 }
4906}
4907
Dan Gohman8181bd12008-07-27 21:46:04 +00004908SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4909 SDValue Cond;
4910 SDValue Op0 = Op.getOperand(0);
4911 SDValue Op1 = Op.getOperand(1);
4912 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004913 MVT VT = Op.getValueType();
4914 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4915 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4916
4917 if (isFP) {
4918 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004919 MVT VT0 = Op0.getValueType();
4920 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4921 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004922 bool Swap = false;
4923
4924 switch (SetCCOpcode) {
4925 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004926 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004927 case ISD::SETEQ: SSECC = 0; break;
4928 case ISD::SETOGT:
4929 case ISD::SETGT: Swap = true; // Fallthrough
4930 case ISD::SETLT:
4931 case ISD::SETOLT: SSECC = 1; break;
4932 case ISD::SETOGE:
4933 case ISD::SETGE: Swap = true; // Fallthrough
4934 case ISD::SETLE:
4935 case ISD::SETOLE: SSECC = 2; break;
4936 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004937 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004938 case ISD::SETNE: SSECC = 4; break;
4939 case ISD::SETULE: Swap = true;
4940 case ISD::SETUGE: SSECC = 5; break;
4941 case ISD::SETULT: Swap = true;
4942 case ISD::SETUGT: SSECC = 6; break;
4943 case ISD::SETO: SSECC = 7; break;
4944 }
4945 if (Swap)
4946 std::swap(Op0, Op1);
4947
Nate Begeman6357f9d2008-07-25 19:05:58 +00004948 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004949 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004950 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004951 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004952 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4953 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4954 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4955 }
4956 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004957 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004958 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4959 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4960 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4961 }
4962 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004963 }
4964 // Handle all other FP comparisons here.
4965 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4966 }
4967
4968 // We are handling one of the integer comparisons here. Since SSE only has
4969 // GT and EQ comparisons for integer, swapping operands and multiple
4970 // operations may be required for some comparisons.
4971 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4972 bool Swap = false, Invert = false, FlipSigns = false;
4973
4974 switch (VT.getSimpleVT()) {
4975 default: break;
4976 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4977 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4978 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4979 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4980 }
4981
4982 switch (SetCCOpcode) {
4983 default: break;
4984 case ISD::SETNE: Invert = true;
4985 case ISD::SETEQ: Opc = EQOpc; break;
4986 case ISD::SETLT: Swap = true;
4987 case ISD::SETGT: Opc = GTOpc; break;
4988 case ISD::SETGE: Swap = true;
4989 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4990 case ISD::SETULT: Swap = true;
4991 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4992 case ISD::SETUGE: Swap = true;
4993 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4994 }
4995 if (Swap)
4996 std::swap(Op0, Op1);
4997
4998 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4999 // bits of the inputs before performing those operations.
5000 if (FlipSigns) {
5001 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005002 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
5003 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
5004 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005005 SignBits.size());
5006 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
5007 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
5008 }
5009
Dan Gohman8181bd12008-07-27 21:46:04 +00005010 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005011
5012 // If the logical-not of the result is required, perform that now.
5013 if (Invert) {
5014 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005015 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
5016 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
5017 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005018 NegOnes.size());
5019 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
5020 }
5021 return Result;
5022}
Evan Cheng950aac02007-09-25 01:57:46 +00005023
Dan Gohman8181bd12008-07-27 21:46:04 +00005024SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005025 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005026 SDValue Cond = Op.getOperand(0);
5027 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005028
5029 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005030 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005031
Evan Cheng50d37ab2007-10-08 22:16:29 +00005032 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5033 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005034 if (Cond.getOpcode() == X86ISD::SETCC) {
5035 CC = Cond.getOperand(0);
5036
Dan Gohman8181bd12008-07-27 21:46:04 +00005037 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005038 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005039 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005040
Evan Cheng50d37ab2007-10-08 22:16:29 +00005041 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005042 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005043 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005044 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005045
Evan Cheng621216e2007-09-29 00:00:36 +00005046 if ((Opc == X86ISD::CMP ||
5047 Opc == X86ISD::COMI ||
5048 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005049 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005050 addTest = false;
5051 }
5052 }
5053
5054 if (addTest) {
5055 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00005056 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005057 }
5058
Duncan Sands92c43912008-06-06 12:08:01 +00005059 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005060 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005061 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005062 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5063 // condition is true.
5064 Ops.push_back(Op.getOperand(2));
5065 Ops.push_back(Op.getOperand(1));
5066 Ops.push_back(CC);
5067 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00005068 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005069}
5070
Dan Gohman8181bd12008-07-27 21:46:04 +00005071SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005072 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005073 SDValue Chain = Op.getOperand(0);
5074 SDValue Cond = Op.getOperand(1);
5075 SDValue Dest = Op.getOperand(2);
5076 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005077
5078 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005079 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005080
Evan Cheng50d37ab2007-10-08 22:16:29 +00005081 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5082 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005083 if (Cond.getOpcode() == X86ISD::SETCC) {
5084 CC = Cond.getOperand(0);
5085
Dan Gohman8181bd12008-07-27 21:46:04 +00005086 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005087 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00005088 if (Opc == X86ISD::CMP ||
5089 Opc == X86ISD::COMI ||
5090 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005091 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005092 addTest = false;
5093 }
5094 }
5095
5096 if (addTest) {
5097 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005098 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005099 }
Evan Cheng621216e2007-09-29 00:00:36 +00005100 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005101 Chain, Op.getOperand(2), CC, Cond);
5102}
5103
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005104
5105// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5106// Calls to _alloca is needed to probe the stack when allocating more than 4k
5107// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5108// that the guard pages used by the OS virtual memory manager are allocated in
5109// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005110SDValue
5111X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005112 SelectionDAG &DAG) {
5113 assert(Subtarget->isTargetCygMing() &&
5114 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005115
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005116 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005117 SDValue Chain = Op.getOperand(0);
5118 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005119 // FIXME: Ensure alignment here
5120
Dan Gohman8181bd12008-07-27 21:46:04 +00005121 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005122
Duncan Sands92c43912008-06-06 12:08:01 +00005123 MVT IntPtr = getPointerTy();
5124 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005125
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005126 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005127
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005128 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5129 Flag = Chain.getValue(1);
5130
5131 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005132 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005133 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005134 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005135 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005136 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005137 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005138 Flag = Chain.getValue(1);
5139
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005140 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005141 DAG.getIntPtrConstant(0, true),
5142 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005143 Flag);
5144
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005145 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005146
Dan Gohman8181bd12008-07-27 21:46:04 +00005147 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005148 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005149}
5150
Dan Gohman8181bd12008-07-27 21:46:04 +00005151SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005152X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005153 SDValue Chain,
5154 SDValue Dst, SDValue Src,
5155 SDValue Size, unsigned Align,
5156 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005157 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005158 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005159
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005160 // If not DWORD aligned or size is more than the threshold, call the library.
5161 // The libc version is likely to be faster for these cases. It can use the
5162 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005163 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005164 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005165 ConstantSize->getZExtValue() >
5166 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005167 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005168
5169 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005170 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005171
Bill Wendling4b2e3782008-10-01 00:59:58 +00005172 if (const char *bzeroEntry = V &&
5173 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5174 MVT IntPtr = getPointerTy();
5175 const Type *IntPtrTy = TD->getIntPtrType();
5176 TargetLowering::ArgListTy Args;
5177 TargetLowering::ArgListEntry Entry;
5178 Entry.Node = Dst;
5179 Entry.Ty = IntPtrTy;
5180 Args.push_back(Entry);
5181 Entry.Node = Size;
5182 Args.push_back(Entry);
5183 std::pair<SDValue,SDValue> CallResult =
5184 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5185 CallingConv::C, false,
5186 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG);
5187 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005188 }
5189
Dan Gohmane8b391e2008-04-12 04:36:06 +00005190 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005191 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005192 }
5193
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005194 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005195 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005196 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005197 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005198 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005199 unsigned BytesLeft = 0;
5200 bool TwoRepStos = false;
5201 if (ValC) {
5202 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005203 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005204
5205 // If the value is a constant, then we can potentially use larger sets.
5206 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005207 case 2: // WORD aligned
5208 AVT = MVT::i16;
5209 ValReg = X86::AX;
5210 Val = (Val << 8) | Val;
5211 break;
5212 case 0: // DWORD aligned
5213 AVT = MVT::i32;
5214 ValReg = X86::EAX;
5215 Val = (Val << 8) | Val;
5216 Val = (Val << 16) | Val;
5217 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5218 AVT = MVT::i64;
5219 ValReg = X86::RAX;
5220 Val = (Val << 32) | Val;
5221 }
5222 break;
5223 default: // Byte aligned
5224 AVT = MVT::i8;
5225 ValReg = X86::AL;
5226 Count = DAG.getIntPtrConstant(SizeVal);
5227 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005228 }
5229
Duncan Sandsec142ee2008-06-08 20:54:56 +00005230 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005231 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005232 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5233 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005234 }
5235
5236 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5237 InFlag);
5238 InFlag = Chain.getValue(1);
5239 } else {
5240 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005241 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005242 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005243 InFlag = Chain.getValue(1);
5244 }
5245
5246 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5247 Count, InFlag);
5248 InFlag = Chain.getValue(1);
5249 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005250 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005251 InFlag = Chain.getValue(1);
5252
5253 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005254 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005255 Ops.push_back(Chain);
5256 Ops.push_back(DAG.getValueType(AVT));
5257 Ops.push_back(InFlag);
5258 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5259
5260 if (TwoRepStos) {
5261 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005262 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005263 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005264 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005265 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5266 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5267 Left, InFlag);
5268 InFlag = Chain.getValue(1);
5269 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5270 Ops.clear();
5271 Ops.push_back(Chain);
5272 Ops.push_back(DAG.getValueType(MVT::i8));
5273 Ops.push_back(InFlag);
5274 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5275 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005276 // Handle the last 1 - 7 bytes.
5277 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005278 MVT AddrVT = Dst.getValueType();
5279 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005280
5281 Chain = DAG.getMemset(Chain,
5282 DAG.getNode(ISD::ADD, AddrVT, Dst,
5283 DAG.getConstant(Offset, AddrVT)),
5284 Src,
5285 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005286 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005287 }
5288
Dan Gohmane8b391e2008-04-12 04:36:06 +00005289 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005290 return Chain;
5291}
5292
Dan Gohman8181bd12008-07-27 21:46:04 +00005293SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005294X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005295 SDValue Chain, SDValue Dst, SDValue Src,
5296 SDValue Size, unsigned Align,
5297 bool AlwaysInline,
5298 const Value *DstSV, uint64_t DstSVOff,
5299 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005300 // This requires the copy size to be a constant, preferrably
5301 // within a subtarget-specific limit.
5302 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5303 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005304 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005305 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005306 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005307 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005308
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005309 /// If not DWORD aligned, call the library.
5310 if ((Align & 3) != 0)
5311 return SDValue();
5312
5313 // DWORD aligned
5314 MVT AVT = MVT::i32;
5315 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005316 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005317
Duncan Sands92c43912008-06-06 12:08:01 +00005318 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005319 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005320 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005321 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005322
Dan Gohman8181bd12008-07-27 21:46:04 +00005323 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005324 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5325 Count, InFlag);
5326 InFlag = Chain.getValue(1);
5327 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005328 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005329 InFlag = Chain.getValue(1);
5330 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005331 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005332 InFlag = Chain.getValue(1);
5333
5334 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005335 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005336 Ops.push_back(Chain);
5337 Ops.push_back(DAG.getValueType(AVT));
5338 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005339 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005340
Dan Gohman8181bd12008-07-27 21:46:04 +00005341 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005342 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005343 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005344 // Handle the last 1 - 7 bytes.
5345 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005346 MVT DstVT = Dst.getValueType();
5347 MVT SrcVT = Src.getValueType();
5348 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005349 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005350 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005351 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005352 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005353 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005354 DAG.getConstant(BytesLeft, SizeVT),
5355 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005356 DstSV, DstSVOff + Offset,
5357 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005358 }
5359
Dan Gohmane8b391e2008-04-12 04:36:06 +00005360 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005361}
5362
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005363/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5364SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005365 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005366 SDValue TheChain = N->getOperand(0);
5367 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005368 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005369 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5370 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005371 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005372 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005373 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005374 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005375 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005376 };
5377
Gabor Greif1c80d112008-08-28 21:40:38 +00005378 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005379 }
5380
Dan Gohman8181bd12008-07-27 21:46:04 +00005381 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5382 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005383 MVT::i32, eax.getValue(2));
5384 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005385 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005386 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5387
5388 // Use a MERGE_VALUES to return the value and chain.
5389 Ops[1] = edx.getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00005390 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005391}
5392
Dan Gohman8181bd12008-07-27 21:46:04 +00005393SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005394 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005395
5396 if (!Subtarget->is64Bit()) {
5397 // vastart just stores the address of the VarArgsFrameIndex slot into the
5398 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005399 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005400 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005401 }
5402
5403 // __va_list_tag:
5404 // gp_offset (0 - 6 * 8)
5405 // fp_offset (48 - 48 + 8 * 16)
5406 // overflow_arg_area (point to parameters coming in memory).
5407 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005408 SmallVector<SDValue, 8> MemOps;
5409 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005410 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005411 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005412 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005413 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005414 MemOps.push_back(Store);
5415
5416 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005417 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005418 Store = DAG.getStore(Op.getOperand(0),
5419 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005420 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005421 MemOps.push_back(Store);
5422
5423 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005424 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005425 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005426 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005427 MemOps.push_back(Store);
5428
5429 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005430 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005431 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005432 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005433 MemOps.push_back(Store);
5434 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5435}
5436
Dan Gohman8181bd12008-07-27 21:46:04 +00005437SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005438 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5439 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005440 SDValue Chain = Op.getOperand(0);
5441 SDValue SrcPtr = Op.getOperand(1);
5442 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005443
5444 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5445 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005446 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005447}
5448
Dan Gohman8181bd12008-07-27 21:46:04 +00005449SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005450 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005451 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005452 SDValue Chain = Op.getOperand(0);
5453 SDValue DstPtr = Op.getOperand(1);
5454 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005455 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5456 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005457
Dan Gohman840ff5c2008-04-18 20:55:41 +00005458 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5459 DAG.getIntPtrConstant(24), 8, false,
5460 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005461}
5462
Dan Gohman8181bd12008-07-27 21:46:04 +00005463SDValue
5464X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005465 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005466 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005467 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005468 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005469 case Intrinsic::x86_sse_comieq_ss:
5470 case Intrinsic::x86_sse_comilt_ss:
5471 case Intrinsic::x86_sse_comile_ss:
5472 case Intrinsic::x86_sse_comigt_ss:
5473 case Intrinsic::x86_sse_comige_ss:
5474 case Intrinsic::x86_sse_comineq_ss:
5475 case Intrinsic::x86_sse_ucomieq_ss:
5476 case Intrinsic::x86_sse_ucomilt_ss:
5477 case Intrinsic::x86_sse_ucomile_ss:
5478 case Intrinsic::x86_sse_ucomigt_ss:
5479 case Intrinsic::x86_sse_ucomige_ss:
5480 case Intrinsic::x86_sse_ucomineq_ss:
5481 case Intrinsic::x86_sse2_comieq_sd:
5482 case Intrinsic::x86_sse2_comilt_sd:
5483 case Intrinsic::x86_sse2_comile_sd:
5484 case Intrinsic::x86_sse2_comigt_sd:
5485 case Intrinsic::x86_sse2_comige_sd:
5486 case Intrinsic::x86_sse2_comineq_sd:
5487 case Intrinsic::x86_sse2_ucomieq_sd:
5488 case Intrinsic::x86_sse2_ucomilt_sd:
5489 case Intrinsic::x86_sse2_ucomile_sd:
5490 case Intrinsic::x86_sse2_ucomigt_sd:
5491 case Intrinsic::x86_sse2_ucomige_sd:
5492 case Intrinsic::x86_sse2_ucomineq_sd: {
5493 unsigned Opc = 0;
5494 ISD::CondCode CC = ISD::SETCC_INVALID;
5495 switch (IntNo) {
5496 default: break;
5497 case Intrinsic::x86_sse_comieq_ss:
5498 case Intrinsic::x86_sse2_comieq_sd:
5499 Opc = X86ISD::COMI;
5500 CC = ISD::SETEQ;
5501 break;
5502 case Intrinsic::x86_sse_comilt_ss:
5503 case Intrinsic::x86_sse2_comilt_sd:
5504 Opc = X86ISD::COMI;
5505 CC = ISD::SETLT;
5506 break;
5507 case Intrinsic::x86_sse_comile_ss:
5508 case Intrinsic::x86_sse2_comile_sd:
5509 Opc = X86ISD::COMI;
5510 CC = ISD::SETLE;
5511 break;
5512 case Intrinsic::x86_sse_comigt_ss:
5513 case Intrinsic::x86_sse2_comigt_sd:
5514 Opc = X86ISD::COMI;
5515 CC = ISD::SETGT;
5516 break;
5517 case Intrinsic::x86_sse_comige_ss:
5518 case Intrinsic::x86_sse2_comige_sd:
5519 Opc = X86ISD::COMI;
5520 CC = ISD::SETGE;
5521 break;
5522 case Intrinsic::x86_sse_comineq_ss:
5523 case Intrinsic::x86_sse2_comineq_sd:
5524 Opc = X86ISD::COMI;
5525 CC = ISD::SETNE;
5526 break;
5527 case Intrinsic::x86_sse_ucomieq_ss:
5528 case Intrinsic::x86_sse2_ucomieq_sd:
5529 Opc = X86ISD::UCOMI;
5530 CC = ISD::SETEQ;
5531 break;
5532 case Intrinsic::x86_sse_ucomilt_ss:
5533 case Intrinsic::x86_sse2_ucomilt_sd:
5534 Opc = X86ISD::UCOMI;
5535 CC = ISD::SETLT;
5536 break;
5537 case Intrinsic::x86_sse_ucomile_ss:
5538 case Intrinsic::x86_sse2_ucomile_sd:
5539 Opc = X86ISD::UCOMI;
5540 CC = ISD::SETLE;
5541 break;
5542 case Intrinsic::x86_sse_ucomigt_ss:
5543 case Intrinsic::x86_sse2_ucomigt_sd:
5544 Opc = X86ISD::UCOMI;
5545 CC = ISD::SETGT;
5546 break;
5547 case Intrinsic::x86_sse_ucomige_ss:
5548 case Intrinsic::x86_sse2_ucomige_sd:
5549 Opc = X86ISD::UCOMI;
5550 CC = ISD::SETGE;
5551 break;
5552 case Intrinsic::x86_sse_ucomineq_ss:
5553 case Intrinsic::x86_sse2_ucomineq_sd:
5554 Opc = X86ISD::UCOMI;
5555 CC = ISD::SETNE;
5556 break;
5557 }
5558
5559 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005560 SDValue LHS = Op.getOperand(1);
5561 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005562 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5563
Dan Gohman8181bd12008-07-27 21:46:04 +00005564 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5565 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005566 DAG.getConstant(X86CC, MVT::i8), Cond);
5567 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005568 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005569
5570 // Fix vector shift instructions where the last operand is a non-immediate
5571 // i32 value.
5572 case Intrinsic::x86_sse2_pslli_w:
5573 case Intrinsic::x86_sse2_pslli_d:
5574 case Intrinsic::x86_sse2_pslli_q:
5575 case Intrinsic::x86_sse2_psrli_w:
5576 case Intrinsic::x86_sse2_psrli_d:
5577 case Intrinsic::x86_sse2_psrli_q:
5578 case Intrinsic::x86_sse2_psrai_w:
5579 case Intrinsic::x86_sse2_psrai_d:
5580 case Intrinsic::x86_mmx_pslli_w:
5581 case Intrinsic::x86_mmx_pslli_d:
5582 case Intrinsic::x86_mmx_pslli_q:
5583 case Intrinsic::x86_mmx_psrli_w:
5584 case Intrinsic::x86_mmx_psrli_d:
5585 case Intrinsic::x86_mmx_psrli_q:
5586 case Intrinsic::x86_mmx_psrai_w:
5587 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005588 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005589 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005590 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005591
5592 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005593 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005594 switch (IntNo) {
5595 case Intrinsic::x86_sse2_pslli_w:
5596 NewIntNo = Intrinsic::x86_sse2_psll_w;
5597 break;
5598 case Intrinsic::x86_sse2_pslli_d:
5599 NewIntNo = Intrinsic::x86_sse2_psll_d;
5600 break;
5601 case Intrinsic::x86_sse2_pslli_q:
5602 NewIntNo = Intrinsic::x86_sse2_psll_q;
5603 break;
5604 case Intrinsic::x86_sse2_psrli_w:
5605 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5606 break;
5607 case Intrinsic::x86_sse2_psrli_d:
5608 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5609 break;
5610 case Intrinsic::x86_sse2_psrli_q:
5611 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5612 break;
5613 case Intrinsic::x86_sse2_psrai_w:
5614 NewIntNo = Intrinsic::x86_sse2_psra_w;
5615 break;
5616 case Intrinsic::x86_sse2_psrai_d:
5617 NewIntNo = Intrinsic::x86_sse2_psra_d;
5618 break;
5619 default: {
5620 ShAmtVT = MVT::v2i32;
5621 switch (IntNo) {
5622 case Intrinsic::x86_mmx_pslli_w:
5623 NewIntNo = Intrinsic::x86_mmx_psll_w;
5624 break;
5625 case Intrinsic::x86_mmx_pslli_d:
5626 NewIntNo = Intrinsic::x86_mmx_psll_d;
5627 break;
5628 case Intrinsic::x86_mmx_pslli_q:
5629 NewIntNo = Intrinsic::x86_mmx_psll_q;
5630 break;
5631 case Intrinsic::x86_mmx_psrli_w:
5632 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5633 break;
5634 case Intrinsic::x86_mmx_psrli_d:
5635 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5636 break;
5637 case Intrinsic::x86_mmx_psrli_q:
5638 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5639 break;
5640 case Intrinsic::x86_mmx_psrai_w:
5641 NewIntNo = Intrinsic::x86_mmx_psra_w;
5642 break;
5643 case Intrinsic::x86_mmx_psrai_d:
5644 NewIntNo = Intrinsic::x86_mmx_psra_d;
5645 break;
5646 default: abort(); // Can't reach here.
5647 }
5648 break;
5649 }
5650 }
Duncan Sands92c43912008-06-06 12:08:01 +00005651 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005652 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5653 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5654 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5655 DAG.getConstant(NewIntNo, MVT::i32),
5656 Op.getOperand(1), ShAmt);
5657 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005658 }
5659}
5660
Dan Gohman8181bd12008-07-27 21:46:04 +00005661SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005662 // Depths > 0 not supported yet!
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005663 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005664 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005665
5666 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005667 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005668 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5669}
5670
Dan Gohman8181bd12008-07-27 21:46:04 +00005671SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00005672 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
5673 MFI->setFrameAddressIsTaken(true);
5674 MVT VT = Op.getValueType();
5675 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5676 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
5677 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), FrameReg, VT);
5678 while (Depth--)
5679 FrameAddr = DAG.getLoad(VT, DAG.getEntryNode(), FrameAddr, NULL, 0);
5680 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005681}
5682
Dan Gohman8181bd12008-07-27 21:46:04 +00005683SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005684 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005685 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005686}
5687
Dan Gohman8181bd12008-07-27 21:46:04 +00005688SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005689{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005690 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005691 SDValue Chain = Op.getOperand(0);
5692 SDValue Offset = Op.getOperand(1);
5693 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005694
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005695 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5696 getPointerTy());
5697 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005698
Dan Gohman8181bd12008-07-27 21:46:04 +00005699 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005700 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005701 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5702 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005703 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5704 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005705
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005706 return DAG.getNode(X86ISD::EH_RETURN,
5707 MVT::Other,
5708 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005709}
5710
Dan Gohman8181bd12008-07-27 21:46:04 +00005711SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005712 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005713 SDValue Root = Op.getOperand(0);
5714 SDValue Trmp = Op.getOperand(1); // trampoline
5715 SDValue FPtr = Op.getOperand(2); // nested function
5716 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005717
Dan Gohman12a9c082008-02-06 22:27:42 +00005718 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005719
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005720 const X86InstrInfo *TII =
5721 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5722
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005723 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005724 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005725
5726 // Large code-model.
5727
5728 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5729 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5730
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005731 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5732 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005733
5734 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5735
5736 // Load the pointer to the nested function into R11.
5737 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005738 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005739 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005740 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005741
5742 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005743 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005744
5745 // Load the 'nest' parameter value into R10.
5746 // R10 is specified in X86CallingConv.td
5747 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5748 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5749 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005750 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005751
5752 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005753 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005754
5755 // Jump to the nested function.
5756 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5757 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5758 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005759 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005760
5761 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5762 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5763 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005764 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005765
Dan Gohman8181bd12008-07-27 21:46:04 +00005766 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005767 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005768 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005769 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005770 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005771 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5772 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005773 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005774
5775 switch (CC) {
5776 default:
5777 assert(0 && "Unsupported calling convention");
5778 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005779 case CallingConv::X86_StdCall: {
5780 // Pass 'nest' parameter in ECX.
5781 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005782 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005783
5784 // Check that ECX wasn't needed by an 'inreg' parameter.
5785 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00005786 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005787
Chris Lattner1c8733e2008-03-12 17:45:29 +00005788 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005789 unsigned InRegCount = 0;
5790 unsigned Idx = 1;
5791
5792 for (FunctionType::param_iterator I = FTy->param_begin(),
5793 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00005794 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005795 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005796 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005797
5798 if (InRegCount > 2) {
5799 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5800 abort();
5801 }
5802 }
5803 break;
5804 }
5805 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00005806 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005807 // Pass 'nest' parameter in EAX.
5808 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005809 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005810 break;
5811 }
5812
Dan Gohman8181bd12008-07-27 21:46:04 +00005813 SDValue OutChains[4];
5814 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005815
5816 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5817 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5818
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005819 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005820 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005821 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005822 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005823
5824 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005825 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005826
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005827 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005828 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5829 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005830 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005831
5832 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005833 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005834
Dan Gohman8181bd12008-07-27 21:46:04 +00005835 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005836 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005837 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005838 }
5839}
5840
Dan Gohman8181bd12008-07-27 21:46:04 +00005841SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005842 /*
5843 The rounding mode is in bits 11:10 of FPSR, and has the following
5844 settings:
5845 00 Round to nearest
5846 01 Round to -inf
5847 10 Round to +inf
5848 11 Round to 0
5849
5850 FLT_ROUNDS, on the other hand, expects the following:
5851 -1 Undefined
5852 0 Round to 0
5853 1 Round to nearest
5854 2 Round to +inf
5855 3 Round to -inf
5856
5857 To perform the conversion, we do:
5858 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5859 */
5860
5861 MachineFunction &MF = DAG.getMachineFunction();
5862 const TargetMachine &TM = MF.getTarget();
5863 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5864 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005865 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005866
5867 // Save FP Control Word to stack slot
5868 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005869 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005870
Dan Gohman8181bd12008-07-27 21:46:04 +00005871 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00005872 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005873
5874 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005875 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005876
5877 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005878 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005879 DAG.getNode(ISD::SRL, MVT::i16,
5880 DAG.getNode(ISD::AND, MVT::i16,
5881 CWD, DAG.getConstant(0x800, MVT::i16)),
5882 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005883 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005884 DAG.getNode(ISD::SRL, MVT::i16,
5885 DAG.getNode(ISD::AND, MVT::i16,
5886 CWD, DAG.getConstant(0x400, MVT::i16)),
5887 DAG.getConstant(9, MVT::i8));
5888
Dan Gohman8181bd12008-07-27 21:46:04 +00005889 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005890 DAG.getNode(ISD::AND, MVT::i16,
5891 DAG.getNode(ISD::ADD, MVT::i16,
5892 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5893 DAG.getConstant(1, MVT::i16)),
5894 DAG.getConstant(3, MVT::i16));
5895
5896
Duncan Sands92c43912008-06-06 12:08:01 +00005897 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005898 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5899}
5900
Dan Gohman8181bd12008-07-27 21:46:04 +00005901SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005902 MVT VT = Op.getValueType();
5903 MVT OpVT = VT;
5904 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005905
5906 Op = Op.getOperand(0);
5907 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005908 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005909 OpVT = MVT::i32;
5910 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5911 }
Evan Cheng48679f42007-12-14 02:13:44 +00005912
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005913 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5914 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5915 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5916
5917 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005918 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005919 Ops.push_back(Op);
5920 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5921 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5922 Ops.push_back(Op.getValue(1));
5923 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5924
5925 // Finally xor with NumBits-1.
5926 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5927
Evan Cheng48679f42007-12-14 02:13:44 +00005928 if (VT == MVT::i8)
5929 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5930 return Op;
5931}
5932
Dan Gohman8181bd12008-07-27 21:46:04 +00005933SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005934 MVT VT = Op.getValueType();
5935 MVT OpVT = VT;
5936 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005937
5938 Op = Op.getOperand(0);
5939 if (VT == MVT::i8) {
5940 OpVT = MVT::i32;
5941 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5942 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005943
5944 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5945 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5946 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5947
5948 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005949 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005950 Ops.push_back(Op);
5951 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5952 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5953 Ops.push_back(Op.getValue(1));
5954 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5955
Evan Cheng48679f42007-12-14 02:13:44 +00005956 if (VT == MVT::i8)
5957 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5958 return Op;
5959}
5960
Dan Gohman8181bd12008-07-27 21:46:04 +00005961SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005962 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005963 unsigned Reg = 0;
5964 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005965 switch(T.getSimpleVT()) {
5966 default:
5967 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005968 case MVT::i8: Reg = X86::AL; size = 1; break;
5969 case MVT::i16: Reg = X86::AX; size = 2; break;
5970 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005971 case MVT::i64:
5972 if (Subtarget->is64Bit()) {
5973 Reg = X86::RAX; size = 8;
5974 } else //Should go away when LowerType stuff lands
Gabor Greif1c80d112008-08-28 21:40:38 +00005975 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005976 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005977 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005978 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00005979 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00005980 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00005981 Op.getOperand(1),
5982 Op.getOperand(3),
5983 DAG.getTargetConstant(size, MVT::i8),
5984 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005985 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005986 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5987 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005988 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5989 return cpOut;
5990}
5991
Gabor Greif825aa892008-08-28 23:19:51 +00005992SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
5993 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005994 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005995 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005996 SDValue cpInL, cpInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00005997 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00005998 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00005999 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00006000 DAG.getConstant(1, MVT::i32));
6001 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00006002 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00006003 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
6004 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00006005 SDValue swapInL, swapInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00006006 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00006007 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00006008 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00006009 DAG.getConstant(1, MVT::i32));
6010 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
6011 swapInL, cpInH.getValue(1));
6012 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
6013 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00006014 SDValue Ops[] = { swapInH.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006015 Op->getOperand(1),
6016 swapInH.getValue(1) };
Andrew Lenharth81580822008-03-05 01:15:49 +00006017 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006018 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
6019 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00006020 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00006021 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00006022 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00006023 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
6024 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
6025 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greif1c80d112008-08-28 21:40:38 +00006026 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharth81580822008-03-05 01:15:49 +00006027}
6028
Dale Johannesenf160d802008-10-02 18:53:47 +00006029SDValue X86TargetLowering::LowerATOMIC_BINARY_64(SDValue Op,
6030 SelectionDAG &DAG,
6031 unsigned NewOp) {
6032 SDNode *Node = Op.getNode();
6033 MVT T = Node->getValueType(0);
6034 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6035
6036 SDValue Chain = Node->getOperand(0);
6037 SDValue In1 = Node->getOperand(1);
6038 assert(Node->getOperand(2).getNode()->getOpcode()==ISD::BUILD_PAIR);
6039 SDValue In2L = Node->getOperand(2).getNode()->getOperand(0);
6040 SDValue In2H = Node->getOperand(2).getNode()->getOperand(1);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006041 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6042 // have a MemOperand. Pass the info through as a normal operand.
6043 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6044 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
Dale Johannesenf160d802008-10-02 18:53:47 +00006045 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006046 SDValue Result = DAG.getNode(NewOp, Tys, Ops, 5);
Dale Johannesenf160d802008-10-02 18:53:47 +00006047 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
6048 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
6049 SDValue Vals[2] = { ResultVal, Result.getValue(2) };
6050 return SDValue(DAG.getMergeValues(Vals, 2).getNode(), 0);
6051}
6052
Dale Johannesen9011d872008-09-29 22:25:26 +00006053SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6054 SDNode *Node = Op.getNode();
6055 MVT T = Node->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006056 SDValue negOp = DAG.getNode(ISD::SUB, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006057 DAG.getConstant(0, T), Node->getOperand(2));
6058 return DAG.getAtomic((Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_8 ?
6059 ISD::ATOMIC_LOAD_ADD_8 :
6060 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_16 ?
6061 ISD::ATOMIC_LOAD_ADD_16 :
6062 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_32 ?
6063 ISD::ATOMIC_LOAD_ADD_32 :
6064 ISD::ATOMIC_LOAD_ADD_64),
6065 Node->getOperand(0),
6066 Node->getOperand(1), negOp,
6067 cast<AtomicSDNode>(Node)->getSrcValue(),
6068 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006069}
6070
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006071/// LowerOperation - Provide custom lowering hooks for some operations.
6072///
Dan Gohman8181bd12008-07-27 21:46:04 +00006073SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006074 switch (Op.getOpcode()) {
6075 default: assert(0 && "Should not custom lower this!");
Dale Johannesenf160d802008-10-02 18:53:47 +00006076 case ISD::ATOMIC_CMP_SWAP_8:
6077 case ISD::ATOMIC_CMP_SWAP_16:
6078 case ISD::ATOMIC_CMP_SWAP_32:
Dale Johannesenbc187662008-08-28 02:44:49 +00006079 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006080 case ISD::ATOMIC_LOAD_SUB_8:
6081 case ISD::ATOMIC_LOAD_SUB_16:
Dale Johannesen9011d872008-09-29 22:25:26 +00006082 case ISD::ATOMIC_LOAD_SUB_32: return LowerLOAD_SUB(Op,DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006083 case ISD::ATOMIC_LOAD_SUB_64: return (Subtarget->is64Bit()) ?
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006084 LowerLOAD_SUB(Op,DAG) :
6085 LowerATOMIC_BINARY_64(Op,DAG,
Dale Johannesenf160d802008-10-02 18:53:47 +00006086 X86ISD::ATOMSUB64_DAG);
6087 case ISD::ATOMIC_LOAD_AND_64: return LowerATOMIC_BINARY_64(Op,DAG,
6088 X86ISD::ATOMAND64_DAG);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006089 case ISD::ATOMIC_LOAD_OR_64: return LowerATOMIC_BINARY_64(Op, DAG,
Dale Johannesenf160d802008-10-02 18:53:47 +00006090 X86ISD::ATOMOR64_DAG);
6091 case ISD::ATOMIC_LOAD_XOR_64: return LowerATOMIC_BINARY_64(Op,DAG,
6092 X86ISD::ATOMXOR64_DAG);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006093 case ISD::ATOMIC_LOAD_NAND_64:return LowerATOMIC_BINARY_64(Op,DAG,
Dale Johannesenf160d802008-10-02 18:53:47 +00006094 X86ISD::ATOMNAND64_DAG);
6095 case ISD::ATOMIC_LOAD_ADD_64: return LowerATOMIC_BINARY_64(Op,DAG,
6096 X86ISD::ATOMADD64_DAG);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006097 case ISD::ATOMIC_SWAP_64: return LowerATOMIC_BINARY_64(Op,DAG,
6098 X86ISD::ATOMSWAP64_DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006099 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6100 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6101 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6102 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6103 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6104 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6105 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6106 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006107 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006108 case ISD::SHL_PARTS:
6109 case ISD::SRA_PARTS:
6110 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6111 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
6112 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6113 case ISD::FABS: return LowerFABS(Op, DAG);
6114 case ISD::FNEG: return LowerFNEG(Op, DAG);
6115 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006116 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006117 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006118 case ISD::SELECT: return LowerSELECT(Op, DAG);
6119 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006120 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6121 case ISD::CALL: return LowerCALL(Op, DAG);
6122 case ISD::RET: return LowerRET(Op, DAG);
6123 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006124 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006125 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006126 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6127 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6128 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6129 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6130 case ISD::FRAME_TO_ARGS_OFFSET:
6131 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6132 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6133 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006134 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006135 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006136 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6137 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006138
6139 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6140 case ISD::READCYCLECOUNTER:
Gabor Greif1c80d112008-08-28 21:40:38 +00006141 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006142 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006143}
6144
Duncan Sandsac496a12008-07-04 11:47:58 +00006145/// ReplaceNodeResults - Replace a node with an illegal result type
6146/// with a new node built out of custom code.
6147SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006148 switch (N->getOpcode()) {
6149 default: assert(0 && "Should not custom lower this!");
6150 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6151 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesenbc187662008-08-28 02:44:49 +00006152 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006153 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006154}
6155
6156const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6157 switch (Opcode) {
6158 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006159 case X86ISD::BSF: return "X86ISD::BSF";
6160 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006161 case X86ISD::SHLD: return "X86ISD::SHLD";
6162 case X86ISD::SHRD: return "X86ISD::SHRD";
6163 case X86ISD::FAND: return "X86ISD::FAND";
6164 case X86ISD::FOR: return "X86ISD::FOR";
6165 case X86ISD::FXOR: return "X86ISD::FXOR";
6166 case X86ISD::FSRL: return "X86ISD::FSRL";
6167 case X86ISD::FILD: return "X86ISD::FILD";
6168 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6169 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6170 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6171 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6172 case X86ISD::FLD: return "X86ISD::FLD";
6173 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006174 case X86ISD::CALL: return "X86ISD::CALL";
6175 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6176 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6177 case X86ISD::CMP: return "X86ISD::CMP";
6178 case X86ISD::COMI: return "X86ISD::COMI";
6179 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6180 case X86ISD::SETCC: return "X86ISD::SETCC";
6181 case X86ISD::CMOV: return "X86ISD::CMOV";
6182 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6183 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6184 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6185 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006186 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6187 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006188 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006189 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006190 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6191 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006192 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6193 case X86ISD::FMAX: return "X86ISD::FMAX";
6194 case X86ISD::FMIN: return "X86ISD::FMIN";
6195 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6196 case X86ISD::FRCP: return "X86ISD::FRCP";
6197 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6198 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6199 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006200 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006201 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006202 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6203 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006204 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6205 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6206 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6207 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6208 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6209 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006210 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6211 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006212 case X86ISD::VSHL: return "X86ISD::VSHL";
6213 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006214 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6215 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6216 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6217 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6218 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6219 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6220 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6221 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6222 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6223 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006224 }
6225}
6226
6227// isLegalAddressingMode - Return true if the addressing mode represented
6228// by AM is legal for this target, for a load/store of the specified type.
6229bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6230 const Type *Ty) const {
6231 // X86 supports extremely general addressing modes.
6232
6233 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6234 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6235 return false;
6236
6237 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006238 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006239 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6240 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006241
6242 // X86-64 only supports addr of globals in small code model.
6243 if (Subtarget->is64Bit()) {
6244 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6245 return false;
6246 // If lower 4G is not available, then we must use rip-relative addressing.
6247 if (AM.BaseOffs || AM.Scale > 1)
6248 return false;
6249 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006250 }
6251
6252 switch (AM.Scale) {
6253 case 0:
6254 case 1:
6255 case 2:
6256 case 4:
6257 case 8:
6258 // These scales always work.
6259 break;
6260 case 3:
6261 case 5:
6262 case 9:
6263 // These scales are formed with basereg+scalereg. Only accept if there is
6264 // no basereg yet.
6265 if (AM.HasBaseReg)
6266 return false;
6267 break;
6268 default: // Other stuff never works.
6269 return false;
6270 }
6271
6272 return true;
6273}
6274
6275
Evan Cheng27a820a2007-10-26 01:56:11 +00006276bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6277 if (!Ty1->isInteger() || !Ty2->isInteger())
6278 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006279 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6280 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006281 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006282 return false;
6283 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006284}
6285
Duncan Sands92c43912008-06-06 12:08:01 +00006286bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6287 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006288 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006289 unsigned NumBits1 = VT1.getSizeInBits();
6290 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006291 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006292 return false;
6293 return Subtarget->is64Bit() || NumBits1 < 64;
6294}
Evan Cheng27a820a2007-10-26 01:56:11 +00006295
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006296/// isShuffleMaskLegal - Targets can use this to indicate that they only
6297/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6298/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6299/// are assumed to be legal.
6300bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006301X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006302 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006303 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006304 return (Mask.getNode()->getNumOperands() <= 4 ||
6305 isIdentityMask(Mask.getNode()) ||
6306 isIdentityMask(Mask.getNode(), true) ||
6307 isSplatMask(Mask.getNode()) ||
6308 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6309 X86::isUNPCKLMask(Mask.getNode()) ||
6310 X86::isUNPCKHMask(Mask.getNode()) ||
6311 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6312 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006313}
6314
Dan Gohman48d5f062008-04-09 20:09:42 +00006315bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006316X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006317 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006318 unsigned NumElts = BVOps.size();
6319 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006320 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006321 if (NumElts == 2) return true;
6322 if (NumElts == 4) {
6323 return (isMOVLMask(&BVOps[0], 4) ||
6324 isCommutedMOVL(&BVOps[0], 4, true) ||
6325 isSHUFPMask(&BVOps[0], 4) ||
6326 isCommutedSHUFP(&BVOps[0], 4));
6327 }
6328 return false;
6329}
6330
6331//===----------------------------------------------------------------------===//
6332// X86 Scheduler Hooks
6333//===----------------------------------------------------------------------===//
6334
Mon P Wang078a62d2008-05-05 19:05:59 +00006335// private utility function
6336MachineBasicBlock *
6337X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6338 MachineBasicBlock *MBB,
6339 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006340 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006341 unsigned LoadOpc,
6342 unsigned CXchgOpc,
6343 unsigned copyOpc,
6344 unsigned notOpc,
6345 unsigned EAXreg,
6346 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006347 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006348 // For the atomic bitwise operator, we generate
6349 // thisMBB:
6350 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006351 // ld t1 = [bitinstr.addr]
6352 // op t2 = t1, [bitinstr.val]
6353 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006354 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6355 // bz newMBB
6356 // fallthrough -->nextMBB
6357 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6358 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006359 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006360 ++MBBIter;
6361
6362 /// First build the CFG
6363 MachineFunction *F = MBB->getParent();
6364 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006365 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6366 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6367 F->insert(MBBIter, newMBB);
6368 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006369
6370 // Move all successors to thisMBB to nextMBB
6371 nextMBB->transferSuccessors(thisMBB);
6372
6373 // Update thisMBB to fall through to newMBB
6374 thisMBB->addSuccessor(newMBB);
6375
6376 // newMBB jumps to itself and fall through to nextMBB
6377 newMBB->addSuccessor(nextMBB);
6378 newMBB->addSuccessor(newMBB);
6379
6380 // Insert instructions into newMBB based on incoming instruction
6381 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6382 MachineOperand& destOper = bInstr->getOperand(0);
6383 MachineOperand* argOpers[6];
6384 int numArgs = bInstr->getNumOperands() - 1;
6385 for (int i=0; i < numArgs; ++i)
6386 argOpers[i] = &bInstr->getOperand(i+1);
6387
6388 // x86 address has 4 operands: base, index, scale, and displacement
6389 int lastAddrIndx = 3; // [0,3]
6390 int valArgIndx = 4;
6391
Dale Johannesend20e4452008-08-19 18:47:28 +00006392 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6393 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006394 for (int i=0; i <= lastAddrIndx; ++i)
6395 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006396
Dale Johannesend20e4452008-08-19 18:47:28 +00006397 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006398 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006399 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006400 }
6401 else
6402 tt = t1;
6403
Dale Johannesend20e4452008-08-19 18:47:28 +00006404 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006405 assert((argOpers[valArgIndx]->isReg() ||
6406 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006407 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006408 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006409 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6410 else
6411 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006412 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006413 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006414
Dale Johannesend20e4452008-08-19 18:47:28 +00006415 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006416 MIB.addReg(t1);
6417
Dale Johannesend20e4452008-08-19 18:47:28 +00006418 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006419 for (int i=0; i <= lastAddrIndx; ++i)
6420 (*MIB).addOperand(*argOpers[i]);
6421 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006422 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6423 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6424
Dale Johannesend20e4452008-08-19 18:47:28 +00006425 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6426 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006427
6428 // insert branch
6429 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6430
Dan Gohman221a4372008-07-07 23:14:23 +00006431 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006432 return nextMBB;
6433}
6434
Dale Johannesen44eb5372008-10-03 19:41:08 +00006435// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00006436MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00006437X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
6438 MachineBasicBlock *MBB,
6439 unsigned regOpcL,
6440 unsigned regOpcH,
6441 unsigned immOpcL,
6442 unsigned immOpcH,
6443 bool invSrc) {
6444 // For the atomic bitwise operator, we generate
6445 // thisMBB (instructions are in pairs, except cmpxchg8b)
6446 // ld t1,t2 = [bitinstr.addr]
6447 // newMBB:
6448 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
6449 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006450 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00006451 // mov ECX, EBX <- t5, t6
6452 // mov EAX, EDX <- t1, t2
6453 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
6454 // mov t3, t4 <- EAX, EDX
6455 // bz newMBB
6456 // result in out1, out2
6457 // fallthrough -->nextMBB
6458
6459 const TargetRegisterClass *RC = X86::GR32RegisterClass;
6460 const unsigned LoadOpc = X86::MOV32rm;
6461 const unsigned copyOpc = X86::MOV32rr;
6462 const unsigned NotOpc = X86::NOT32r;
6463 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6464 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6465 MachineFunction::iterator MBBIter = MBB;
6466 ++MBBIter;
6467
6468 /// First build the CFG
6469 MachineFunction *F = MBB->getParent();
6470 MachineBasicBlock *thisMBB = MBB;
6471 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6472 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6473 F->insert(MBBIter, newMBB);
6474 F->insert(MBBIter, nextMBB);
6475
6476 // Move all successors to thisMBB to nextMBB
6477 nextMBB->transferSuccessors(thisMBB);
6478
6479 // Update thisMBB to fall through to newMBB
6480 thisMBB->addSuccessor(newMBB);
6481
6482 // newMBB jumps to itself and fall through to nextMBB
6483 newMBB->addSuccessor(nextMBB);
6484 newMBB->addSuccessor(newMBB);
6485
6486 // Insert instructions into newMBB based on incoming instruction
6487 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
6488 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
6489 MachineOperand& dest1Oper = bInstr->getOperand(0);
6490 MachineOperand& dest2Oper = bInstr->getOperand(1);
6491 MachineOperand* argOpers[6];
6492 for (int i=0; i < 6; ++i)
6493 argOpers[i] = &bInstr->getOperand(i+2);
6494
6495 // x86 address has 4 operands: base, index, scale, and displacement
6496 int lastAddrIndx = 3; // [0,3]
6497
6498 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6499 MachineInstrBuilder MIB = BuildMI(thisMBB, TII->get(LoadOpc), t1);
6500 for (int i=0; i <= lastAddrIndx; ++i)
6501 (*MIB).addOperand(*argOpers[i]);
6502 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
6503 MIB = BuildMI(thisMBB, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006504 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00006505 for (int i=0; i <= lastAddrIndx-1; ++i)
6506 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006507 MachineOperand newOp3 = *(argOpers[3]);
6508 if (newOp3.isImm())
6509 newOp3.setImm(newOp3.getImm()+4);
6510 else
6511 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00006512 (*MIB).addOperand(newOp3);
6513
6514 // t3/4 are defined later, at the bottom of the loop
6515 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
6516 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
6517 BuildMI(newMBB, TII->get(X86::PHI), dest1Oper.getReg())
6518 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
6519 BuildMI(newMBB, TII->get(X86::PHI), dest2Oper.getReg())
6520 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
6521
6522 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
6523 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
6524 if (invSrc) {
6525 MIB = BuildMI(newMBB, TII->get(NotOpc), tt1).addReg(t1);
6526 MIB = BuildMI(newMBB, TII->get(NotOpc), tt2).addReg(t2);
6527 } else {
6528 tt1 = t1;
6529 tt2 = t2;
6530 }
6531
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006532 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00006533 "invalid operand");
6534 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
6535 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006536 if (argOpers[4]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006537 MIB = BuildMI(newMBB, TII->get(regOpcL), t5);
6538 else
6539 MIB = BuildMI(newMBB, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006540 if (regOpcL != X86::MOV32rr)
6541 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00006542 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006543 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
6544 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
6545 if (argOpers[5]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006546 MIB = BuildMI(newMBB, TII->get(regOpcH), t6);
6547 else
6548 MIB = BuildMI(newMBB, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006549 if (regOpcH != X86::MOV32rr)
6550 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006551 (*MIB).addOperand(*argOpers[5]);
6552
6553 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EAX);
6554 MIB.addReg(t1);
6555 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EDX);
6556 MIB.addReg(t2);
6557
6558 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EBX);
6559 MIB.addReg(t5);
6560 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::ECX);
6561 MIB.addReg(t6);
6562
6563 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG8B));
6564 for (int i=0; i <= lastAddrIndx; ++i)
6565 (*MIB).addOperand(*argOpers[i]);
6566
6567 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6568 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6569
6570 MIB = BuildMI(newMBB, TII->get(copyOpc), t3);
6571 MIB.addReg(X86::EAX);
6572 MIB = BuildMI(newMBB, TII->get(copyOpc), t4);
6573 MIB.addReg(X86::EDX);
6574
6575 // insert branch
6576 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6577
6578 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
6579 return nextMBB;
6580}
6581
6582// private utility function
6583MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00006584X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6585 MachineBasicBlock *MBB,
6586 unsigned cmovOpc) {
6587 // For the atomic min/max operator, we generate
6588 // thisMBB:
6589 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006590 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006591 // mov t2 = [min/max.val]
6592 // cmp t1, t2
6593 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006594 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006595 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6596 // bz newMBB
6597 // fallthrough -->nextMBB
6598 //
6599 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6600 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006601 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006602 ++MBBIter;
6603
6604 /// First build the CFG
6605 MachineFunction *F = MBB->getParent();
6606 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006607 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6608 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6609 F->insert(MBBIter, newMBB);
6610 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006611
6612 // Move all successors to thisMBB to nextMBB
6613 nextMBB->transferSuccessors(thisMBB);
6614
6615 // Update thisMBB to fall through to newMBB
6616 thisMBB->addSuccessor(newMBB);
6617
6618 // newMBB jumps to newMBB and fall through to nextMBB
6619 newMBB->addSuccessor(nextMBB);
6620 newMBB->addSuccessor(newMBB);
6621
6622 // Insert instructions into newMBB based on incoming instruction
6623 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6624 MachineOperand& destOper = mInstr->getOperand(0);
6625 MachineOperand* argOpers[6];
6626 int numArgs = mInstr->getNumOperands() - 1;
6627 for (int i=0; i < numArgs; ++i)
6628 argOpers[i] = &mInstr->getOperand(i+1);
6629
6630 // x86 address has 4 operands: base, index, scale, and displacement
6631 int lastAddrIndx = 3; // [0,3]
6632 int valArgIndx = 4;
6633
Mon P Wang318b0372008-05-05 22:56:23 +00006634 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6635 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006636 for (int i=0; i <= lastAddrIndx; ++i)
6637 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006638
Mon P Wang078a62d2008-05-05 19:05:59 +00006639 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006640 assert((argOpers[valArgIndx]->isReg() ||
6641 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006642 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00006643
6644 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006645 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006646 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6647 else
6648 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6649 (*MIB).addOperand(*argOpers[valArgIndx]);
6650
Mon P Wang318b0372008-05-05 22:56:23 +00006651 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6652 MIB.addReg(t1);
6653
Mon P Wang078a62d2008-05-05 19:05:59 +00006654 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6655 MIB.addReg(t1);
6656 MIB.addReg(t2);
6657
6658 // Generate movc
6659 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6660 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6661 MIB.addReg(t2);
6662 MIB.addReg(t1);
6663
6664 // Cmp and exchange if none has modified the memory location
6665 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6666 for (int i=0; i <= lastAddrIndx; ++i)
6667 (*MIB).addOperand(*argOpers[i]);
6668 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006669 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6670 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006671
6672 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6673 MIB.addReg(X86::EAX);
6674
6675 // insert branch
6676 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6677
Dan Gohman221a4372008-07-07 23:14:23 +00006678 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006679 return nextMBB;
6680}
6681
6682
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006683MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006684X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6685 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006686 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6687 switch (MI->getOpcode()) {
6688 default: assert(false && "Unexpected instr type to insert");
6689 case X86::CMOV_FR32:
6690 case X86::CMOV_FR64:
6691 case X86::CMOV_V4F32:
6692 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006693 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006694 // To "insert" a SELECT_CC instruction, we actually have to insert the
6695 // diamond control-flow pattern. The incoming instruction knows the
6696 // destination vreg to set, the condition code register to branch on, the
6697 // true/false values to select between, and a branch opcode to use.
6698 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006699 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006700 ++It;
6701
6702 // thisMBB:
6703 // ...
6704 // TrueVal = ...
6705 // cmpTY ccX, r1, r2
6706 // bCC copy1MBB
6707 // fallthrough --> copy0MBB
6708 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006709 MachineFunction *F = BB->getParent();
6710 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6711 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006712 unsigned Opc =
6713 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6714 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006715 F->insert(It, copy0MBB);
6716 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006717 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006718 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006719 sinkMBB->transferSuccessors(BB);
6720
6721 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006722 BB->addSuccessor(copy0MBB);
6723 BB->addSuccessor(sinkMBB);
6724
6725 // copy0MBB:
6726 // %FalseValue = ...
6727 // # fallthrough to sinkMBB
6728 BB = copy0MBB;
6729
6730 // Update machine-CFG edges
6731 BB->addSuccessor(sinkMBB);
6732
6733 // sinkMBB:
6734 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6735 // ...
6736 BB = sinkMBB;
6737 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6738 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6739 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6740
Dan Gohman221a4372008-07-07 23:14:23 +00006741 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006742 return BB;
6743 }
6744
6745 case X86::FP32_TO_INT16_IN_MEM:
6746 case X86::FP32_TO_INT32_IN_MEM:
6747 case X86::FP32_TO_INT64_IN_MEM:
6748 case X86::FP64_TO_INT16_IN_MEM:
6749 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006750 case X86::FP64_TO_INT64_IN_MEM:
6751 case X86::FP80_TO_INT16_IN_MEM:
6752 case X86::FP80_TO_INT32_IN_MEM:
6753 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006754 // Change the floating point control register to use "round towards zero"
6755 // mode when truncating to an integer value.
6756 MachineFunction *F = BB->getParent();
6757 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6758 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6759
6760 // Load the old value of the high byte of the control word...
6761 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006762 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006763 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6764
6765 // Set the high part to be round to zero...
6766 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6767 .addImm(0xC7F);
6768
6769 // Reload the modified control word now...
6770 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6771
6772 // Restore the memory image of control word to original value
6773 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6774 .addReg(OldCW);
6775
6776 // Get the X86 opcode to use.
6777 unsigned Opc;
6778 switch (MI->getOpcode()) {
6779 default: assert(0 && "illegal opcode!");
6780 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6781 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6782 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6783 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6784 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6785 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006786 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6787 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6788 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006789 }
6790
6791 X86AddressMode AM;
6792 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006793 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006794 AM.BaseType = X86AddressMode::RegBase;
6795 AM.Base.Reg = Op.getReg();
6796 } else {
6797 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006798 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006799 }
6800 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006801 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006802 AM.Scale = Op.getImm();
6803 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006804 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006805 AM.IndexReg = Op.getImm();
6806 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006807 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006808 AM.GV = Op.getGlobal();
6809 } else {
6810 AM.Disp = Op.getImm();
6811 }
6812 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6813 .addReg(MI->getOperand(4).getReg());
6814
6815 // Reload the original control word now.
6816 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6817
Dan Gohman221a4372008-07-07 23:14:23 +00006818 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006819 return BB;
6820 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006821 case X86::ATOMAND32:
6822 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006823 X86::AND32ri, X86::MOV32rm,
6824 X86::LCMPXCHG32, X86::MOV32rr,
6825 X86::NOT32r, X86::EAX,
6826 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006827 case X86::ATOMOR32:
6828 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006829 X86::OR32ri, X86::MOV32rm,
6830 X86::LCMPXCHG32, X86::MOV32rr,
6831 X86::NOT32r, X86::EAX,
6832 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006833 case X86::ATOMXOR32:
6834 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006835 X86::XOR32ri, X86::MOV32rm,
6836 X86::LCMPXCHG32, X86::MOV32rr,
6837 X86::NOT32r, X86::EAX,
6838 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006839 case X86::ATOMNAND32:
6840 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006841 X86::AND32ri, X86::MOV32rm,
6842 X86::LCMPXCHG32, X86::MOV32rr,
6843 X86::NOT32r, X86::EAX,
6844 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006845 case X86::ATOMMIN32:
6846 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6847 case X86::ATOMMAX32:
6848 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6849 case X86::ATOMUMIN32:
6850 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6851 case X86::ATOMUMAX32:
6852 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006853
6854 case X86::ATOMAND16:
6855 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6856 X86::AND16ri, X86::MOV16rm,
6857 X86::LCMPXCHG16, X86::MOV16rr,
6858 X86::NOT16r, X86::AX,
6859 X86::GR16RegisterClass);
6860 case X86::ATOMOR16:
6861 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6862 X86::OR16ri, X86::MOV16rm,
6863 X86::LCMPXCHG16, X86::MOV16rr,
6864 X86::NOT16r, X86::AX,
6865 X86::GR16RegisterClass);
6866 case X86::ATOMXOR16:
6867 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6868 X86::XOR16ri, X86::MOV16rm,
6869 X86::LCMPXCHG16, X86::MOV16rr,
6870 X86::NOT16r, X86::AX,
6871 X86::GR16RegisterClass);
6872 case X86::ATOMNAND16:
6873 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6874 X86::AND16ri, X86::MOV16rm,
6875 X86::LCMPXCHG16, X86::MOV16rr,
6876 X86::NOT16r, X86::AX,
6877 X86::GR16RegisterClass, true);
6878 case X86::ATOMMIN16:
6879 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6880 case X86::ATOMMAX16:
6881 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6882 case X86::ATOMUMIN16:
6883 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6884 case X86::ATOMUMAX16:
6885 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6886
6887 case X86::ATOMAND8:
6888 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6889 X86::AND8ri, X86::MOV8rm,
6890 X86::LCMPXCHG8, X86::MOV8rr,
6891 X86::NOT8r, X86::AL,
6892 X86::GR8RegisterClass);
6893 case X86::ATOMOR8:
6894 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6895 X86::OR8ri, X86::MOV8rm,
6896 X86::LCMPXCHG8, X86::MOV8rr,
6897 X86::NOT8r, X86::AL,
6898 X86::GR8RegisterClass);
6899 case X86::ATOMXOR8:
6900 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6901 X86::XOR8ri, X86::MOV8rm,
6902 X86::LCMPXCHG8, X86::MOV8rr,
6903 X86::NOT8r, X86::AL,
6904 X86::GR8RegisterClass);
6905 case X86::ATOMNAND8:
6906 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6907 X86::AND8ri, X86::MOV8rm,
6908 X86::LCMPXCHG8, X86::MOV8rr,
6909 X86::NOT8r, X86::AL,
6910 X86::GR8RegisterClass, true);
6911 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00006912 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006913 case X86::ATOMAND64:
6914 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6915 X86::AND64ri32, X86::MOV64rm,
6916 X86::LCMPXCHG64, X86::MOV64rr,
6917 X86::NOT64r, X86::RAX,
6918 X86::GR64RegisterClass);
6919 case X86::ATOMOR64:
6920 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6921 X86::OR64ri32, X86::MOV64rm,
6922 X86::LCMPXCHG64, X86::MOV64rr,
6923 X86::NOT64r, X86::RAX,
6924 X86::GR64RegisterClass);
6925 case X86::ATOMXOR64:
6926 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6927 X86::XOR64ri32, X86::MOV64rm,
6928 X86::LCMPXCHG64, X86::MOV64rr,
6929 X86::NOT64r, X86::RAX,
6930 X86::GR64RegisterClass);
6931 case X86::ATOMNAND64:
6932 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6933 X86::AND64ri32, X86::MOV64rm,
6934 X86::LCMPXCHG64, X86::MOV64rr,
6935 X86::NOT64r, X86::RAX,
6936 X86::GR64RegisterClass, true);
6937 case X86::ATOMMIN64:
6938 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6939 case X86::ATOMMAX64:
6940 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6941 case X86::ATOMUMIN64:
6942 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6943 case X86::ATOMUMAX64:
6944 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00006945
6946 // This group does 64-bit operations on a 32-bit host.
6947 case X86::ATOMAND6432:
6948 return EmitAtomicBit6432WithCustomInserter(MI, BB,
6949 X86::AND32rr, X86::AND32rr,
6950 X86::AND32ri, X86::AND32ri,
6951 false);
6952 case X86::ATOMOR6432:
6953 return EmitAtomicBit6432WithCustomInserter(MI, BB,
6954 X86::OR32rr, X86::OR32rr,
6955 X86::OR32ri, X86::OR32ri,
6956 false);
6957 case X86::ATOMXOR6432:
6958 return EmitAtomicBit6432WithCustomInserter(MI, BB,
6959 X86::XOR32rr, X86::XOR32rr,
6960 X86::XOR32ri, X86::XOR32ri,
6961 false);
6962 case X86::ATOMNAND6432:
6963 return EmitAtomicBit6432WithCustomInserter(MI, BB,
6964 X86::AND32rr, X86::AND32rr,
6965 X86::AND32ri, X86::AND32ri,
6966 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00006967 case X86::ATOMADD6432:
6968 return EmitAtomicBit6432WithCustomInserter(MI, BB,
6969 X86::ADD32rr, X86::ADC32rr,
6970 X86::ADD32ri, X86::ADC32ri,
6971 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00006972 case X86::ATOMSUB6432:
6973 return EmitAtomicBit6432WithCustomInserter(MI, BB,
6974 X86::SUB32rr, X86::SBB32rr,
6975 X86::SUB32ri, X86::SBB32ri,
6976 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006977 case X86::ATOMSWAP6432:
6978 return EmitAtomicBit6432WithCustomInserter(MI, BB,
6979 X86::MOV32rr, X86::MOV32rr,
6980 X86::MOV32ri, X86::MOV32ri,
6981 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006982 }
6983}
6984
6985//===----------------------------------------------------------------------===//
6986// X86 Optimization Hooks
6987//===----------------------------------------------------------------------===//
6988
Dan Gohman8181bd12008-07-27 21:46:04 +00006989void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006990 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006991 APInt &KnownZero,
6992 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006993 const SelectionDAG &DAG,
6994 unsigned Depth) const {
6995 unsigned Opc = Op.getOpcode();
6996 assert((Opc >= ISD::BUILTIN_OP_END ||
6997 Opc == ISD::INTRINSIC_WO_CHAIN ||
6998 Opc == ISD::INTRINSIC_W_CHAIN ||
6999 Opc == ISD::INTRINSIC_VOID) &&
7000 "Should use MaskedValueIsZero if you don't know whether Op"
7001 " is a target node!");
7002
Dan Gohman1d79e432008-02-13 23:07:24 +00007003 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007004 switch (Opc) {
7005 default: break;
7006 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007007 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7008 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007009 break;
7010 }
7011}
7012
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007013/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007014/// node is a GlobalAddress + offset.
7015bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7016 GlobalValue* &GA, int64_t &Offset) const{
7017 if (N->getOpcode() == X86ISD::Wrapper) {
7018 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007019 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
7020 return true;
7021 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007022 }
Evan Chengef7be082008-05-12 19:56:52 +00007023 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007024}
7025
Evan Chengef7be082008-05-12 19:56:52 +00007026static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7027 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007028 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007029 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007030 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007031 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007032 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007033 return false;
7034}
7035
Dan Gohman8181bd12008-07-27 21:46:04 +00007036static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007037 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007038 SDNode *&Base,
7039 SelectionDAG &DAG, MachineFrameInfo *MFI,
7040 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007041 Base = NULL;
7042 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007043 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007044 if (Idx.getOpcode() == ISD::UNDEF) {
7045 if (!Base)
7046 return false;
7047 continue;
7048 }
7049
Dan Gohman8181bd12008-07-27 21:46:04 +00007050 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007051 if (!Elt.getNode() ||
7052 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007053 return false;
7054 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007055 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007056 if (Base->getOpcode() == ISD::UNDEF)
7057 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007058 continue;
7059 }
7060 if (Elt.getOpcode() == ISD::UNDEF)
7061 continue;
7062
Gabor Greif1c80d112008-08-28 21:40:38 +00007063 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007064 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007065 return false;
7066 }
7067 return true;
7068}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007069
7070/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7071/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7072/// if the load addresses are consecutive, non-overlapping, and in the right
7073/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007074static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007075 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007076 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00007077 MVT VT = N->getValueType(0);
7078 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007079 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007080 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007081 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007082 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7083 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007084 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007085
Dan Gohman11821702007-07-27 17:16:43 +00007086 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007087 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007088 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00007089 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00007090 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
7091 LD->getSrcValueOffset(), LD->isVolatile(),
7092 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007093}
7094
Evan Chengb6290462008-05-12 23:04:07 +00007095/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007096static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng6617eed2008-09-24 23:26:36 +00007097 const X86Subtarget *Subtarget,
7098 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007099 unsigned NumOps = N->getNumOperands();
7100
Evan Chenge9b9c672008-05-09 21:53:03 +00007101 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007102 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007103 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007104
Duncan Sands92c43912008-06-06 12:08:01 +00007105 MVT VT = N->getValueType(0);
7106 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007107 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7108 // We are looking for load i64 and zero extend. We want to transform
7109 // it before legalizer has a chance to expand it. Also look for i64
7110 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007111 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007112 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007113 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007114 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007115 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007116
7117 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007118 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007119 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007120 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007121 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007122 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007123 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007124 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007125 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007126
7127 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007128 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007129
7130 // Load must not be an extload.
7131 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007132 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00007133
Evan Cheng6617eed2008-09-24 23:26:36 +00007134 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7135 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7136 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
7137 DAG.ReplaceAllUsesOfValueWith(SDValue(Base, 1), ResNode.getValue(1));
7138 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007139}
7140
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007141/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007142static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007143 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007144 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007145
7146 // If we have SSE[12] support, try to form min/max nodes.
7147 if (Subtarget->hasSSE2() &&
7148 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7149 if (Cond.getOpcode() == ISD::SETCC) {
7150 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007151 SDValue LHS = N->getOperand(1);
7152 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007153 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7154
7155 unsigned Opcode = 0;
7156 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7157 switch (CC) {
7158 default: break;
7159 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7160 case ISD::SETULE:
7161 case ISD::SETLE:
7162 if (!UnsafeFPMath) break;
7163 // FALL THROUGH.
7164 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7165 case ISD::SETLT:
7166 Opcode = X86ISD::FMIN;
7167 break;
7168
7169 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7170 case ISD::SETUGT:
7171 case ISD::SETGT:
7172 if (!UnsafeFPMath) break;
7173 // FALL THROUGH.
7174 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7175 case ISD::SETGE:
7176 Opcode = X86ISD::FMAX;
7177 break;
7178 }
7179 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7180 switch (CC) {
7181 default: break;
7182 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7183 case ISD::SETUGT:
7184 case ISD::SETGT:
7185 if (!UnsafeFPMath) break;
7186 // FALL THROUGH.
7187 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7188 case ISD::SETGE:
7189 Opcode = X86ISD::FMIN;
7190 break;
7191
7192 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7193 case ISD::SETULE:
7194 case ISD::SETLE:
7195 if (!UnsafeFPMath) break;
7196 // FALL THROUGH.
7197 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7198 case ISD::SETLT:
7199 Opcode = X86ISD::FMAX;
7200 break;
7201 }
7202 }
7203
7204 if (Opcode)
7205 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
7206 }
7207
7208 }
7209
Dan Gohman8181bd12008-07-27 21:46:04 +00007210 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007211}
7212
Chris Lattnerce84ae42008-02-22 02:09:43 +00007213/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007214static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007215 const X86Subtarget *Subtarget) {
7216 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7217 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007218 // A preferable solution to the general problem is to figure out the right
7219 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007220 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007221 if (St->getValue().getValueType().isVector() &&
7222 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007223 isa<LoadSDNode>(St->getValue()) &&
7224 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7225 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007226 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007227 LoadSDNode *Ld = 0;
7228 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007229 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007230 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007231 // Must be a store of a load. We currently handle two cases: the load
7232 // is a direct child, and it's under an intervening TokenFactor. It is
7233 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007234 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007235 Ld = cast<LoadSDNode>(St->getChain());
7236 else if (St->getValue().hasOneUse() &&
7237 ChainVal->getOpcode() == ISD::TokenFactor) {
7238 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007239 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007240 TokenFactorIndex = i;
7241 Ld = cast<LoadSDNode>(St->getValue());
7242 } else
7243 Ops.push_back(ChainVal->getOperand(i));
7244 }
7245 }
7246 if (Ld) {
7247 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7248 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007249 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007250 Ld->getBasePtr(), Ld->getSrcValue(),
7251 Ld->getSrcValueOffset(), Ld->isVolatile(),
7252 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007253 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007254 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00007255 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00007256 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7257 Ops.size());
7258 }
7259 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
7260 St->getSrcValue(), St->getSrcValueOffset(),
7261 St->isVolatile(), St->getAlignment());
7262 }
7263
7264 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00007265 SDValue LoAddr = Ld->getBasePtr();
7266 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007267 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007268
Dan Gohman8181bd12008-07-27 21:46:04 +00007269 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007270 Ld->getSrcValue(), Ld->getSrcValueOffset(),
7271 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007272 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007273 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
7274 Ld->isVolatile(),
7275 MinAlign(Ld->getAlignment(), 4));
7276
Dan Gohman8181bd12008-07-27 21:46:04 +00007277 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007278 if (TokenFactorIndex != -1) {
7279 Ops.push_back(LoLd);
7280 Ops.push_back(HiLd);
7281 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7282 Ops.size());
7283 }
7284
7285 LoAddr = St->getBasePtr();
7286 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007287 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007288
Dan Gohman8181bd12008-07-27 21:46:04 +00007289 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007290 St->getSrcValue(), St->getSrcValueOffset(),
7291 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007292 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00007293 St->getSrcValue(),
7294 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00007295 St->isVolatile(),
7296 MinAlign(St->getAlignment(), 4));
7297 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00007298 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00007299 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007300 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00007301}
7302
Chris Lattner470d5dc2008-01-25 06:14:17 +00007303/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7304/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007305static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00007306 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7307 // F[X]OR(0.0, x) -> x
7308 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00007309 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7310 if (C->getValueAPF().isPosZero())
7311 return N->getOperand(1);
7312 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7313 if (C->getValueAPF().isPosZero())
7314 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007315 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007316}
7317
7318/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007319static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007320 // FAND(0.0, x) -> 0.0
7321 // FAND(x, 0.0) -> 0.0
7322 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7323 if (C->getValueAPF().isPosZero())
7324 return N->getOperand(0);
7325 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7326 if (C->getValueAPF().isPosZero())
7327 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007328 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007329}
7330
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007331
Dan Gohman8181bd12008-07-27 21:46:04 +00007332SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007333 DAGCombinerInfo &DCI) const {
7334 SelectionDAG &DAG = DCI.DAG;
7335 switch (N->getOpcode()) {
7336 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007337 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7338 case ISD::BUILD_VECTOR:
7339 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007340 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007341 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007342 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007343 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7344 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007345 }
7346
Dan Gohman8181bd12008-07-27 21:46:04 +00007347 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007348}
7349
7350//===----------------------------------------------------------------------===//
7351// X86 Inline Assembly Support
7352//===----------------------------------------------------------------------===//
7353
7354/// getConstraintType - Given a constraint letter, return the type of
7355/// constraint it is for this target.
7356X86TargetLowering::ConstraintType
7357X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7358 if (Constraint.size() == 1) {
7359 switch (Constraint[0]) {
7360 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00007361 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007362 case 'r':
7363 case 'R':
7364 case 'l':
7365 case 'q':
7366 case 'Q':
7367 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007368 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007369 case 'Y':
7370 return C_RegisterClass;
7371 default:
7372 break;
7373 }
7374 }
7375 return TargetLowering::getConstraintType(Constraint);
7376}
7377
Dale Johannesene99fc902008-01-29 02:21:21 +00007378/// LowerXConstraint - try to replace an X constraint, which matches anything,
7379/// with another that has more specific requirements based on the type of the
7380/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007381const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007382LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007383 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7384 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007385 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007386 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007387 return "Y";
7388 if (Subtarget->hasSSE1())
7389 return "x";
7390 }
7391
7392 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007393}
7394
Chris Lattnera531abc2007-08-25 00:47:38 +00007395/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7396/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007397void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007398 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00007399 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00007400 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007401 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007402 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007403
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007404 switch (Constraint) {
7405 default: break;
7406 case 'I':
7407 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007408 if (C->getZExtValue() <= 31) {
7409 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007410 break;
7411 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007412 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007413 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00007414 case 'J':
7415 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
7416 if (C->getZExtValue() <= 63) {
7417 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
7418 break;
7419 }
7420 }
7421 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007422 case 'N':
7423 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007424 if (C->getZExtValue() <= 255) {
7425 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007426 break;
7427 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007428 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007429 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007430 case 'i': {
7431 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007432 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007433 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007434 break;
7435 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007436
7437 // If we are in non-pic codegen mode, we allow the address of a global (with
7438 // an optional displacement) to be used with 'i'.
7439 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7440 int64_t Offset = 0;
7441
7442 // Match either (GA) or (GA+C)
7443 if (GA) {
7444 Offset = GA->getOffset();
7445 } else if (Op.getOpcode() == ISD::ADD) {
7446 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7447 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7448 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007449 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007450 } else {
7451 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7452 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7453 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007454 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007455 else
7456 C = 0, GA = 0;
7457 }
7458 }
7459
7460 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00007461 if (hasMemory)
7462 Op = LowerGlobalAddress(GA->getGlobal(), DAG);
7463 else
7464 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7465 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007466 Result = Op;
7467 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007468 }
7469
7470 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007471 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007472 }
7473 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007474
Gabor Greif1c80d112008-08-28 21:40:38 +00007475 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007476 Ops.push_back(Result);
7477 return;
7478 }
Evan Cheng7f250d62008-09-24 00:05:32 +00007479 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
7480 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007481}
7482
7483std::vector<unsigned> X86TargetLowering::
7484getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007485 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007486 if (Constraint.size() == 1) {
7487 // FIXME: not handling fp-stack yet!
7488 switch (Constraint[0]) { // GCC X86 Constraint Letters
7489 default: break; // Unknown constraint letter
7490 case 'A': // EAX/EDX
7491 if (VT == MVT::i32 || VT == MVT::i64)
7492 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7493 break;
7494 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7495 case 'Q': // Q_REGS
7496 if (VT == MVT::i32)
7497 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7498 else if (VT == MVT::i16)
7499 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7500 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007501 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007502 else if (VT == MVT::i64)
7503 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7504 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007505 }
7506 }
7507
7508 return std::vector<unsigned>();
7509}
7510
7511std::pair<unsigned, const TargetRegisterClass*>
7512X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007513 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007514 // First, see if this is a constraint that directly corresponds to an LLVM
7515 // register class.
7516 if (Constraint.size() == 1) {
7517 // GCC Constraint Letters
7518 switch (Constraint[0]) {
7519 default: break;
7520 case 'r': // GENERAL_REGS
7521 case 'R': // LEGACY_REGS
7522 case 'l': // INDEX_REGS
7523 if (VT == MVT::i64 && Subtarget->is64Bit())
7524 return std::make_pair(0U, X86::GR64RegisterClass);
7525 if (VT == MVT::i32)
7526 return std::make_pair(0U, X86::GR32RegisterClass);
7527 else if (VT == MVT::i16)
7528 return std::make_pair(0U, X86::GR16RegisterClass);
7529 else if (VT == MVT::i8)
7530 return std::make_pair(0U, X86::GR8RegisterClass);
7531 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007532 case 'f': // FP Stack registers.
7533 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7534 // value to the correct fpstack register class.
7535 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7536 return std::make_pair(0U, X86::RFP32RegisterClass);
7537 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7538 return std::make_pair(0U, X86::RFP64RegisterClass);
7539 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007540 case 'y': // MMX_REGS if MMX allowed.
7541 if (!Subtarget->hasMMX()) break;
7542 return std::make_pair(0U, X86::VR64RegisterClass);
7543 break;
7544 case 'Y': // SSE_REGS if SSE2 allowed
7545 if (!Subtarget->hasSSE2()) break;
7546 // FALL THROUGH.
7547 case 'x': // SSE_REGS if SSE1 allowed
7548 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007549
7550 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007551 default: break;
7552 // Scalar SSE types.
7553 case MVT::f32:
7554 case MVT::i32:
7555 return std::make_pair(0U, X86::FR32RegisterClass);
7556 case MVT::f64:
7557 case MVT::i64:
7558 return std::make_pair(0U, X86::FR64RegisterClass);
7559 // Vector types.
7560 case MVT::v16i8:
7561 case MVT::v8i16:
7562 case MVT::v4i32:
7563 case MVT::v2i64:
7564 case MVT::v4f32:
7565 case MVT::v2f64:
7566 return std::make_pair(0U, X86::VR128RegisterClass);
7567 }
7568 break;
7569 }
7570 }
7571
7572 // Use the default implementation in TargetLowering to convert the register
7573 // constraint into a member of a register class.
7574 std::pair<unsigned, const TargetRegisterClass*> Res;
7575 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7576
7577 // Not found as a standard register?
7578 if (Res.second == 0) {
7579 // GCC calls "st(0)" just plain "st".
7580 if (StringsEqualNoCase("{st}", Constraint)) {
7581 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007582 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007583 }
7584
7585 return Res;
7586 }
7587
7588 // Otherwise, check to see if this is a register class of the wrong value
7589 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7590 // turn into {ax},{dx}.
7591 if (Res.second->hasType(VT))
7592 return Res; // Correct type already, nothing to do.
7593
7594 // All of the single-register GCC register classes map their values onto
7595 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7596 // really want an 8-bit or 32-bit register, map to the appropriate register
7597 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007598 if (Res.second == X86::GR16RegisterClass) {
7599 if (VT == MVT::i8) {
7600 unsigned DestReg = 0;
7601 switch (Res.first) {
7602 default: break;
7603 case X86::AX: DestReg = X86::AL; break;
7604 case X86::DX: DestReg = X86::DL; break;
7605 case X86::CX: DestReg = X86::CL; break;
7606 case X86::BX: DestReg = X86::BL; break;
7607 }
7608 if (DestReg) {
7609 Res.first = DestReg;
7610 Res.second = Res.second = X86::GR8RegisterClass;
7611 }
7612 } else if (VT == MVT::i32) {
7613 unsigned DestReg = 0;
7614 switch (Res.first) {
7615 default: break;
7616 case X86::AX: DestReg = X86::EAX; break;
7617 case X86::DX: DestReg = X86::EDX; break;
7618 case X86::CX: DestReg = X86::ECX; break;
7619 case X86::BX: DestReg = X86::EBX; break;
7620 case X86::SI: DestReg = X86::ESI; break;
7621 case X86::DI: DestReg = X86::EDI; break;
7622 case X86::BP: DestReg = X86::EBP; break;
7623 case X86::SP: DestReg = X86::ESP; break;
7624 }
7625 if (DestReg) {
7626 Res.first = DestReg;
7627 Res.second = Res.second = X86::GR32RegisterClass;
7628 }
7629 } else if (VT == MVT::i64) {
7630 unsigned DestReg = 0;
7631 switch (Res.first) {
7632 default: break;
7633 case X86::AX: DestReg = X86::RAX; break;
7634 case X86::DX: DestReg = X86::RDX; break;
7635 case X86::CX: DestReg = X86::RCX; break;
7636 case X86::BX: DestReg = X86::RBX; break;
7637 case X86::SI: DestReg = X86::RSI; break;
7638 case X86::DI: DestReg = X86::RDI; break;
7639 case X86::BP: DestReg = X86::RBP; break;
7640 case X86::SP: DestReg = X86::RSP; break;
7641 }
7642 if (DestReg) {
7643 Res.first = DestReg;
7644 Res.second = Res.second = X86::GR64RegisterClass;
7645 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007646 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007647 } else if (Res.second == X86::FR32RegisterClass ||
7648 Res.second == X86::FR64RegisterClass ||
7649 Res.second == X86::VR128RegisterClass) {
7650 // Handle references to XMM physical registers that got mapped into the
7651 // wrong class. This can happen with constraints like {xmm0} where the
7652 // target independent register mapper will just pick the first match it can
7653 // find, ignoring the required type.
7654 if (VT == MVT::f32)
7655 Res.second = X86::FR32RegisterClass;
7656 else if (VT == MVT::f64)
7657 Res.second = X86::FR64RegisterClass;
7658 else if (X86::VR128RegisterClass->hasType(VT))
7659 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007660 }
7661
7662 return Res;
7663}