blob: b98792454f2326da6bcc4aacf7fb28660981bb07 [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"
Mon P Wang1f292322008-11-23 04:37:22 +000041#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042using namespace llvm;
43
Mon P Wang1f292322008-11-23 04:37:22 +000044static cl::opt<bool>
Mon P Wangba7e48e2008-11-24 02:10:43 +000045DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang1f292322008-11-23 04:37:22 +000046
Evan Cheng2aea0b42008-04-25 19:11:04 +000047// Forward declarations.
Dan Gohman8181bd12008-07-27 21:46:04 +000048static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG);
Evan Cheng2aea0b42008-04-25 19:11:04 +000049
Dan Gohmanb41dfba2008-05-14 01:58:56 +000050X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 : TargetLowering(TM) {
52 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000053 X86ScalarSSEf64 = Subtarget->hasSSE2();
54 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000056
Chris Lattnerdec9cb52008-01-24 08:07:48 +000057 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
59 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000060 TD = getTargetData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061
62 // Set up the TargetLowering object.
63
64 // X86 is weird, it always uses i8 for shift amounts and setcc results.
65 setShiftAmountType(MVT::i8);
Duncan Sands8cf4a822008-11-23 15:47:28 +000066 setBooleanContents(ZeroOrOneBooleanContent);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 setSchedulingPreference(SchedulingForRegPressure);
68 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
69 setStackPointerRegisterToSaveRestore(X86StackPtr);
70
71 if (Subtarget->isTargetDarwin()) {
72 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
73 setUseUnderscoreSetJmp(false);
74 setUseUnderscoreLongJmp(false);
75 } else if (Subtarget->isTargetMingw()) {
76 // MS runtime is weird: it exports _setjmp, but longjmp!
77 setUseUnderscoreSetJmp(true);
78 setUseUnderscoreLongJmp(false);
79 } else {
80 setUseUnderscoreSetJmp(true);
81 setUseUnderscoreLongJmp(true);
82 }
83
84 // Set up the register classes.
85 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
86 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
87 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
88 if (Subtarget->is64Bit())
89 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
90
Evan Cheng08c171a2008-10-14 21:26:46 +000091 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
Chris Lattner3bc08502008-01-17 19:59:44 +000093 // We don't accept any truncstore of integer registers.
94 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
95 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
96 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
97 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
98 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
Evan Cheng71343822008-10-15 02:05:31 +000099 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
100
101 // SETOEQ and SETUNE require checking two conditions.
102 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
103 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
104 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
105 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
106 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
107 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattner3bc08502008-01-17 19:59:44 +0000108
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
110 // operation.
111 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
112 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
113 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
114
115 if (Subtarget->is64Bit()) {
116 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
117 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
118 } else {
Dale Johannesena359b8b2008-10-21 20:50:01 +0000119 if (X86ScalarSSEf64) {
120 // We have an impenetrably clever algorithm for ui64->double only.
121 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
123 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
Dale Johannesena359b8b2008-10-21 20:50:01 +0000124 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
126 }
127
128 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
129 // this operation.
130 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
131 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
132 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000133 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000135 // f32 and f64 cases are Legal, f80 case is not
136 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
137 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
139 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
140 }
141
Dale Johannesen958b08b2007-09-19 23:55:34 +0000142 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
143 // are Legal, f80 is custom lowered.
144 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
145 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146
147 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
148 // this operation.
149 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
150 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
151
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000152 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000154 // f32 and f64 cases are Legal, f80 case is not
155 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 } else {
157 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
158 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
159 }
160
161 // Handle FP_TO_UINT by promoting the destination to a larger signed
162 // conversion.
163 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
164 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
165 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
166
167 if (Subtarget->is64Bit()) {
168 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
169 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
170 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000171 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 // Expand FP_TO_UINT into a select.
173 // FIXME: We would like to use a Custom expander here eventually to do
174 // the optimal thing for SSE vs. the default expansion in the legalizer.
175 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
176 else
177 // With SSE3 we can use fisttpll to convert to a signed i64.
178 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
179 }
180
181 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000182 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
184 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
185 }
186
Dan Gohman8450d862008-02-18 19:34:53 +0000187 // Scalar integer divide and remainder are lowered to use operations that
188 // produce two results, to match the available instructions. This exposes
189 // the two-result form to trivial CSE, which is able to combine x/y and x%y
190 // into a single instruction.
191 //
192 // Scalar integer multiply-high is also lowered to use two-result
193 // operations, to match the available instructions. However, plain multiply
194 // (low) operations are left as Legal, as there are single-result
195 // instructions for this in x86. Using the two-result multiply instructions
196 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000197 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
198 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
199 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
200 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
201 setOperationAction(ISD::SREM , MVT::i8 , Expand);
202 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000203 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
204 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
205 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
206 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
207 setOperationAction(ISD::SREM , MVT::i16 , Expand);
208 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000209 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
210 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
211 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
212 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
213 setOperationAction(ISD::SREM , MVT::i32 , Expand);
214 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000215 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
216 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
217 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
218 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
219 setOperationAction(ISD::SREM , MVT::i64 , Expand);
220 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000221
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
223 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
224 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
225 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000227 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
228 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
229 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
231 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000232 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000234 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000235 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000236
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000238 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
239 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000241 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
242 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000244 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
245 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 if (Subtarget->is64Bit()) {
247 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000248 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
249 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 }
251
252 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
253 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
254
255 // These should be promoted to a larger select which is supported.
256 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
257 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
258 // X86 wants to expand cmov itself.
259 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
260 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
261 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
262 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000263 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
265 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
266 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
267 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
268 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000269 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 if (Subtarget->is64Bit()) {
271 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
272 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
273 }
274 // X86 ret instruction may pop stack.
275 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000276 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277
278 // Darwin ABI issue.
279 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
280 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
281 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
282 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000283 if (Subtarget->is64Bit())
284 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000285 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 if (Subtarget->is64Bit()) {
287 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
288 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
289 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000290 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 }
292 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
293 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
294 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
295 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000296 if (Subtarget->is64Bit()) {
297 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
298 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
299 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
300 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301
Evan Cheng8d51ab32008-03-10 19:38:10 +0000302 if (Subtarget->hasSSE1())
303 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000304
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000305 if (!Subtarget->hasSSE2())
306 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
307
Mon P Wang078a62d2008-05-05 19:05:59 +0000308 // Expand certain atomics
Dale Johannesenbc187662008-08-28 02:44:49 +0000309 setOperationAction(ISD::ATOMIC_CMP_SWAP_8 , MVT::i8, Custom);
310 setOperationAction(ISD::ATOMIC_CMP_SWAP_16, MVT::i16, Custom);
311 setOperationAction(ISD::ATOMIC_CMP_SWAP_32, MVT::i32, Custom);
312 setOperationAction(ISD::ATOMIC_CMP_SWAP_64, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000313
Dale Johannesen9011d872008-09-29 22:25:26 +0000314 setOperationAction(ISD::ATOMIC_LOAD_SUB_8 , MVT::i8, Custom);
315 setOperationAction(ISD::ATOMIC_LOAD_SUB_16, MVT::i16, Custom);
316 setOperationAction(ISD::ATOMIC_LOAD_SUB_32, MVT::i32, Custom);
317 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000318
Dale Johannesenf160d802008-10-02 18:53:47 +0000319 if (!Subtarget->is64Bit()) {
320 setOperationAction(ISD::ATOMIC_LOAD_ADD_64, MVT::i64, Custom);
321 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Custom);
322 setOperationAction(ISD::ATOMIC_LOAD_AND_64, MVT::i64, Custom);
323 setOperationAction(ISD::ATOMIC_LOAD_OR_64, MVT::i64, Custom);
324 setOperationAction(ISD::ATOMIC_LOAD_XOR_64, MVT::i64, Custom);
325 setOperationAction(ISD::ATOMIC_LOAD_NAND_64, MVT::i64, Custom);
326 setOperationAction(ISD::ATOMIC_SWAP_64, MVT::i64, Custom);
327 }
328
Dan Gohman472d12c2008-06-30 20:59:49 +0000329 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
330 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 // FIXME - use subtarget debug flags
332 if (!Subtarget->isTargetDarwin() &&
333 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000334 !Subtarget->isTargetCygMing()) {
335 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
336 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
337 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338
339 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
340 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
341 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
342 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
343 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 setExceptionPointerRegister(X86::RAX);
345 setExceptionSelectorRegister(X86::RDX);
346 } else {
347 setExceptionPointerRegister(X86::EAX);
348 setExceptionSelectorRegister(X86::EDX);
349 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000350 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000351 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
352
Duncan Sands7407a9f2007-09-11 14:10:23 +0000353 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000354
Chris Lattner56b941f2008-01-15 21:58:22 +0000355 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000356
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
358 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000360 if (Subtarget->is64Bit()) {
361 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000363 } else {
364 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000366 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367
368 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
369 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
370 if (Subtarget->is64Bit())
371 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
372 if (Subtarget->isTargetCygMing())
373 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
374 else
375 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
376
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000377 if (X86ScalarSSEf64) {
378 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 // Set up the FP register classes.
380 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
381 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
382
383 // Use ANDPD to simulate FABS.
384 setOperationAction(ISD::FABS , MVT::f64, Custom);
385 setOperationAction(ISD::FABS , MVT::f32, Custom);
386
387 // Use XORP to simulate FNEG.
388 setOperationAction(ISD::FNEG , MVT::f64, Custom);
389 setOperationAction(ISD::FNEG , MVT::f32, Custom);
390
391 // Use ANDPD and ORPD to simulate FCOPYSIGN.
392 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
393 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
394
395 // We don't support sin/cos/fmod
396 setOperationAction(ISD::FSIN , MVT::f64, Expand);
397 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 setOperationAction(ISD::FSIN , MVT::f32, Expand);
399 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400
401 // Expand FP immediates into loads from the stack, except for the special
402 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000403 addLegalFPImmediate(APFloat(+0.0)); // xorpd
404 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000405
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000406 // Floating truncations from f80 and extensions to f80 go through memory.
407 // If optimizing, we lie about this though and handle it in
408 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
409 if (Fast) {
410 setConvertAction(MVT::f32, MVT::f80, Expand);
411 setConvertAction(MVT::f64, MVT::f80, Expand);
412 setConvertAction(MVT::f80, MVT::f32, Expand);
413 setConvertAction(MVT::f80, MVT::f64, Expand);
414 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000415 } else if (X86ScalarSSEf32) {
416 // Use SSE for f32, x87 for f64.
417 // Set up the FP register classes.
418 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
419 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
420
421 // Use ANDPS to simulate FABS.
422 setOperationAction(ISD::FABS , MVT::f32, Custom);
423
424 // Use XORP to simulate FNEG.
425 setOperationAction(ISD::FNEG , MVT::f32, Custom);
426
427 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
428
429 // Use ANDPS and ORPS to simulate FCOPYSIGN.
430 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
431 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
432
433 // We don't support sin/cos/fmod
434 setOperationAction(ISD::FSIN , MVT::f32, Expand);
435 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000436
Nate Begemane2ba64f2008-02-14 08:57:00 +0000437 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000438 addLegalFPImmediate(APFloat(+0.0f)); // xorps
439 addLegalFPImmediate(APFloat(+0.0)); // FLD0
440 addLegalFPImmediate(APFloat(+1.0)); // FLD1
441 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
442 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
443
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000444 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
445 // this though and handle it in InstructionSelectPreprocess so that
446 // dagcombine2 can hack on these.
447 if (Fast) {
448 setConvertAction(MVT::f32, MVT::f64, Expand);
449 setConvertAction(MVT::f32, MVT::f80, Expand);
450 setConvertAction(MVT::f80, MVT::f32, Expand);
451 setConvertAction(MVT::f64, MVT::f32, Expand);
452 // And x87->x87 truncations also.
453 setConvertAction(MVT::f80, MVT::f64, Expand);
454 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000455
456 if (!UnsafeFPMath) {
457 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
458 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
459 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000461 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462 // Set up the FP register classes.
463 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
464 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
465
466 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
467 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
468 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
469 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000470
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000471 // Floating truncations go through memory. If optimizing, we lie about
472 // this though and handle it in InstructionSelectPreprocess so that
473 // dagcombine2 can hack on these.
474 if (Fast) {
475 setConvertAction(MVT::f80, MVT::f32, Expand);
476 setConvertAction(MVT::f64, MVT::f32, Expand);
477 setConvertAction(MVT::f80, MVT::f64, Expand);
478 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479
480 if (!UnsafeFPMath) {
481 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
482 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
483 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000484 addLegalFPImmediate(APFloat(+0.0)); // FLD0
485 addLegalFPImmediate(APFloat(+1.0)); // FLD1
486 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
487 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000488 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
489 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
490 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
491 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 }
493
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000494 // Long double always uses X87.
495 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000496 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
497 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000498 {
Dale Johannesen6e547b42008-10-09 23:00:39 +0000499 bool ignored;
Chris Lattnerdd867392008-01-27 06:19:31 +0000500 APFloat TmpFlt(+0.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000501 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
502 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000503 addLegalFPImmediate(TmpFlt); // FLD0
504 TmpFlt.changeSign();
505 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
506 APFloat TmpFlt2(+1.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000507 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
508 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000509 addLegalFPImmediate(TmpFlt2); // FLD1
510 TmpFlt2.changeSign();
511 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
512 }
513
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000514 if (!UnsafeFPMath) {
515 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
516 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
517 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000518
Dan Gohman2f7b1982007-10-11 23:21:31 +0000519 // Always use a library call for pow.
520 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
521 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
522 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
523
Dale Johannesen92b33082008-09-04 00:47:13 +0000524 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000525 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000526 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000527 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000528 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
529
Mon P Wanga5a239f2008-11-06 05:31:54 +0000530 // First set operation action for all vector types to either promote
Mon P Wang1448aad2008-10-30 08:01:45 +0000531 // (for widening) or expand (for scalarization). Then we will selectively
532 // turn on ones that can be effectively codegen'd.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
534 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000535 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif825aa892008-08-28 23:19:51 +0000548 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
550 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands92c43912008-06-06 12:08:01 +0000551 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesen177edff2008-09-10 17:31:40 +0000573 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 }
579
Mon P Wang1f292322008-11-23 04:37:22 +0000580 if (!DisableMMX && Subtarget->hasMMX()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000581 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
582 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
583 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000584 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
586
587 // FIXME: add MMX packed arithmetics
588
589 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
590 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
591 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
592 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
593
594 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
595 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
596 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000597 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598
599 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
600 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
601
602 setOperationAction(ISD::AND, MVT::v8i8, Promote);
603 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
604 setOperationAction(ISD::AND, MVT::v4i16, Promote);
605 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
606 setOperationAction(ISD::AND, MVT::v2i32, Promote);
607 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
608 setOperationAction(ISD::AND, MVT::v1i64, Legal);
609
610 setOperationAction(ISD::OR, MVT::v8i8, Promote);
611 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
612 setOperationAction(ISD::OR, MVT::v4i16, Promote);
613 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
614 setOperationAction(ISD::OR, MVT::v2i32, Promote);
615 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
616 setOperationAction(ISD::OR, MVT::v1i64, Legal);
617
618 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
619 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
620 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
621 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
622 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
623 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
624 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
625
626 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
627 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
628 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
629 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
630 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
631 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000632 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
633 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
635
636 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
637 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
638 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000639 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
641
642 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
643 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
644 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
645 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
646
Evan Cheng759fe022008-07-22 18:39:19 +0000647 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
649 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000651
652 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653 }
654
655 if (Subtarget->hasSSE1()) {
656 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
657
658 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
659 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
660 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
661 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
662 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
663 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
665 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
666 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
667 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
668 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000669 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 }
671
672 if (Subtarget->hasSSE2()) {
673 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
674 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
675 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
676 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
677 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
678
679 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
680 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
681 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
682 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
683 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
684 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
685 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
686 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
687 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
688 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
689 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
690 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
691 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
692 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
693 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694
Nate Begeman03605a02008-07-17 16:51:19 +0000695 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
696 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
697 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
698 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000699
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
701 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
702 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
703 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
705
706 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000707 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
708 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000709 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000710 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000711 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000712 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
713 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
714 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 }
716 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
717 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
718 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
719 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000720 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000722 if (Subtarget->is64Bit()) {
723 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000724 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000725 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726
727 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
728 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000729 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
730 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
731 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
732 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
733 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
734 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
735 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
736 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
737 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
738 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739 }
740
Chris Lattner3bc08502008-01-17 19:59:44 +0000741 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000742
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000743 // Custom lower v2i64 and v2f64 selects.
744 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
745 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
746 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
747 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000748
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000750
751 if (Subtarget->hasSSE41()) {
752 // FIXME: Do we need to handle scalar-to-vector here?
753 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000754 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000755
756 // i8 and i16 vectors are custom , because the source register and source
757 // source memory operand types are not the same width. f32 vectors are
758 // custom since the immediate controlling the insert encodes additional
759 // information.
760 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
761 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
762 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
763 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
764
765 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
766 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
767 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000768 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000769
770 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000771 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
772 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000773 }
774 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775
Nate Begeman03605a02008-07-17 16:51:19 +0000776 if (Subtarget->hasSSE42()) {
777 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
778 }
779
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000780 // We want to custom lower some of our intrinsics.
781 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
782
Bill Wendling4c134df2008-11-24 19:21:46 +0000783 // Add with overflow operations are custom lowered.
784 setOperationAction(ISD::SADDO, MVT::i32, Custom);
785 setOperationAction(ISD::SADDO, MVT::i64, Custom);
786 setOperationAction(ISD::UADDO, MVT::i32, Custom);
787 setOperationAction(ISD::UADDO, MVT::i64, Custom);
788
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000789 // We have target-specific dag combine patterns for the following nodes:
790 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000791 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000793 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794
795 computeRegisterProperties();
796
797 // FIXME: These should be based on subtarget info. Plus, the values should
798 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000799 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
800 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
801 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000803 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804}
805
Scott Michel502151f2008-03-10 15:42:14 +0000806
Dan Gohman8181bd12008-07-27 21:46:04 +0000807MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000808 return MVT::i8;
809}
810
811
Evan Cheng5a67b812008-01-23 23:17:41 +0000812/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
813/// the desired ByVal argument alignment.
814static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
815 if (MaxAlign == 16)
816 return;
817 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
818 if (VTy->getBitWidth() == 128)
819 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000820 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
821 unsigned EltAlign = 0;
822 getMaxByValAlign(ATy->getElementType(), EltAlign);
823 if (EltAlign > MaxAlign)
824 MaxAlign = EltAlign;
825 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
826 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
827 unsigned EltAlign = 0;
828 getMaxByValAlign(STy->getElementType(i), EltAlign);
829 if (EltAlign > MaxAlign)
830 MaxAlign = EltAlign;
831 if (MaxAlign == 16)
832 break;
833 }
834 }
835 return;
836}
837
838/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
839/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000840/// that contain SSE vectors are placed at 16-byte boundaries while the rest
841/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000842unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000843 if (Subtarget->is64Bit()) {
844 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000845 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000846 if (TyAlign > 8)
847 return TyAlign;
848 return 8;
849 }
850
Evan Cheng5a67b812008-01-23 23:17:41 +0000851 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000852 if (Subtarget->hasSSE1())
853 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000854 return Align;
855}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856
Evan Cheng8c590372008-05-15 08:39:06 +0000857/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000858/// and store operations as a result of memset, memcpy, and memmove
859/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000860/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000861MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000862X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
863 bool isSrcConst, bool isSrcStr) const {
Chris Lattnerf0bf1062008-10-28 05:49:35 +0000864 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
865 // linux. This is because the stack realignment code can't handle certain
866 // cases like PR2962. This should be removed when PR2962 is fixed.
867 if (Subtarget->getStackAlignment() >= 16) {
868 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
869 return MVT::v4i32;
870 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
871 return MVT::v4f32;
872 }
Evan Cheng8c590372008-05-15 08:39:06 +0000873 if (Subtarget->is64Bit() && Size >= 8)
874 return MVT::i64;
875 return MVT::i32;
876}
877
878
Evan Cheng6fb06762007-11-09 01:32:10 +0000879/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
880/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000881SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000882 SelectionDAG &DAG) const {
883 if (usesGlobalOffsetTable())
884 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
885 if (!Subtarget->isPICStyleRIPRel())
886 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
887 return Table;
888}
889
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890//===----------------------------------------------------------------------===//
891// Return Value Calling Convention Implementation
892//===----------------------------------------------------------------------===//
893
894#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000895
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000897SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000898 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
899
900 SmallVector<CCValAssign, 16> RVLocs;
901 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
902 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
903 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000904 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000905
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 // If this is the first return lowered for this function, add the regs to the
907 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000908 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909 for (unsigned i = 0; i != RVLocs.size(); ++i)
910 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000911 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000913 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000915 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000916 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000917 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000918 SDValue TailCall = Chain;
919 SDValue TargetAddress = TailCall.getOperand(1);
920 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000921 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000922 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000923 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000924 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000925 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
926 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000927 assert(StackAdjustment.getOpcode() == ISD::Constant &&
928 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000929
Dan Gohman8181bd12008-07-27 21:46:04 +0000930 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000931 Operands.push_back(Chain.getOperand(0));
932 Operands.push_back(TargetAddress);
933 Operands.push_back(StackAdjustment);
934 // Copy registers used by the call. Last operand is a flag so it is not
935 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000936 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000937 Operands.push_back(Chain.getOperand(i));
938 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000939 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
940 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000941 }
942
943 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000944 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000945
Dan Gohman8181bd12008-07-27 21:46:04 +0000946 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000947 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
948 // Operand #1 = Bytes To Pop
949 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
950
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000951 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000952 for (unsigned i = 0; i != RVLocs.size(); ++i) {
953 CCValAssign &VA = RVLocs[i];
954 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000955 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000956
Chris Lattnerb56cc342008-03-11 03:23:40 +0000957 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
958 // the RET instruction and handled by the FP Stackifier.
959 if (RVLocs[i].getLocReg() == X86::ST0 ||
960 RVLocs[i].getLocReg() == X86::ST1) {
961 // If this is a copy from an xmm register to ST(0), use an FPExtend to
962 // change the value to the FP stack register class.
963 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
964 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
965 RetOps.push_back(ValToCopy);
966 // Don't emit a copytoreg.
967 continue;
968 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000969
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000970 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971 Flag = Chain.getValue(1);
972 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000973
974 // The x86-64 ABI for returning structs by value requires that we copy
975 // the sret argument into %rax for the return. We saved the argument into
976 // a virtual register in the entry block, so now we copy the value out
977 // and into %rax.
978 if (Subtarget->is64Bit() &&
979 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
980 MachineFunction &MF = DAG.getMachineFunction();
981 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
982 unsigned Reg = FuncInfo->getSRetReturnReg();
983 if (!Reg) {
984 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
985 FuncInfo->setSRetReturnReg(Reg);
986 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000987 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000988
989 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
990 Flag = Chain.getValue(1);
991 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992
Chris Lattnerb56cc342008-03-11 03:23:40 +0000993 RetOps[0] = Chain; // Update chain.
994
995 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +0000996 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +0000997 RetOps.push_back(Flag);
998
999 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000}
1001
1002
1003/// LowerCallResult - Lower the result values of an ISD::CALL into the
1004/// appropriate copies out of appropriate physical registers. This assumes that
1005/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1006/// being lowered. The returns a SDNode with the same number of values as the
1007/// ISD::CALL.
1008SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +00001009LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 unsigned CallingConv, SelectionDAG &DAG) {
1011
1012 // Assign locations to each value returned by this call.
1013 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +00001014 bool isVarArg = TheCall->isVarArg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
1016 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1017
Dan Gohman8181bd12008-07-27 21:46:04 +00001018 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001019
1020 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001021 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00001022 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001023
1024 // If this is a call to a function that returns an fp value on the floating
1025 // point stack, but where we prefer to use the value in xmm registers, copy
1026 // 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 +00001027 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1028 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001029 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1030 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001033 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1034 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001035 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001036 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001037
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001038 if (CopyVT != RVLocs[i].getValVT()) {
1039 // Round the F80 the right size, which also moves to the appropriate xmm
1040 // register.
1041 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1042 // This truncation won't change the value.
1043 DAG.getIntPtrConstant(1));
1044 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001045
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001046 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047 }
Duncan Sands698842f2008-07-02 17:40:58 +00001048
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049 // Merge everything together with a MERGE_VALUES node.
1050 ResultVals.push_back(Chain);
Duncan Sands42d7bb82008-12-01 11:41:29 +00001051 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(), &ResultVals[0],
1052 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001053}
1054
1055
1056//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001057// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058//===----------------------------------------------------------------------===//
1059// StdCall calling convention seems to be standard for many Windows' API
1060// routines and around. It differs from C calling convention just a little:
1061// callee should clean up the stack, not caller. Symbols should be also
1062// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001063// For info on fast calling convention see Fast Calling Convention (tail call)
1064// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001065
1066/// AddLiveIn - This helper function adds the specified physical register to the
1067/// MachineFunction as a live in value. It also creates a corresponding virtual
1068/// register for it.
1069static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1070 const TargetRegisterClass *RC) {
1071 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001072 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1073 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 return VReg;
1075}
1076
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001077/// CallIsStructReturn - Determines whether a CALL node uses struct return
1078/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001079static bool CallIsStructReturn(CallSDNode *TheCall) {
1080 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001081 if (!NumOps)
1082 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001083
Dan Gohman705e3f72008-09-13 01:54:27 +00001084 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001085}
1086
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001087/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1088/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001089static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001090 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001091 if (!NumArgs)
1092 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001093
1094 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001095}
1096
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001097/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1098/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001099/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001100bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001101 if (IsVarArg)
1102 return false;
1103
Dan Gohman705e3f72008-09-13 01:54:27 +00001104 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001105 default:
1106 return false;
1107 case CallingConv::X86_StdCall:
1108 return !Subtarget->is64Bit();
1109 case CallingConv::X86_FastCall:
1110 return !Subtarget->is64Bit();
1111 case CallingConv::Fast:
1112 return PerformTailCallOpt;
1113 }
1114}
1115
Dan Gohman705e3f72008-09-13 01:54:27 +00001116/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1117/// given CallingConvention value.
1118CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001119 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001120 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001121 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001122 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1123 return CC_X86_64_TailCall;
1124 else
1125 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001126 }
1127
Gordon Henriksen18ace102008-01-05 16:56:59 +00001128 if (CC == CallingConv::X86_FastCall)
1129 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001130 else if (CC == CallingConv::Fast)
1131 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001132 else
1133 return CC_X86_32_C;
1134}
1135
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001136/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1137/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001138NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001139X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001140 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001141 if (CC == CallingConv::X86_FastCall)
1142 return FastCall;
1143 else if (CC == CallingConv::X86_StdCall)
1144 return StdCall;
1145 return None;
1146}
1147
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001148
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001149/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1150/// in a register before calling.
1151bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1152 return !IsTailCall && !Is64Bit &&
1153 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1154 Subtarget->isPICStyleGOT();
1155}
1156
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001157/// CallRequiresFnAddressInReg - Check whether the call requires the function
1158/// address to be loaded in a register.
1159bool
1160X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1161 return !Is64Bit && IsTailCall &&
1162 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1163 Subtarget->isPICStyleGOT();
1164}
1165
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001166/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1167/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001168/// the specific parameter attribute. The copy will be passed as a byval
1169/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001170static SDValue
1171CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001172 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001173 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001174 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001175 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001176}
1177
Dan Gohman8181bd12008-07-27 21:46:04 +00001178SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001179 const CCValAssign &VA,
1180 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001181 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001182 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001183 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001184 ISD::ArgFlagsTy Flags =
1185 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001186 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001187 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001188
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001189 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1190 // changed with more analysis.
1191 // In case of tail call optimization mark all arguments mutable. Since they
1192 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001193 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001194 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001195 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001196 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001197 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001198 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001199 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001200}
1201
Dan Gohman8181bd12008-07-27 21:46:04 +00001202SDValue
1203X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001205 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1206
1207 const Function* Fn = MF.getFunction();
1208 if (Fn->hasExternalLinkage() &&
1209 Subtarget->isTargetCygMing() &&
1210 Fn->getName() == "main")
1211 FuncInfo->setForceFramePointer(true);
1212
1213 // Decorate the function name.
1214 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1215
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001216 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001217 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001218 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001219 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001220 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001221 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001222
1223 assert(!(isVarArg && CC == CallingConv::Fast) &&
1224 "Var args not supported with calling convention fastcc");
1225
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001226 // Assign locations to all of the incoming arguments.
1227 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001228 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001229 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001230
Dan Gohman8181bd12008-07-27 21:46:04 +00001231 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001232 unsigned LastVal = ~0U;
1233 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1234 CCValAssign &VA = ArgLocs[i];
1235 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1236 // places.
1237 assert(VA.getValNo() != LastVal &&
1238 "Don't support value assigned to multiple locs yet");
1239 LastVal = VA.getValNo();
1240
1241 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001242 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001243 TargetRegisterClass *RC;
1244 if (RegVT == MVT::i32)
1245 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001246 else if (Is64Bit && RegVT == MVT::i64)
1247 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001248 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001249 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001250 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001251 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001252 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001253 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001254 else if (RegVT.isVector()) {
1255 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001256 if (!Is64Bit)
1257 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1258 else {
1259 // Darwin calling convention passes MMX values in either GPRs or
1260 // XMMs in x86-64. Other targets pass them in memory.
1261 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1262 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1263 RegVT = MVT::v2i64;
1264 } else {
1265 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1266 RegVT = MVT::i64;
1267 }
1268 }
1269 } else {
1270 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001271 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001272
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001273 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001274 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001275
1276 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1277 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1278 // right size.
1279 if (VA.getLocInfo() == CCValAssign::SExt)
1280 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1281 DAG.getValueType(VA.getValVT()));
1282 else if (VA.getLocInfo() == CCValAssign::ZExt)
1283 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1284 DAG.getValueType(VA.getValVT()));
1285
1286 if (VA.getLocInfo() != CCValAssign::Full)
1287 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1288
Gordon Henriksen18ace102008-01-05 16:56:59 +00001289 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001290 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001291 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001292 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1293 else if (RC == X86::VR128RegisterClass) {
1294 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1295 DAG.getConstant(0, MVT::i64));
1296 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1297 }
1298 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001299
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001300 ArgValues.push_back(ArgValue);
1301 } else {
1302 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001303 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001304 }
1305 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001306
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001307 // The x86-64 ABI for returning structs by value requires that we copy
1308 // the sret argument into %rax for the return. Save the argument into
1309 // a virtual register so that we can access it from the return points.
1310 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1311 MachineFunction &MF = DAG.getMachineFunction();
1312 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1313 unsigned Reg = FuncInfo->getSRetReturnReg();
1314 if (!Reg) {
1315 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1316 FuncInfo->setSRetReturnReg(Reg);
1317 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001318 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001319 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1320 }
1321
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001322 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001323 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001324 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001325 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001326
1327 // If the function takes variable number of arguments, make a frame index for
1328 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001329 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001330 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1331 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1332 }
1333 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001334 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1335
1336 // FIXME: We should really autogenerate these arrays
1337 static const unsigned GPR64ArgRegsWin64[] = {
1338 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001339 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001340 static const unsigned XMMArgRegsWin64[] = {
1341 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1342 };
1343 static const unsigned GPR64ArgRegs64Bit[] = {
1344 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1345 };
1346 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001347 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1348 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1349 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001350 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1351
1352 if (IsWin64) {
1353 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1354 GPR64ArgRegs = GPR64ArgRegsWin64;
1355 XMMArgRegs = XMMArgRegsWin64;
1356 } else {
1357 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1358 GPR64ArgRegs = GPR64ArgRegs64Bit;
1359 XMMArgRegs = XMMArgRegs64Bit;
1360 }
1361 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1362 TotalNumIntRegs);
1363 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1364 TotalNumXMMRegs);
1365
Gordon Henriksen18ace102008-01-05 16:56:59 +00001366 // For X86-64, if there are vararg parameters that are passed via
1367 // registers, then we must store them to their spots on the stack so they
1368 // may be loaded by deferencing the result of va_next.
1369 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001370 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1371 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1372 TotalNumXMMRegs * 16, 16);
1373
Gordon Henriksen18ace102008-01-05 16:56:59 +00001374 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001375 SmallVector<SDValue, 8> MemOps;
1376 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1377 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001378 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001379 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001380 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1381 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001382 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1383 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001384 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001385 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001386 MemOps.push_back(Store);
1387 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001388 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001389 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001390
Gordon Henriksen18ace102008-01-05 16:56:59 +00001391 // Now store the XMM (fp + vector) parameter registers.
1392 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001393 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001394 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001395 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1396 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001397 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1398 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001399 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001400 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001401 MemOps.push_back(Store);
1402 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001403 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001404 }
1405 if (!MemOps.empty())
1406 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1407 &MemOps[0], MemOps.size());
1408 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001409 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001410
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001411 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001412
Gordon Henriksen18ace102008-01-05 16:56:59 +00001413 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001414 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001415 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001416 BytesCallerReserves = 0;
1417 } else {
1418 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001419 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001420 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001421 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001422 BytesCallerReserves = StackSize;
1423 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001424
Gordon Henriksen18ace102008-01-05 16:56:59 +00001425 if (!Is64Bit) {
1426 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1427 if (CC == CallingConv::X86_FastCall)
1428 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1429 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001430
Anton Korobeynikove844e472007-08-15 17:12:32 +00001431 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001432
1433 // Return the new list of results.
Duncan Sands42d7bb82008-12-01 11:41:29 +00001434 return DAG.getNode(ISD::MERGE_VALUES, Op.getNode()->getVTList(),
1435 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001436}
1437
Dan Gohman8181bd12008-07-27 21:46:04 +00001438SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001439X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001440 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001441 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001442 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001443 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001444 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001445 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001446 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001447 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001448 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001449 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001450 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001451 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001452}
1453
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001454/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1455/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001456SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001457X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001458 SDValue &OutRetAddr,
1459 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001460 bool IsTailCall,
1461 bool Is64Bit,
1462 int FPDiff) {
1463 if (!IsTailCall || FPDiff==0) return Chain;
1464
1465 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001466 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001467 OutRetAddr = getReturnAddressFrameIndex(DAG);
1468 // Load the "old" Return address.
1469 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001470 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001471}
1472
1473/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1474/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001475static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001476EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001477 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001478 bool Is64Bit, int FPDiff) {
1479 // Store the return address to the appropriate stack slot.
1480 if (!FPDiff) return Chain;
1481 // Calculate the new stack slot for the return address.
1482 int SlotSize = Is64Bit ? 8 : 4;
1483 int NewReturnAddrFI =
1484 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001485 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001486 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001487 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001488 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001489 return Chain;
1490}
1491
Dan Gohman8181bd12008-07-27 21:46:04 +00001492SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001493 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001494 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1495 SDValue Chain = TheCall->getChain();
1496 unsigned CC = TheCall->getCallingConv();
1497 bool isVarArg = TheCall->isVarArg();
1498 bool IsTailCall = TheCall->isTailCall() &&
1499 CC == CallingConv::Fast && PerformTailCallOpt;
1500 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001501 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001502 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001503
1504 assert(!(isVarArg && CC == CallingConv::Fast) &&
1505 "Var args not supported with calling convention fastcc");
1506
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001507 // Analyze operands of the call, assigning locations to each operand.
1508 SmallVector<CCValAssign, 16> ArgLocs;
1509 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001510 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001511
1512 // Get a count of how many bytes are to be pushed on the stack.
1513 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001514 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001515 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001516
Gordon Henriksen18ace102008-01-05 16:56:59 +00001517 int FPDiff = 0;
1518 if (IsTailCall) {
1519 // Lower arguments at fp - stackoffset + fpdiff.
1520 unsigned NumBytesCallerPushed =
1521 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1522 FPDiff = NumBytesCallerPushed - NumBytes;
1523
1524 // Set the delta of movement of the returnaddr stackslot.
1525 // But only set if delta is greater than previous delta.
1526 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1527 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1528 }
1529
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001530 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001531
Dan Gohman8181bd12008-07-27 21:46:04 +00001532 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001533 // Load return adress for tail calls.
1534 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1535 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001536
Dan Gohman8181bd12008-07-27 21:46:04 +00001537 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1538 SmallVector<SDValue, 8> MemOpChains;
1539 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001540
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001541 // Walk the register/memloc assignments, inserting copies/loads. In the case
1542 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001543 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1544 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001545 SDValue Arg = TheCall->getArg(i);
1546 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1547 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001548
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001549 // Promote the value if needed.
1550 switch (VA.getLocInfo()) {
1551 default: assert(0 && "Unknown loc info!");
1552 case CCValAssign::Full: break;
1553 case CCValAssign::SExt:
1554 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1555 break;
1556 case CCValAssign::ZExt:
1557 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1558 break;
1559 case CCValAssign::AExt:
1560 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1561 break;
1562 }
1563
1564 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001565 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001566 MVT RegVT = VA.getLocVT();
1567 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001568 switch (VA.getLocReg()) {
1569 default:
1570 break;
1571 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1572 case X86::R8: {
1573 // Special case: passing MMX values in GPR registers.
1574 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1575 break;
1576 }
1577 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1578 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1579 // Special case: passing MMX values in XMM registers.
1580 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1581 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1582 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1583 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1584 getMOVLMask(2, DAG));
1585 break;
1586 }
1587 }
1588 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001589 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1590 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001591 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001592 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001593 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001594 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1595
Dan Gohman705e3f72008-09-13 01:54:27 +00001596 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1597 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001598 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001599 }
1600 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001601
1602 if (!MemOpChains.empty())
1603 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1604 &MemOpChains[0], MemOpChains.size());
1605
1606 // Build a sequence of copy-to-reg nodes chained together with token chain
1607 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001608 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001609 // Tail call byval lowering might overwrite argument registers so in case of
1610 // tail call optimization the copies to registers are lowered later.
1611 if (!IsTailCall)
1612 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1613 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1614 InFlag);
1615 InFlag = Chain.getValue(1);
1616 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001617
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001618 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001619 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001620 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1621 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1622 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1623 InFlag);
1624 InFlag = Chain.getValue(1);
1625 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001626 // If we are tail calling and generating PIC/GOT style code load the address
1627 // of the callee into ecx. The value in ecx is used as target of the tail
1628 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1629 // calls on PIC/GOT architectures. Normally we would just put the address of
1630 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1631 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001632 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001633 // Note: The actual moving to ecx is done further down.
1634 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001635 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001636 !G->getGlobal()->hasProtectedVisibility())
1637 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001638 else if (isa<ExternalSymbolSDNode>(Callee))
1639 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001640 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001641
Gordon Henriksen18ace102008-01-05 16:56:59 +00001642 if (Is64Bit && isVarArg) {
1643 // From AMD64 ABI document:
1644 // For calls that may call functions that use varargs or stdargs
1645 // (prototype-less calls or calls to functions containing ellipsis (...) in
1646 // the declaration) %al is used as hidden argument to specify the number
1647 // of SSE registers used. The contents of %al do not need to match exactly
1648 // the number of registers, but must be an ubound on the number of SSE
1649 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001650
1651 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001652 // Count the number of XMM registers allocated.
1653 static const unsigned XMMArgRegs[] = {
1654 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1655 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1656 };
1657 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1658
1659 Chain = DAG.getCopyToReg(Chain, X86::AL,
1660 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1661 InFlag = Chain.getValue(1);
1662 }
1663
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001664
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001665 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001666 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001667 SmallVector<SDValue, 8> MemOpChains2;
1668 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001669 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001670 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001671 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001672 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1673 CCValAssign &VA = ArgLocs[i];
1674 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001675 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001676 SDValue Arg = TheCall->getArg(i);
1677 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001678 // Create frame index.
1679 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001680 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001681 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001682 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001683
Duncan Sandsc93fae32008-03-21 09:14:45 +00001684 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001685 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001686 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001687 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001688 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1689 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1690
1691 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001692 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001693 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001694 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001695 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001696 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001697 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001698 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001699 }
1700 }
1701
1702 if (!MemOpChains2.empty())
1703 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001704 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001705
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001706 // Copy arguments to their registers.
1707 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1708 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1709 InFlag);
1710 InFlag = Chain.getValue(1);
1711 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001712 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001713
Gordon Henriksen18ace102008-01-05 16:56:59 +00001714 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001715 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1716 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001717 }
1718
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001719 // If the callee is a GlobalAddress node (quite common, every direct call is)
1720 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1721 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1722 // We should use extra load for direct calls to dllimported functions in
1723 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001724 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1725 getTargetMachine(), true))
Dan Gohman36322c72008-10-18 02:06:02 +00001726 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1727 G->getOffset());
Bill Wendlingfef06052008-09-16 21:48:12 +00001728 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1729 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001730 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001731 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001732
1733 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001734 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001735 Callee,InFlag);
1736 Callee = DAG.getRegister(Opc, getPointerTy());
1737 // Add register as live out.
1738 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001739 }
1740
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001741 // Returns a chain & a flag for retval copy to use.
1742 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001743 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001744
1745 if (IsTailCall) {
1746 Ops.push_back(Chain);
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001747 Ops.push_back(DAG.getIntPtrConstant(NumBytes, true));
1748 Ops.push_back(DAG.getIntPtrConstant(0, true));
Gabor Greif1c80d112008-08-28 21:40:38 +00001749 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001750 Ops.push_back(InFlag);
1751 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1752 InFlag = Chain.getValue(1);
1753
1754 // Returns a chain & a flag for retval copy to use.
1755 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1756 Ops.clear();
1757 }
1758
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001759 Ops.push_back(Chain);
1760 Ops.push_back(Callee);
1761
Gordon Henriksen18ace102008-01-05 16:56:59 +00001762 if (IsTailCall)
1763 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001764
Gordon Henriksen18ace102008-01-05 16:56:59 +00001765 // Add argument registers to the end of the list so that they are known live
1766 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001767 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1768 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1769 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001770
Evan Cheng8ba45e62008-03-18 23:36:35 +00001771 // Add an implicit use GOT pointer in EBX.
1772 if (!IsTailCall && !Is64Bit &&
1773 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1774 Subtarget->isPICStyleGOT())
1775 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1776
1777 // Add an implicit use of AL for x86 vararg functions.
1778 if (Is64Bit && isVarArg)
1779 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1780
Gabor Greif1c80d112008-08-28 21:40:38 +00001781 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001782 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001783
Gordon Henriksen18ace102008-01-05 16:56:59 +00001784 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001785 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001786 "Flag must be set. Depend on flag being set in LowerRET");
1787 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman705e3f72008-09-13 01:54:27 +00001788 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001789
Gabor Greif1c80d112008-08-28 21:40:38 +00001790 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001791 }
1792
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001793 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001794 InFlag = Chain.getValue(1);
1795
1796 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001797 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001798 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001799 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001800 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001801 // If this is is a call to a struct-return function, the callee
1802 // pops the hidden struct pointer, so we have to push it back.
1803 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001804 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001805 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001806 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001807
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001808 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001809 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001810 DAG.getIntPtrConstant(NumBytes, true),
1811 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1812 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001813 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001814 InFlag = Chain.getValue(1);
1815
1816 // Handle result values, copying them out of physregs into vregs that we
1817 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001818 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001819 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001820}
1821
1822
1823//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001824// Fast Calling Convention (tail call) implementation
1825//===----------------------------------------------------------------------===//
1826
1827// Like std call, callee cleans arguments, convention except that ECX is
1828// reserved for storing the tail called function address. Only 2 registers are
1829// free for argument passing (inreg). Tail call optimization is performed
1830// provided:
1831// * tailcallopt is enabled
1832// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001833// On X86_64 architecture with GOT-style position independent code only local
1834// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001835// To keep the stack aligned according to platform abi the function
1836// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1837// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001838// If a tail called function callee has more arguments than the caller the
1839// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001840// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001841// original REtADDR, but before the saved framepointer or the spilled registers
1842// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1843// stack layout:
1844// arg1
1845// arg2
1846// RETADDR
1847// [ new RETADDR
1848// move area ]
1849// (possible EBP)
1850// ESI
1851// EDI
1852// local1 ..
1853
1854/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1855/// for a 16 byte align requirement.
1856unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1857 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001858 MachineFunction &MF = DAG.getMachineFunction();
1859 const TargetMachine &TM = MF.getTarget();
1860 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1861 unsigned StackAlignment = TFI.getStackAlignment();
1862 uint64_t AlignMask = StackAlignment - 1;
1863 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001864 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001865 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1866 // Number smaller than 12 so just add the difference.
1867 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1868 } else {
1869 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1870 Offset = ((~AlignMask) & Offset) + StackAlignment +
1871 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001872 }
Evan Chengded8f902008-09-07 09:07:23 +00001873 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001874}
1875
1876/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001877/// following the call is a return. A function is eligible if caller/callee
1878/// calling conventions match, currently only fastcc supports tail calls, and
1879/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001880bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001881 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001882 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001883 if (!PerformTailCallOpt)
1884 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001885
Dan Gohman705e3f72008-09-13 01:54:27 +00001886 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001887 MachineFunction &MF = DAG.getMachineFunction();
1888 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001889 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001890 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001891 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001892 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001893 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001894 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001895 return true;
1896
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001897 // Can only do local tail calls (in same module, hidden or protected) on
1898 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001899 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1900 return G->getGlobal()->hasHiddenVisibility()
1901 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001902 }
1903 }
Evan Chenge7a87392007-11-02 01:26:22 +00001904
1905 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001906}
1907
Dan Gohmanca4857a2008-09-03 23:12:08 +00001908FastISel *
1909X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001910 MachineModuleInfo *mmo,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001911 DenseMap<const Value *, unsigned> &vm,
1912 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001913 MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00001914 DenseMap<const AllocaInst *, int> &am
1915#ifndef NDEBUG
1916 , SmallSet<Instruction*, 8> &cil
1917#endif
1918 ) {
1919 return X86::createFastISel(mf, mmo, vm, bm, am
1920#ifndef NDEBUG
1921 , cil
1922#endif
1923 );
Dan Gohman97805ee2008-08-19 21:32:53 +00001924}
1925
1926
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001927//===----------------------------------------------------------------------===//
1928// Other Lowering Hooks
1929//===----------------------------------------------------------------------===//
1930
1931
Dan Gohman8181bd12008-07-27 21:46:04 +00001932SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001933 MachineFunction &MF = DAG.getMachineFunction();
1934 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1935 int ReturnAddrIndex = FuncInfo->getRAIndex();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001936 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikove844e472007-08-15 17:12:32 +00001937
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001938 if (ReturnAddrIndex == 0) {
1939 // Set up a frame object for the return address.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001940 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001941 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001942 }
1943
1944 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1945}
1946
1947
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001948/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1949/// specific condition code. It returns a false if it cannot do a direct
1950/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1951/// needed.
1952static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001953 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001954 SelectionDAG &DAG) {
1955 X86CC = X86::COND_INVALID;
1956 if (!isFP) {
1957 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1958 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1959 // X > -1 -> X == 0, jump !sign.
1960 RHS = DAG.getConstant(0, RHS.getValueType());
1961 X86CC = X86::COND_NS;
1962 return true;
1963 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1964 // X < 0 -> X == 0, jump on sign.
1965 X86CC = X86::COND_S;
1966 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001967 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00001968 // X < 1 -> X <= 0
1969 RHS = DAG.getConstant(0, RHS.getValueType());
1970 X86CC = X86::COND_LE;
1971 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001972 }
1973 }
1974
1975 switch (SetCCOpcode) {
1976 default: break;
1977 case ISD::SETEQ: X86CC = X86::COND_E; break;
1978 case ISD::SETGT: X86CC = X86::COND_G; break;
1979 case ISD::SETGE: X86CC = X86::COND_GE; break;
1980 case ISD::SETLT: X86CC = X86::COND_L; break;
1981 case ISD::SETLE: X86CC = X86::COND_LE; break;
1982 case ISD::SETNE: X86CC = X86::COND_NE; break;
1983 case ISD::SETULT: X86CC = X86::COND_B; break;
1984 case ISD::SETUGT: X86CC = X86::COND_A; break;
1985 case ISD::SETULE: X86CC = X86::COND_BE; break;
1986 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1987 }
1988 } else {
Duncan Sandsc2a04622008-10-24 13:03:10 +00001989 // First determine if it is required or is profitable to flip the operands.
1990
1991 // If LHS is a foldable load, but RHS is not, flip the condition.
1992 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1993 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1994 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1995 std::swap(LHS, RHS);
1996 }
1997
Evan Chengb488ca32008-08-29 23:22:12 +00001998 switch (SetCCOpcode) {
1999 default: break;
2000 case ISD::SETOLT:
2001 case ISD::SETOLE:
2002 case ISD::SETUGT:
2003 case ISD::SETUGE:
Duncan Sandsc2a04622008-10-24 13:03:10 +00002004 std::swap(LHS, RHS);
Evan Chengb488ca32008-08-29 23:22:12 +00002005 break;
2006 }
2007
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002008 // On a floating point condition, the flags are set as follows:
2009 // ZF PF CF op
2010 // 0 | 0 | 0 | X > Y
2011 // 0 | 0 | 1 | X < Y
2012 // 1 | 0 | 0 | X == Y
2013 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002014 switch (SetCCOpcode) {
2015 default: break;
2016 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00002017 case ISD::SETEQ:
2018 X86CC = X86::COND_E;
2019 break;
2020 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002021 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00002022 case ISD::SETGT:
2023 X86CC = X86::COND_A;
2024 break;
2025 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002026 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00002027 case ISD::SETGE:
2028 X86CC = X86::COND_AE;
2029 break;
2030 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002031 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00002032 case ISD::SETLT:
2033 X86CC = X86::COND_B;
2034 break;
2035 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002036 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00002037 case ISD::SETLE:
2038 X86CC = X86::COND_BE;
2039 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002040 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00002041 case ISD::SETNE:
2042 X86CC = X86::COND_NE;
2043 break;
2044 case ISD::SETUO:
2045 X86CC = X86::COND_P;
2046 break;
2047 case ISD::SETO:
2048 X86CC = X86::COND_NP;
2049 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002050 }
Evan Chengfc937c92008-08-28 23:48:31 +00002051 }
2052
Evan Chengc6162692008-08-29 22:13:21 +00002053 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002054}
2055
2056/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2057/// code. Current x86 isa includes the following FP cmov instructions:
2058/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2059static bool hasFPCMov(unsigned X86CC) {
2060 switch (X86CC) {
2061 default:
2062 return false;
2063 case X86::COND_B:
2064 case X86::COND_BE:
2065 case X86::COND_E:
2066 case X86::COND_P:
2067 case X86::COND_A:
2068 case X86::COND_AE:
2069 case X86::COND_NE:
2070 case X86::COND_NP:
2071 return true;
2072 }
2073}
2074
2075/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2076/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002077static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002078 if (Op.getOpcode() == ISD::UNDEF)
2079 return true;
2080
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002081 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002082 return (Val >= Low && Val < Hi);
2083}
2084
2085/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2086/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002087static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002088 if (Op.getOpcode() == ISD::UNDEF)
2089 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002090 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002091}
2092
2093/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2094/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2095bool X86::isPSHUFDMask(SDNode *N) {
2096 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2097
Dan Gohman7dc19012007-08-02 21:17:01 +00002098 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002099 return false;
2100
2101 // Check if the value doesn't reference the second vector.
2102 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002103 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002104 if (Arg.getOpcode() == ISD::UNDEF) continue;
2105 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002106 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002107 return false;
2108 }
2109
2110 return true;
2111}
2112
2113/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2114/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2115bool X86::isPSHUFHWMask(SDNode *N) {
2116 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2117
2118 if (N->getNumOperands() != 8)
2119 return false;
2120
2121 // Lower quadword copied in order.
2122 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002123 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002124 if (Arg.getOpcode() == ISD::UNDEF) continue;
2125 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002126 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002127 return false;
2128 }
2129
2130 // Upper quadword shuffled.
2131 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002132 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002133 if (Arg.getOpcode() == ISD::UNDEF) continue;
2134 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002135 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002136 if (Val < 4 || Val > 7)
2137 return false;
2138 }
2139
2140 return true;
2141}
2142
2143/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2144/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2145bool X86::isPSHUFLWMask(SDNode *N) {
2146 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2147
2148 if (N->getNumOperands() != 8)
2149 return false;
2150
2151 // Upper quadword copied in order.
2152 for (unsigned i = 4; i != 8; ++i)
2153 if (!isUndefOrEqual(N->getOperand(i), i))
2154 return false;
2155
2156 // Lower quadword shuffled.
2157 for (unsigned i = 0; i != 4; ++i)
2158 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2159 return false;
2160
2161 return true;
2162}
2163
2164/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2165/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002166static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002167 if (NumElems != 2 && NumElems != 4) return false;
2168
2169 unsigned Half = NumElems / 2;
2170 for (unsigned i = 0; i < Half; ++i)
2171 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2172 return false;
2173 for (unsigned i = Half; i < NumElems; ++i)
2174 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2175 return false;
2176
2177 return true;
2178}
2179
2180bool X86::isSHUFPMask(SDNode *N) {
2181 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2182 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2183}
2184
2185/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2186/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2187/// half elements to come from vector 1 (which would equal the dest.) and
2188/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002189static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002190 if (NumOps != 2 && NumOps != 4) return false;
2191
2192 unsigned Half = NumOps / 2;
2193 for (unsigned i = 0; i < Half; ++i)
2194 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2195 return false;
2196 for (unsigned i = Half; i < NumOps; ++i)
2197 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2198 return false;
2199 return true;
2200}
2201
2202static bool isCommutedSHUFP(SDNode *N) {
2203 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2204 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2205}
2206
2207/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2208/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2209bool X86::isMOVHLPSMask(SDNode *N) {
2210 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2211
2212 if (N->getNumOperands() != 4)
2213 return false;
2214
2215 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2216 return isUndefOrEqual(N->getOperand(0), 6) &&
2217 isUndefOrEqual(N->getOperand(1), 7) &&
2218 isUndefOrEqual(N->getOperand(2), 2) &&
2219 isUndefOrEqual(N->getOperand(3), 3);
2220}
2221
2222/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2223/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2224/// <2, 3, 2, 3>
2225bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2226 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2227
2228 if (N->getNumOperands() != 4)
2229 return false;
2230
2231 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2232 return isUndefOrEqual(N->getOperand(0), 2) &&
2233 isUndefOrEqual(N->getOperand(1), 3) &&
2234 isUndefOrEqual(N->getOperand(2), 2) &&
2235 isUndefOrEqual(N->getOperand(3), 3);
2236}
2237
2238/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2239/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2240bool X86::isMOVLPMask(SDNode *N) {
2241 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2242
2243 unsigned NumElems = N->getNumOperands();
2244 if (NumElems != 2 && NumElems != 4)
2245 return false;
2246
2247 for (unsigned i = 0; i < NumElems/2; ++i)
2248 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2249 return false;
2250
2251 for (unsigned i = NumElems/2; i < NumElems; ++i)
2252 if (!isUndefOrEqual(N->getOperand(i), i))
2253 return false;
2254
2255 return true;
2256}
2257
2258/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2259/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2260/// and MOVLHPS.
2261bool X86::isMOVHPMask(SDNode *N) {
2262 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2263
2264 unsigned NumElems = N->getNumOperands();
2265 if (NumElems != 2 && NumElems != 4)
2266 return false;
2267
2268 for (unsigned i = 0; i < NumElems/2; ++i)
2269 if (!isUndefOrEqual(N->getOperand(i), i))
2270 return false;
2271
2272 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002273 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002274 if (!isUndefOrEqual(Arg, i + NumElems))
2275 return false;
2276 }
2277
2278 return true;
2279}
2280
2281/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2282/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002283bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002284 bool V2IsSplat = false) {
2285 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2286 return false;
2287
2288 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002289 SDValue BitI = Elts[i];
2290 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002291 if (!isUndefOrEqual(BitI, j))
2292 return false;
2293 if (V2IsSplat) {
2294 if (isUndefOrEqual(BitI1, NumElts))
2295 return false;
2296 } else {
2297 if (!isUndefOrEqual(BitI1, j + NumElts))
2298 return false;
2299 }
2300 }
2301
2302 return true;
2303}
2304
2305bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2306 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2307 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2308}
2309
2310/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2311/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002312bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002313 bool V2IsSplat = false) {
2314 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2315 return false;
2316
2317 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002318 SDValue BitI = Elts[i];
2319 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002320 if (!isUndefOrEqual(BitI, j + NumElts/2))
2321 return false;
2322 if (V2IsSplat) {
2323 if (isUndefOrEqual(BitI1, NumElts))
2324 return false;
2325 } else {
2326 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2327 return false;
2328 }
2329 }
2330
2331 return true;
2332}
2333
2334bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2335 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2336 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2337}
2338
2339/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2340/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2341/// <0, 0, 1, 1>
2342bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2343 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2344
2345 unsigned NumElems = N->getNumOperands();
2346 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2347 return false;
2348
2349 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002350 SDValue BitI = N->getOperand(i);
2351 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002352
2353 if (!isUndefOrEqual(BitI, j))
2354 return false;
2355 if (!isUndefOrEqual(BitI1, j))
2356 return false;
2357 }
2358
2359 return true;
2360}
2361
2362/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2363/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2364/// <2, 2, 3, 3>
2365bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2366 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2367
2368 unsigned NumElems = N->getNumOperands();
2369 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2370 return false;
2371
2372 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002373 SDValue BitI = N->getOperand(i);
2374 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002375
2376 if (!isUndefOrEqual(BitI, j))
2377 return false;
2378 if (!isUndefOrEqual(BitI1, j))
2379 return false;
2380 }
2381
2382 return true;
2383}
2384
2385/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2386/// specifies a shuffle of elements that is suitable for input to MOVSS,
2387/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002388static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002389 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002390 return false;
2391
2392 if (!isUndefOrEqual(Elts[0], NumElts))
2393 return false;
2394
2395 for (unsigned i = 1; i < NumElts; ++i) {
2396 if (!isUndefOrEqual(Elts[i], i))
2397 return false;
2398 }
2399
2400 return true;
2401}
2402
2403bool X86::isMOVLMask(SDNode *N) {
2404 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2405 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2406}
2407
2408/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2409/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2410/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002411static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002412 bool V2IsSplat = false,
2413 bool V2IsUndef = false) {
2414 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2415 return false;
2416
2417 if (!isUndefOrEqual(Ops[0], 0))
2418 return false;
2419
2420 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002421 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002422 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2423 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2424 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2425 return false;
2426 }
2427
2428 return true;
2429}
2430
2431static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2432 bool V2IsUndef = false) {
2433 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2434 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2435 V2IsSplat, V2IsUndef);
2436}
2437
2438/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2439/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2440bool X86::isMOVSHDUPMask(SDNode *N) {
2441 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2442
2443 if (N->getNumOperands() != 4)
2444 return false;
2445
2446 // Expect 1, 1, 3, 3
2447 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002448 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002449 if (Arg.getOpcode() == ISD::UNDEF) continue;
2450 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002451 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002452 if (Val != 1) return false;
2453 }
2454
2455 bool HasHi = false;
2456 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002457 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002458 if (Arg.getOpcode() == ISD::UNDEF) continue;
2459 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002460 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002461 if (Val != 3) return false;
2462 HasHi = true;
2463 }
2464
2465 // Don't use movshdup if it can be done with a shufps.
2466 return HasHi;
2467}
2468
2469/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2470/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2471bool X86::isMOVSLDUPMask(SDNode *N) {
2472 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2473
2474 if (N->getNumOperands() != 4)
2475 return false;
2476
2477 // Expect 0, 0, 2, 2
2478 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002479 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002480 if (Arg.getOpcode() == ISD::UNDEF) continue;
2481 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002482 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002483 if (Val != 0) return false;
2484 }
2485
2486 bool HasHi = false;
2487 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002488 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002489 if (Arg.getOpcode() == ISD::UNDEF) continue;
2490 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002491 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002492 if (Val != 2) return false;
2493 HasHi = true;
2494 }
2495
2496 // Don't use movshdup if it can be done with a shufps.
2497 return HasHi;
2498}
2499
2500/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2501/// specifies a identity operation on the LHS or RHS.
2502static bool isIdentityMask(SDNode *N, bool RHS = false) {
2503 unsigned NumElems = N->getNumOperands();
2504 for (unsigned i = 0; i < NumElems; ++i)
2505 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2506 return false;
2507 return true;
2508}
2509
2510/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2511/// a splat of a single element.
2512static bool isSplatMask(SDNode *N) {
2513 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2514
2515 // This is a splat operation if each element of the permute is the same, and
2516 // if the value doesn't reference the second vector.
2517 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002518 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002519 unsigned i = 0;
2520 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002521 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002522 if (isa<ConstantSDNode>(Elt)) {
2523 ElementBase = Elt;
2524 break;
2525 }
2526 }
2527
Gabor Greif1c80d112008-08-28 21:40:38 +00002528 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002529 return false;
2530
2531 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002532 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002533 if (Arg.getOpcode() == ISD::UNDEF) continue;
2534 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2535 if (Arg != ElementBase) return false;
2536 }
2537
2538 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002539 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002540}
2541
2542/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2543/// a splat of a single element and it's a 2 or 4 element mask.
2544bool X86::isSplatMask(SDNode *N) {
2545 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2546
2547 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2548 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2549 return false;
2550 return ::isSplatMask(N);
2551}
2552
2553/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2554/// specifies a splat of zero element.
2555bool X86::isSplatLoMask(SDNode *N) {
2556 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2557
2558 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2559 if (!isUndefOrEqual(N->getOperand(i), 0))
2560 return false;
2561 return true;
2562}
2563
Evan Chenga2497eb2008-09-25 20:50:48 +00002564/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2565/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2566bool X86::isMOVDDUPMask(SDNode *N) {
2567 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2568
2569 unsigned e = N->getNumOperands() / 2;
2570 for (unsigned i = 0; i < e; ++i)
2571 if (!isUndefOrEqual(N->getOperand(i), i))
2572 return false;
2573 for (unsigned i = 0; i < e; ++i)
2574 if (!isUndefOrEqual(N->getOperand(e+i), i))
2575 return false;
2576 return true;
2577}
2578
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002579/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2580/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2581/// instructions.
2582unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2583 unsigned NumOperands = N->getNumOperands();
2584 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2585 unsigned Mask = 0;
2586 for (unsigned i = 0; i < NumOperands; ++i) {
2587 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002588 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002589 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002590 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002591 if (Val >= NumOperands) Val -= NumOperands;
2592 Mask |= Val;
2593 if (i != NumOperands - 1)
2594 Mask <<= Shift;
2595 }
2596
2597 return Mask;
2598}
2599
2600/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2601/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2602/// instructions.
2603unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2604 unsigned Mask = 0;
2605 // 8 nodes, but we only care about the last 4.
2606 for (unsigned i = 7; i >= 4; --i) {
2607 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002608 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002609 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002610 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002611 Mask |= (Val - 4);
2612 if (i != 4)
2613 Mask <<= 2;
2614 }
2615
2616 return Mask;
2617}
2618
2619/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2620/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2621/// instructions.
2622unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2623 unsigned Mask = 0;
2624 // 8 nodes, but we only care about the first 4.
2625 for (int i = 3; i >= 0; --i) {
2626 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002627 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002628 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002629 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002630 Mask |= Val;
2631 if (i != 0)
2632 Mask <<= 2;
2633 }
2634
2635 return Mask;
2636}
2637
2638/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2639/// specifies a 8 element shuffle that can be broken into a pair of
2640/// PSHUFHW and PSHUFLW.
2641static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2642 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2643
2644 if (N->getNumOperands() != 8)
2645 return false;
2646
2647 // Lower quadword shuffled.
2648 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002649 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002650 if (Arg.getOpcode() == ISD::UNDEF) continue;
2651 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002652 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002653 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002654 return false;
2655 }
2656
2657 // Upper quadword shuffled.
2658 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002659 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002660 if (Arg.getOpcode() == ISD::UNDEF) continue;
2661 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002662 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002663 if (Val < 4 || Val > 7)
2664 return false;
2665 }
2666
2667 return true;
2668}
2669
Chris Lattnere6aa3862007-11-25 00:24:49 +00002670/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002671/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002672static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2673 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002674 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002675 MVT VT = Op.getValueType();
2676 MVT MaskVT = Mask.getValueType();
2677 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002678 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002679 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002680
2681 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002682 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002683 if (Arg.getOpcode() == ISD::UNDEF) {
2684 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2685 continue;
2686 }
2687 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002688 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002689 if (Val < NumElems)
2690 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2691 else
2692 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2693 }
2694
2695 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002696 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002697 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2698}
2699
Evan Chenga6769df2007-12-07 21:30:01 +00002700/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2701/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002702static
Dan Gohman8181bd12008-07-27 21:46:04 +00002703SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002704 MVT MaskVT = Mask.getValueType();
2705 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002706 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002707 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002708 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002709 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002710 if (Arg.getOpcode() == ISD::UNDEF) {
2711 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2712 continue;
2713 }
2714 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002715 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002716 if (Val < NumElems)
2717 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2718 else
2719 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2720 }
2721 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2722}
2723
2724
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002725/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2726/// match movhlps. The lower half elements should come from upper half of
2727/// V1 (and in order), and the upper half elements should come from the upper
2728/// half of V2 (and in order).
2729static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2730 unsigned NumElems = Mask->getNumOperands();
2731 if (NumElems != 4)
2732 return false;
2733 for (unsigned i = 0, e = 2; i != e; ++i)
2734 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2735 return false;
2736 for (unsigned i = 2; i != 4; ++i)
2737 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2738 return false;
2739 return true;
2740}
2741
2742/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002743/// is promoted to a vector. It also returns the LoadSDNode by reference if
2744/// required.
2745static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002746 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2747 return false;
2748 N = N->getOperand(0).getNode();
2749 if (!ISD::isNON_EXTLoad(N))
2750 return false;
2751 if (LD)
2752 *LD = cast<LoadSDNode>(N);
2753 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002754}
2755
2756/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2757/// match movlp{s|d}. The lower half elements should come from lower half of
2758/// V1 (and in order), and the upper half elements should come from the upper
2759/// half of V2 (and in order). And since V1 will become the source of the
2760/// MOVLP, it must be either a vector load or a scalar load to vector.
2761static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2762 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2763 return false;
2764 // Is V2 is a vector load, don't do this transformation. We will try to use
2765 // load folding shufps op.
2766 if (ISD::isNON_EXTLoad(V2))
2767 return false;
2768
2769 unsigned NumElems = Mask->getNumOperands();
2770 if (NumElems != 2 && NumElems != 4)
2771 return false;
2772 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2773 if (!isUndefOrEqual(Mask->getOperand(i), i))
2774 return false;
2775 for (unsigned i = NumElems/2; i != NumElems; ++i)
2776 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2777 return false;
2778 return true;
2779}
2780
2781/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2782/// all the same.
2783static bool isSplatVector(SDNode *N) {
2784 if (N->getOpcode() != ISD::BUILD_VECTOR)
2785 return false;
2786
Dan Gohman8181bd12008-07-27 21:46:04 +00002787 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002788 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2789 if (N->getOperand(i) != SplatValue)
2790 return false;
2791 return true;
2792}
2793
2794/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2795/// to an undef.
2796static bool isUndefShuffle(SDNode *N) {
2797 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2798 return false;
2799
Dan Gohman8181bd12008-07-27 21:46:04 +00002800 SDValue V1 = N->getOperand(0);
2801 SDValue V2 = N->getOperand(1);
2802 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002803 unsigned NumElems = Mask.getNumOperands();
2804 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002805 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002806 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002807 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002808 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2809 return false;
2810 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2811 return false;
2812 }
2813 }
2814 return true;
2815}
2816
2817/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2818/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002819static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002820 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002821 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002822 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002823 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002824}
2825
2826/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2827/// to an zero vector.
2828static bool isZeroShuffle(SDNode *N) {
2829 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2830 return false;
2831
Dan Gohman8181bd12008-07-27 21:46:04 +00002832 SDValue V1 = N->getOperand(0);
2833 SDValue V2 = N->getOperand(1);
2834 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835 unsigned NumElems = Mask.getNumOperands();
2836 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002837 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002838 if (Arg.getOpcode() == ISD::UNDEF)
2839 continue;
2840
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002841 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002842 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002843 unsigned Opc = V1.getNode()->getOpcode();
2844 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002845 continue;
2846 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002847 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002848 return false;
2849 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002850 unsigned Opc = V2.getNode()->getOpcode();
2851 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002852 continue;
2853 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002854 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002855 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002856 }
2857 }
2858 return true;
2859}
2860
2861/// getZeroVector - Returns a vector of specified type with all zero elements.
2862///
Dan Gohman8181bd12008-07-27 21:46:04 +00002863static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002864 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002865
2866 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2867 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002868 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002869 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002870 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002871 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002872 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002873 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002874 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002875 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002876 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002877 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2878 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002879 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002880}
2881
Chris Lattnere6aa3862007-11-25 00:24:49 +00002882/// getOnesVector - Returns a vector of specified type with all bits set.
2883///
Dan Gohman8181bd12008-07-27 21:46:04 +00002884static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002885 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002886
2887 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2888 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002889 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2890 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002891 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002892 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2893 else // SSE
2894 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2895 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2896}
2897
2898
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002899/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2900/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002901static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002902 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2903
2904 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002905 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002906 unsigned NumElems = Mask.getNumOperands();
2907 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002908 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002909 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002910 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002911 if (Val > NumElems) {
2912 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2913 Changed = true;
2914 }
2915 }
2916 MaskVec.push_back(Arg);
2917 }
2918
2919 if (Changed)
2920 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2921 &MaskVec[0], MaskVec.size());
2922 return Mask;
2923}
2924
2925/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2926/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002927static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002928 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2929 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002930
Dan Gohman8181bd12008-07-27 21:46:04 +00002931 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002932 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2933 for (unsigned i = 1; i != NumElems; ++i)
2934 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2935 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2936}
2937
2938/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2939/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002940static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002941 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2942 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002943 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002944 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2945 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2946 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2947 }
2948 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2949}
2950
2951/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2952/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002953static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002954 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2955 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002956 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002957 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002958 for (unsigned i = 0; i != Half; ++i) {
2959 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2960 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2961 }
2962 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2963}
2964
Chris Lattner2d91b962008-03-09 01:05:04 +00002965/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2966/// element #0 of a vector with the specified index, leaving the rest of the
2967/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002968static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002969 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002970 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2971 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002972 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002973 // Element #0 of the result gets the elt we are replacing.
2974 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2975 for (unsigned i = 1; i != NumElems; ++i)
2976 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2977 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2978}
2979
Evan Chengbf8b2c52008-04-05 00:30:36 +00002980/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002981static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002982 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2983 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002984 if (PVT == VT)
2985 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002986 SDValue V1 = Op.getOperand(0);
2987 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002988 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002989 // Special handling of v4f32 -> v4i32.
2990 if (VT != MVT::v4f32) {
2991 Mask = getUnpacklMask(NumElems, DAG);
2992 while (NumElems > 4) {
2993 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2994 NumElems >>= 1;
2995 }
Evan Cheng8c590372008-05-15 08:39:06 +00002996 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002997 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002998
Evan Chengbf8b2c52008-04-05 00:30:36 +00002999 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00003000 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00003001 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003002 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3003}
3004
Evan Chenga2497eb2008-09-25 20:50:48 +00003005/// isVectorLoad - Returns true if the node is a vector load, a scalar
3006/// load that's promoted to vector, or a load bitcasted.
3007static bool isVectorLoad(SDValue Op) {
3008 assert(Op.getValueType().isVector() && "Expected a vector type");
3009 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
3010 Op.getOpcode() == ISD::BIT_CONVERT) {
3011 return isa<LoadSDNode>(Op.getOperand(0));
3012 }
3013 return isa<LoadSDNode>(Op);
3014}
3015
3016
3017/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3018///
3019static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3020 SelectionDAG &DAG, bool HasSSE3) {
3021 // If we have sse3 and shuffle has more than one use or input is a load, then
3022 // use movddup. Otherwise, use movlhps.
3023 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3024 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3025 MVT VT = Op.getValueType();
3026 if (VT == PVT)
3027 return Op;
3028 unsigned NumElems = PVT.getVectorNumElements();
3029 if (NumElems == 2) {
3030 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3031 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
3032 } else {
3033 assert(NumElems == 4);
3034 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3035 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
3036 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst0, Cst1, Cst0, Cst1);
3037 }
3038
3039 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
3040 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
3041 DAG.getNode(ISD::UNDEF, PVT), Mask);
3042 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3043}
3044
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003045/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003046/// vector of zero or undef vector. This produces a shuffle where the low
3047/// element of V2 is swizzled into the zero/undef vector, landing at element
3048/// 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 +00003049static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003050 bool isZero, bool HasSSE2,
3051 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003052 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003053 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00003054 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003055 unsigned NumElems = V2.getValueType().getVectorNumElements();
3056 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3057 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003058 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003059 for (unsigned i = 0; i != NumElems; ++i)
3060 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3061 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3062 else
3063 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003064 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065 &MaskVec[0], MaskVec.size());
3066 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3067}
3068
Evan Chengdea99362008-05-29 08:22:04 +00003069/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3070/// a shuffle that is zero.
3071static
Dan Gohman8181bd12008-07-27 21:46:04 +00003072unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003073 unsigned NumElems, bool Low,
3074 SelectionDAG &DAG) {
3075 unsigned NumZeros = 0;
3076 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003077 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003078 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003079 if (Idx.getOpcode() == ISD::UNDEF) {
3080 ++NumZeros;
3081 continue;
3082 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003083 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3084 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003085 ++NumZeros;
3086 else
3087 break;
3088 }
3089 return NumZeros;
3090}
3091
3092/// isVectorShift - Returns true if the shuffle can be implemented as a
3093/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003094static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3095 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003096 unsigned NumElems = Mask.getNumOperands();
3097
3098 isLeft = true;
3099 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3100 if (!NumZeros) {
3101 isLeft = false;
3102 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3103 if (!NumZeros)
3104 return false;
3105 }
3106
3107 bool SeenV1 = false;
3108 bool SeenV2 = false;
3109 for (unsigned i = NumZeros; i < NumElems; ++i) {
3110 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003111 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003112 if (Idx.getOpcode() == ISD::UNDEF)
3113 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003114 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003115 if (Index < NumElems)
3116 SeenV1 = true;
3117 else {
3118 Index -= NumElems;
3119 SeenV2 = true;
3120 }
3121 if (Index != Val)
3122 return false;
3123 }
3124 if (SeenV1 && SeenV2)
3125 return false;
3126
3127 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3128 ShAmt = NumZeros;
3129 return true;
3130}
3131
3132
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003133/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3134///
Dan Gohman8181bd12008-07-27 21:46:04 +00003135static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003136 unsigned NumNonZero, unsigned NumZero,
3137 SelectionDAG &DAG, TargetLowering &TLI) {
3138 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003139 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003140
Dan Gohman8181bd12008-07-27 21:46:04 +00003141 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003142 bool First = true;
3143 for (unsigned i = 0; i < 16; ++i) {
3144 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3145 if (ThisIsNonZero && First) {
3146 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003147 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003148 else
3149 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3150 First = false;
3151 }
3152
3153 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003154 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003155 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3156 if (LastIsNonZero) {
3157 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3158 }
3159 if (ThisIsNonZero) {
3160 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3161 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3162 ThisElt, DAG.getConstant(8, MVT::i8));
3163 if (LastIsNonZero)
3164 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3165 } else
3166 ThisElt = LastElt;
3167
Gabor Greif1c80d112008-08-28 21:40:38 +00003168 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003169 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003170 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003171 }
3172 }
3173
3174 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3175}
3176
3177/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3178///
Dan Gohman8181bd12008-07-27 21:46:04 +00003179static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003180 unsigned NumNonZero, unsigned NumZero,
3181 SelectionDAG &DAG, TargetLowering &TLI) {
3182 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003183 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003184
Dan Gohman8181bd12008-07-27 21:46:04 +00003185 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003186 bool First = true;
3187 for (unsigned i = 0; i < 8; ++i) {
3188 bool isNonZero = (NonZeros & (1 << i)) != 0;
3189 if (isNonZero) {
3190 if (First) {
3191 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003192 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003193 else
3194 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3195 First = false;
3196 }
3197 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003198 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003199 }
3200 }
3201
3202 return V;
3203}
3204
Evan Chengdea99362008-05-29 08:22:04 +00003205/// getVShift - Return a vector logical shift node.
3206///
Dan Gohman8181bd12008-07-27 21:46:04 +00003207static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003208 unsigned NumBits, SelectionDAG &DAG,
3209 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003210 bool isMMX = VT.getSizeInBits() == 64;
3211 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003212 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3213 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3214 return DAG.getNode(ISD::BIT_CONVERT, VT,
3215 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003216 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003217}
3218
Dan Gohman8181bd12008-07-27 21:46:04 +00003219SDValue
3220X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003221 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003222 if (ISD::isBuildVectorAllZeros(Op.getNode())
3223 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003224 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3225 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3226 // eliminated on x86-32 hosts.
3227 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3228 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003229
Gabor Greif1c80d112008-08-28 21:40:38 +00003230 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003231 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003232 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003233 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003234
Duncan Sands92c43912008-06-06 12:08:01 +00003235 MVT VT = Op.getValueType();
3236 MVT EVT = VT.getVectorElementType();
3237 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003238
3239 unsigned NumElems = Op.getNumOperands();
3240 unsigned NumZero = 0;
3241 unsigned NumNonZero = 0;
3242 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003243 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003244 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003245 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003246 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003247 if (Elt.getOpcode() == ISD::UNDEF)
3248 continue;
3249 Values.insert(Elt);
3250 if (Elt.getOpcode() != ISD::Constant &&
3251 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003252 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003253 if (isZeroNode(Elt))
3254 NumZero++;
3255 else {
3256 NonZeros |= (1 << i);
3257 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003258 }
3259 }
3260
3261 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003262 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3263 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003264 }
3265
Chris Lattner66a4dda2008-03-09 05:42:06 +00003266 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003267 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003268 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003269 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003270
Chris Lattner2d91b962008-03-09 01:05:04 +00003271 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3272 // the value are obviously zero, truncate the value to i32 and do the
3273 // insertion that way. Only do this if the value is non-constant or if the
3274 // value is a constant being inserted into element 0. It is cheaper to do
3275 // a constant pool load than it is to do a movd + shuffle.
3276 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3277 (!IsAllConstants || Idx == 0)) {
3278 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3279 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003280 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3281 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003282
3283 // Truncate the value (which may itself be a constant) to i32, and
3284 // convert it to a vector with movd (S2V+shuffle to zero extend).
3285 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3286 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003287 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3288 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003289
3290 // Now we have our 32-bit value zero extended in the low element of
3291 // a vector. If Idx != 0, swizzle it into place.
3292 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003293 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003294 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3295 getSwapEltZeroMask(VecElts, Idx, DAG)
3296 };
3297 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3298 }
3299 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3300 }
3301 }
3302
Chris Lattnerac914892008-03-08 22:59:52 +00003303 // If we have a constant or non-constant insertion into the low element of
3304 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3305 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3306 // depending on what the source datatype is. Because we can only get here
3307 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3308 if (Idx == 0 &&
3309 // Don't do this for i64 values on x86-32.
3310 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003311 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003312 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003313 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3314 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003315 }
Evan Chengdea99362008-05-29 08:22:04 +00003316
3317 // Is it a vector logical left shift?
3318 if (NumElems == 2 && Idx == 1 &&
3319 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003320 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003321 return getVShift(true, VT,
3322 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3323 NumBits/2, DAG, *this);
3324 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003325
3326 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003327 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003328
Chris Lattnerac914892008-03-08 22:59:52 +00003329 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3330 // is a non-constant being inserted into an element other than the low one,
3331 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3332 // movd/movss) to move this into the low element, then shuffle it into
3333 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003334 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003335 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3336
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003337 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003338 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3339 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003340 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3341 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003342 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003343 for (unsigned i = 0; i < NumElems; i++)
3344 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003345 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003346 &MaskVec[0], MaskVec.size());
3347 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3348 DAG.getNode(ISD::UNDEF, VT), Mask);
3349 }
3350 }
3351
Chris Lattner66a4dda2008-03-09 05:42:06 +00003352 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3353 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003354 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003355
Dan Gohman21463242007-07-24 22:55:08 +00003356 // A vector full of immediates; various special cases are already
3357 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003358 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003359 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003360
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003361 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003362 if (EVTBits == 64) {
3363 if (NumNonZero == 1) {
3364 // One half is zero or undef.
3365 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003366 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003367 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003368 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3369 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003370 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003371 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003372 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003373
3374 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3375 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003376 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003377 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003378 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003379 }
3380
3381 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003382 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003383 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003384 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003385 }
3386
3387 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003388 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003389 V.resize(NumElems);
3390 if (NumElems == 4 && NumZero > 0) {
3391 for (unsigned i = 0; i < 4; ++i) {
3392 bool isZero = !(NonZeros & (1 << i));
3393 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003394 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003395 else
3396 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3397 }
3398
3399 for (unsigned i = 0; i < 2; ++i) {
3400 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3401 default: break;
3402 case 0:
3403 V[i] = V[i*2]; // Must be a zero vector.
3404 break;
3405 case 1:
3406 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3407 getMOVLMask(NumElems, DAG));
3408 break;
3409 case 2:
3410 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3411 getMOVLMask(NumElems, DAG));
3412 break;
3413 case 3:
3414 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3415 getUnpacklMask(NumElems, DAG));
3416 break;
3417 }
3418 }
3419
Duncan Sands92c43912008-06-06 12:08:01 +00003420 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3421 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003422 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003423 bool Reverse = (NonZeros & 0x3) == 2;
3424 for (unsigned i = 0; i < 2; ++i)
3425 if (Reverse)
3426 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3427 else
3428 MaskVec.push_back(DAG.getConstant(i, EVT));
3429 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3430 for (unsigned i = 0; i < 2; ++i)
3431 if (Reverse)
3432 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3433 else
3434 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003435 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003436 &MaskVec[0], MaskVec.size());
3437 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3438 }
3439
3440 if (Values.size() > 2) {
3441 // Expand into a number of unpckl*.
3442 // e.g. for v4f32
3443 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3444 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3445 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003446 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003447 for (unsigned i = 0; i < NumElems; ++i)
3448 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3449 NumElems >>= 1;
3450 while (NumElems != 0) {
3451 for (unsigned i = 0; i < NumElems; ++i)
3452 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3453 UnpckMask);
3454 NumElems >>= 1;
3455 }
3456 return V[0];
3457 }
3458
Dan Gohman8181bd12008-07-27 21:46:04 +00003459 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003460}
3461
Evan Chengfca29242007-12-07 08:07:39 +00003462static
Dan Gohman8181bd12008-07-27 21:46:04 +00003463SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003464 SDValue PermMask, SelectionDAG &DAG,
3465 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003466 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003467 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3468 MVT MaskEVT = MaskVT.getVectorElementType();
3469 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003470 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3471 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003472
3473 // First record which half of which vector the low elements come from.
3474 SmallVector<unsigned, 4> LowQuad(4);
3475 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003476 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003477 if (Elt.getOpcode() == ISD::UNDEF)
3478 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003479 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003480 int QuadIdx = EltIdx / 4;
3481 ++LowQuad[QuadIdx];
3482 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003483
Evan Cheng75184a92007-12-11 01:46:18 +00003484 int BestLowQuad = -1;
3485 unsigned MaxQuad = 1;
3486 for (unsigned i = 0; i < 4; ++i) {
3487 if (LowQuad[i] > MaxQuad) {
3488 BestLowQuad = i;
3489 MaxQuad = LowQuad[i];
3490 }
Evan Chengfca29242007-12-07 08:07:39 +00003491 }
3492
Evan Cheng75184a92007-12-11 01:46:18 +00003493 // Record which half of which vector the high elements come from.
3494 SmallVector<unsigned, 4> HighQuad(4);
3495 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003496 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003497 if (Elt.getOpcode() == ISD::UNDEF)
3498 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003499 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003500 int QuadIdx = EltIdx / 4;
3501 ++HighQuad[QuadIdx];
3502 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003503
Evan Cheng75184a92007-12-11 01:46:18 +00003504 int BestHighQuad = -1;
3505 MaxQuad = 1;
3506 for (unsigned i = 0; i < 4; ++i) {
3507 if (HighQuad[i] > MaxQuad) {
3508 BestHighQuad = i;
3509 MaxQuad = HighQuad[i];
3510 }
3511 }
3512
3513 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3514 if (BestLowQuad != -1 || BestHighQuad != -1) {
3515 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003516 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003517
Evan Cheng75184a92007-12-11 01:46:18 +00003518 if (BestLowQuad != -1)
3519 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3520 else
3521 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003522
Evan Cheng75184a92007-12-11 01:46:18 +00003523 if (BestHighQuad != -1)
3524 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3525 else
3526 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003527
Dan Gohman8181bd12008-07-27 21:46:04 +00003528 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003529 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3530 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3531 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3532 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3533
3534 // Now sort high and low parts separately.
3535 BitVector InOrder(8);
3536 if (BestLowQuad != -1) {
3537 // Sort lower half in order using PSHUFLW.
3538 MaskVec.clear();
3539 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003540
Evan Cheng75184a92007-12-11 01:46:18 +00003541 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003542 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003543 if (Elt.getOpcode() == ISD::UNDEF) {
3544 MaskVec.push_back(Elt);
3545 InOrder.set(i);
3546 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003547 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003548 if (EltIdx != i)
3549 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003550
Evan Cheng75184a92007-12-11 01:46:18 +00003551 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003552
Evan Cheng75184a92007-12-11 01:46:18 +00003553 // If this element is in the right place after this shuffle, then
3554 // remember it.
3555 if ((int)(EltIdx / 4) == BestLowQuad)
3556 InOrder.set(i);
3557 }
3558 }
3559 if (AnyOutOrder) {
3560 for (unsigned i = 4; i != 8; ++i)
3561 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003562 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003563 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3564 }
3565 }
3566
3567 if (BestHighQuad != -1) {
3568 // Sort high half in order using PSHUFHW if possible.
3569 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003570
Evan Cheng75184a92007-12-11 01:46:18 +00003571 for (unsigned i = 0; i != 4; ++i)
3572 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003573
Evan Cheng75184a92007-12-11 01:46:18 +00003574 bool AnyOutOrder = false;
3575 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003576 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003577 if (Elt.getOpcode() == ISD::UNDEF) {
3578 MaskVec.push_back(Elt);
3579 InOrder.set(i);
3580 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003581 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003582 if (EltIdx != i)
3583 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003584
Evan Cheng75184a92007-12-11 01:46:18 +00003585 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003586
Evan Cheng75184a92007-12-11 01:46:18 +00003587 // If this element is in the right place after this shuffle, then
3588 // remember it.
3589 if ((int)(EltIdx / 4) == BestHighQuad)
3590 InOrder.set(i);
3591 }
3592 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003593
Evan Cheng75184a92007-12-11 01:46:18 +00003594 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003595 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003596 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3597 }
3598 }
3599
3600 // The other elements are put in the right place using pextrw and pinsrw.
3601 for (unsigned i = 0; i != 8; ++i) {
3602 if (InOrder[i])
3603 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003604 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003605 if (Elt.getOpcode() == ISD::UNDEF)
3606 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003607 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003608 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003609 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3610 DAG.getConstant(EltIdx, PtrVT))
3611 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3612 DAG.getConstant(EltIdx - 8, PtrVT));
3613 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3614 DAG.getConstant(i, PtrVT));
3615 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003616
Evan Cheng75184a92007-12-11 01:46:18 +00003617 return NewV;
3618 }
3619
Bill Wendling2c7cd592008-08-21 22:35:37 +00003620 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3621 // few as possible. First, let's find out how many elements are already in the
3622 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003623 unsigned V1InOrder = 0;
3624 unsigned V1FromV1 = 0;
3625 unsigned V2InOrder = 0;
3626 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003627 SmallVector<SDValue, 8> V1Elts;
3628 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003629 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003630 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003631 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003632 V1Elts.push_back(Elt);
3633 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003634 ++V1InOrder;
3635 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003636 continue;
3637 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003638 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003639 if (EltIdx == i) {
3640 V1Elts.push_back(Elt);
3641 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3642 ++V1InOrder;
3643 } else if (EltIdx == i+8) {
3644 V1Elts.push_back(Elt);
3645 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3646 ++V2InOrder;
3647 } else if (EltIdx < 8) {
3648 V1Elts.push_back(Elt);
3649 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003650 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003651 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3652 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003653 }
3654 }
3655
3656 if (V2InOrder > V1InOrder) {
3657 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3658 std::swap(V1, V2);
3659 std::swap(V1Elts, V2Elts);
3660 std::swap(V1FromV1, V2FromV2);
3661 }
3662
Evan Cheng75184a92007-12-11 01:46:18 +00003663 if ((V1FromV1 + V1InOrder) != 8) {
3664 // Some elements are from V2.
3665 if (V1FromV1) {
3666 // If there are elements that are from V1 but out of place,
3667 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003668 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003669 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003670 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003671 if (Elt.getOpcode() == ISD::UNDEF) {
3672 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3673 continue;
3674 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003675 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003676 if (EltIdx >= 8)
3677 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3678 else
3679 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3680 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003681 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003682 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003683 }
Evan Cheng75184a92007-12-11 01:46:18 +00003684
3685 NewV = V1;
3686 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003687 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003688 if (Elt.getOpcode() == ISD::UNDEF)
3689 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003690 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003691 if (EltIdx < 8)
3692 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003693 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003694 DAG.getConstant(EltIdx - 8, PtrVT));
3695 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3696 DAG.getConstant(i, PtrVT));
3697 }
3698 return NewV;
3699 } else {
3700 // All elements are from V1.
3701 NewV = V1;
3702 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003703 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003704 if (Elt.getOpcode() == ISD::UNDEF)
3705 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003706 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003707 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003708 DAG.getConstant(EltIdx, PtrVT));
3709 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3710 DAG.getConstant(i, PtrVT));
3711 }
3712 return NewV;
3713 }
3714}
3715
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003716/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3717/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3718/// done when every pair / quad of shuffle mask elements point to elements in
3719/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003720/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3721static
Dan Gohman8181bd12008-07-27 21:46:04 +00003722SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003723 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003724 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003725 TargetLowering &TLI) {
3726 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003727 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003728 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003729 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003730 MVT NewVT = MaskVT;
3731 switch (VT.getSimpleVT()) {
3732 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003733 case MVT::v4f32: NewVT = MVT::v2f64; break;
3734 case MVT::v4i32: NewVT = MVT::v2i64; break;
3735 case MVT::v8i16: NewVT = MVT::v4i32; break;
3736 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003737 }
3738
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003739 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003740 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003741 NewVT = MVT::v2i64;
3742 else
3743 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003744 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003745 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003746 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003747 for (unsigned i = 0; i < NumElems; i += Scale) {
3748 unsigned StartIdx = ~0U;
3749 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003750 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003751 if (Elt.getOpcode() == ISD::UNDEF)
3752 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003753 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003754 if (StartIdx == ~0U)
3755 StartIdx = EltIdx - (EltIdx % Scale);
3756 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003757 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003758 }
3759 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003760 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003761 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003762 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003763 }
3764
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003765 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3766 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3767 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3768 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3769 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003770}
3771
Evan Chenge9b9c672008-05-09 21:53:03 +00003772/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003773///
Dan Gohman8181bd12008-07-27 21:46:04 +00003774static SDValue getVZextMovL(MVT VT, MVT OpVT,
3775 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003776 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003777 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3778 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003779 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003780 LD = dyn_cast<LoadSDNode>(SrcOp);
3781 if (!LD) {
3782 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3783 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003784 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003785 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3786 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3787 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3788 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3789 // PR2108
3790 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3791 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003792 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003793 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003794 SrcOp.getOperand(0)
3795 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003796 }
3797 }
3798 }
3799
3800 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003801 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003802 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3803}
3804
Evan Chengf50554e2008-07-22 21:13:36 +00003805/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3806/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003807static SDValue
3808LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3809 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003810 MVT MaskVT = PermMask.getValueType();
3811 MVT MaskEVT = MaskVT.getVectorElementType();
3812 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003813 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003814 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003815 unsigned NumHi = 0;
3816 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003817 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003818 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003819 if (Elt.getOpcode() == ISD::UNDEF) {
3820 Locs[i] = std::make_pair(-1, -1);
3821 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003822 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003823 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003824 if (Val < 4) {
3825 Locs[i] = std::make_pair(0, NumLo);
3826 Mask1[NumLo] = Elt;
3827 NumLo++;
3828 } else {
3829 Locs[i] = std::make_pair(1, NumHi);
3830 if (2+NumHi < 4)
3831 Mask1[2+NumHi] = Elt;
3832 NumHi++;
3833 }
3834 }
3835 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003836
Evan Chengf50554e2008-07-22 21:13:36 +00003837 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003838 // If no more than two elements come from either vector. This can be
3839 // implemented with two shuffles. First shuffle gather the elements.
3840 // The second shuffle, which takes the first shuffle as both of its
3841 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003842 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3843 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3844 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003845
Dan Gohman8181bd12008-07-27 21:46:04 +00003846 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003847 for (unsigned i = 0; i != 4; ++i) {
3848 if (Locs[i].first == -1)
3849 continue;
3850 else {
3851 unsigned Idx = (i < 2) ? 0 : 4;
3852 Idx += Locs[i].first * 2 + Locs[i].second;
3853 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3854 }
3855 }
3856
3857 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3858 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3859 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003860 } else if (NumLo == 3 || NumHi == 3) {
3861 // Otherwise, we must have three elements from one vector, call it X, and
3862 // one element from the other, call it Y. First, use a shufps to build an
3863 // intermediate vector with the one element from Y and the element from X
3864 // that will be in the same half in the final destination (the indexes don't
3865 // matter). Then, use a shufps to build the final vector, taking the half
3866 // containing the element from Y from the intermediate, and the other half
3867 // from X.
3868 if (NumHi == 3) {
3869 // Normalize it so the 3 elements come from V1.
3870 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3871 std::swap(V1, V2);
3872 }
3873
3874 // Find the element from V2.
3875 unsigned HiIndex;
3876 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003877 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003878 if (Elt.getOpcode() == ISD::UNDEF)
3879 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003880 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003881 if (Val >= 4)
3882 break;
3883 }
3884
3885 Mask1[0] = PermMask.getOperand(HiIndex);
3886 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3887 Mask1[2] = PermMask.getOperand(HiIndex^1);
3888 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3889 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3890 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3891
3892 if (HiIndex >= 2) {
3893 Mask1[0] = PermMask.getOperand(0);
3894 Mask1[1] = PermMask.getOperand(1);
3895 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3896 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3897 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3898 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3899 } else {
3900 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3901 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3902 Mask1[2] = PermMask.getOperand(2);
3903 Mask1[3] = PermMask.getOperand(3);
3904 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003905 Mask1[2] =
3906 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3907 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003908 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003909 Mask1[3] =
3910 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3911 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003912 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3913 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3914 }
Evan Chengf50554e2008-07-22 21:13:36 +00003915 }
3916
3917 // Break it into (shuffle shuffle_hi, shuffle_lo).
3918 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003919 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3920 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3921 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003922 unsigned MaskIdx = 0;
3923 unsigned LoIdx = 0;
3924 unsigned HiIdx = 2;
3925 for (unsigned i = 0; i != 4; ++i) {
3926 if (i == 2) {
3927 MaskPtr = &HiMask;
3928 MaskIdx = 1;
3929 LoIdx = 0;
3930 HiIdx = 2;
3931 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003932 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003933 if (Elt.getOpcode() == ISD::UNDEF) {
3934 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003935 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003936 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3937 (*MaskPtr)[LoIdx] = Elt;
3938 LoIdx++;
3939 } else {
3940 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3941 (*MaskPtr)[HiIdx] = Elt;
3942 HiIdx++;
3943 }
3944 }
3945
Dan Gohman8181bd12008-07-27 21:46:04 +00003946 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003947 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3948 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003949 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003950 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3951 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003952 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003953 for (unsigned i = 0; i != 4; ++i) {
3954 if (Locs[i].first == -1) {
3955 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3956 } else {
3957 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3958 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3959 }
3960 }
3961 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3962 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3963 &MaskOps[0], MaskOps.size()));
3964}
3965
Dan Gohman8181bd12008-07-27 21:46:04 +00003966SDValue
3967X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3968 SDValue V1 = Op.getOperand(0);
3969 SDValue V2 = Op.getOperand(1);
3970 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003971 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003972 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003973 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003974 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3975 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3976 bool V1IsSplat = false;
3977 bool V2IsSplat = false;
3978
Gabor Greif1c80d112008-08-28 21:40:38 +00003979 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003980 return DAG.getNode(ISD::UNDEF, VT);
3981
Gabor Greif1c80d112008-08-28 21:40:38 +00003982 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003983 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003984
Gabor Greif1c80d112008-08-28 21:40:38 +00003985 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003986 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003987 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003988 return V2;
3989
Evan Chengae6c9212008-09-25 23:35:16 +00003990 // Canonicalize movddup shuffles.
3991 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Chengbdd9d9f2008-10-06 21:13:08 +00003992 VT.getSizeInBits() == 128 &&
Evan Chengae6c9212008-09-25 23:35:16 +00003993 X86::isMOVDDUPMask(PermMask.getNode()))
3994 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
3995
Gabor Greif1c80d112008-08-28 21:40:38 +00003996 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003997 if (isMMX || NumElems < 4) return Op;
3998 // Promote it to a v4{if}32 splat.
3999 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004000 }
4001
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004002 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4003 // do it!
4004 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004005 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004006 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004007 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
4008 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4009 // FIXME: Figure out a cleaner way to do this.
4010 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004011 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004012 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004013 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004014 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004015 SDValue NewV1 = NewOp.getOperand(0);
4016 SDValue NewV2 = NewOp.getOperand(1);
4017 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00004018 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004019 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00004020 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004021 }
4022 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004023 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004024 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004025 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004026 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004027 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00004028 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004029 }
4030 }
4031
Evan Chengdea99362008-05-29 08:22:04 +00004032 // Check if this can be converted into a logical shift.
4033 bool isLeft = false;
4034 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004035 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00004036 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4037 if (isShift && ShVal.hasOneUse()) {
4038 // If the shifted value has multiple uses, it may be cheaper to use
4039 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004040 MVT EVT = VT.getVectorElementType();
4041 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004042 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4043 }
4044
Gabor Greif1c80d112008-08-28 21:40:38 +00004045 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004046 if (V1IsUndef)
4047 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004048 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004049 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004050 if (!isMMX)
4051 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004052 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004053
Gabor Greif1c80d112008-08-28 21:40:38 +00004054 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4055 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4056 X86::isMOVHLPSMask(PermMask.getNode()) ||
4057 X86::isMOVHPMask(PermMask.getNode()) ||
4058 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004059 return Op;
4060
Gabor Greif1c80d112008-08-28 21:40:38 +00004061 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4062 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004063 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4064
Evan Chengdea99362008-05-29 08:22:04 +00004065 if (isShift) {
4066 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004067 MVT EVT = VT.getVectorElementType();
4068 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004069 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4070 }
4071
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004072 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004073 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4074 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004075 V1IsSplat = isSplatVector(V1.getNode());
4076 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004077
4078 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004079 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4080 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4081 std::swap(V1IsSplat, V2IsSplat);
4082 std::swap(V1IsUndef, V2IsUndef);
4083 Commuted = true;
4084 }
4085
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004086 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004087 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004088 if (V2IsUndef) return V1;
4089 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4090 if (V2IsSplat) {
4091 // V2 is a splat, so the mask may be malformed. That is, it may point
4092 // to any V2 element. The instruction selectior won't like this. Get
4093 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004094 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004095 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004096 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4097 }
4098 return Op;
4099 }
4100
Gabor Greif1c80d112008-08-28 21:40:38 +00004101 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4102 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4103 X86::isUNPCKLMask(PermMask.getNode()) ||
4104 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004105 return Op;
4106
4107 if (V2IsSplat) {
4108 // Normalize mask so all entries that point to V2 points to its first
4109 // element then try to match unpck{h|l} again. If match, return a
4110 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004111 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004112 if (NewMask.getNode() != PermMask.getNode()) {
4113 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004114 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004115 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004116 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004117 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004118 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4119 }
4120 }
4121 }
4122
4123 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004124 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004125 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4126
4127 if (Commuted) {
4128 // Commute is back and try unpck* again.
4129 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004130 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4131 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4132 X86::isUNPCKLMask(PermMask.getNode()) ||
4133 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004134 return Op;
4135 }
4136
Evan Chengbf8b2c52008-04-05 00:30:36 +00004137 // Try PSHUF* first, then SHUFP*.
4138 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4139 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004140 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004141 if (V2.getOpcode() != ISD::UNDEF)
4142 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4143 DAG.getNode(ISD::UNDEF, VT), PermMask);
4144 return Op;
4145 }
4146
4147 if (!isMMX) {
4148 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004149 (X86::isPSHUFDMask(PermMask.getNode()) ||
4150 X86::isPSHUFHWMask(PermMask.getNode()) ||
4151 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004152 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004153 if (VT == MVT::v4f32) {
4154 RVT = MVT::v4i32;
4155 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4156 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4157 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4158 } else if (V2.getOpcode() != ISD::UNDEF)
4159 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4160 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4161 if (RVT != VT)
4162 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004163 return Op;
4164 }
4165
Evan Chengbf8b2c52008-04-05 00:30:36 +00004166 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004167 if (X86::isSHUFPMask(PermMask.getNode()) ||
4168 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004169 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004170 }
4171
Evan Cheng75184a92007-12-11 01:46:18 +00004172 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4173 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004174 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004175 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004176 return NewOp;
4177 }
4178
Evan Chengf50554e2008-07-22 21:13:36 +00004179 // Handle all 4 wide cases with a number of shuffles except for MMX.
4180 if (NumElems == 4 && !isMMX)
4181 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004182
Dan Gohman8181bd12008-07-27 21:46:04 +00004183 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004184}
4185
Dan Gohman8181bd12008-07-27 21:46:04 +00004186SDValue
4187X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004188 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004189 MVT VT = Op.getValueType();
4190 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004191 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004192 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004193 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004194 DAG.getValueType(VT));
4195 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004196 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004197 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004198 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004199 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004200 DAG.getValueType(VT));
4201 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004202 } else if (VT == MVT::f32) {
4203 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4204 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004205 // result has a single use which is a store or a bitcast to i32. And in
4206 // the case of a store, it's not worth it if the index is a constant 0,
4207 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004208 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004209 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004210 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004211 if ((User->getOpcode() != ISD::STORE ||
4212 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4213 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004214 (User->getOpcode() != ISD::BIT_CONVERT ||
4215 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004216 return SDValue();
4217 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004218 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4219 Op.getOperand(1));
4220 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004221 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004222 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004223}
4224
4225
Dan Gohman8181bd12008-07-27 21:46:04 +00004226SDValue
4227X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004228 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004229 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004230
Evan Cheng6c249332008-03-24 21:52:23 +00004231 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004232 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004233 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004234 return Res;
4235 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004236
Duncan Sands92c43912008-06-06 12:08:01 +00004237 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004238 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004239 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004240 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004241 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004242 if (Idx == 0)
4243 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4244 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4245 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4246 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004247 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004248 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004249 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004250 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004251 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004252 DAG.getValueType(VT));
4253 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004254 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004255 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004256 if (Idx == 0)
4257 return Op;
4258 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004259 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004260 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004261 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004262 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004263 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004264 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004265 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004266 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004267 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004268 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004269 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004270 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004271 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004272 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4273 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4274 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004275 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004276 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004277 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4278 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4279 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004280 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004281 if (Idx == 0)
4282 return Op;
4283
4284 // UNPCKHPD the element to the lowest double word, then movsd.
4285 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4286 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004287 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004288 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004289 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004290 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004291 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004292 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004293 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004294 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004295 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4296 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4297 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004298 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004299 }
4300
Dan Gohman8181bd12008-07-27 21:46:04 +00004301 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004302}
4303
Dan Gohman8181bd12008-07-27 21:46:04 +00004304SDValue
4305X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004306 MVT VT = Op.getValueType();
4307 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004308
Dan Gohman8181bd12008-07-27 21:46:04 +00004309 SDValue N0 = Op.getOperand(0);
4310 SDValue N1 = Op.getOperand(1);
4311 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004312
Dan Gohman5a7af042008-08-14 22:53:18 +00004313 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4314 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004315 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004316 : X86ISD::PINSRW;
4317 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4318 // argument.
4319 if (N1.getValueType() != MVT::i32)
4320 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4321 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004322 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begemand77e59e2008-02-11 04:19:36 +00004323 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004324 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004325 // Bits [7:6] of the constant are the source select. This will always be
4326 // zero here. The DAG Combiner may combine an extract_elt index into these
4327 // bits. For example (insert (extract, 3), 2) could be matched by putting
4328 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4329 // Bits [5:4] of the constant are the destination select. This is the
4330 // value of the incoming immediate.
4331 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4332 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004333 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begemand77e59e2008-02-11 04:19:36 +00004334 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4335 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004336 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004337}
4338
Dan Gohman8181bd12008-07-27 21:46:04 +00004339SDValue
4340X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004341 MVT VT = Op.getValueType();
4342 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004343
4344 if (Subtarget->hasSSE41())
4345 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4346
Evan Chenge12a7eb2007-12-12 07:55:34 +00004347 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004348 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004349
Dan Gohman8181bd12008-07-27 21:46:04 +00004350 SDValue N0 = Op.getOperand(0);
4351 SDValue N1 = Op.getOperand(1);
4352 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004353
Duncan Sands92c43912008-06-06 12:08:01 +00004354 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004355 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4356 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004357 if (N1.getValueType() != MVT::i32)
4358 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4359 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004360 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004361 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004362 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004363 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004364}
4365
Dan Gohman8181bd12008-07-27 21:46:04 +00004366SDValue
4367X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004368 if (Op.getValueType() == MVT::v2f32)
4369 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4370 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4371 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4372 Op.getOperand(0))));
4373
Dan Gohman8181bd12008-07-27 21:46:04 +00004374 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004375 MVT VT = MVT::v2i32;
4376 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004377 default: break;
4378 case MVT::v16i8:
4379 case MVT::v8i16:
4380 VT = MVT::v4i32;
4381 break;
4382 }
4383 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4384 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004385}
4386
Bill Wendlingfef06052008-09-16 21:48:12 +00004387// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4388// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4389// one of the above mentioned nodes. It has to be wrapped because otherwise
4390// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4391// be used to form addressing mode. These wrapped nodes will be selected
4392// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004393SDValue
4394X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004395 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004396 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004397 getPointerTy(),
4398 CP->getAlignment());
4399 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4400 // With PIC, the address is actually $g + Offset.
4401 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4402 !Subtarget->isPICStyleRIPRel()) {
4403 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4404 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4405 Result);
4406 }
4407
4408 return Result;
4409}
4410
Dan Gohman8181bd12008-07-27 21:46:04 +00004411SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004412X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
Dan Gohman36322c72008-10-18 02:06:02 +00004413 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00004414 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00004415 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4416 bool ExtraLoadRequired =
4417 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4418
4419 // Create the TargetGlobalAddress node, folding in the constant
4420 // offset if it is legal.
4421 SDValue Result;
Dan Gohman3d5257c2008-10-21 03:38:42 +00004422 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +00004423 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4424 Offset = 0;
4425 } else
4426 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004427 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00004428
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004429 // With PIC, the address is actually $g + Offset.
Dan Gohman36322c72008-10-18 02:06:02 +00004430 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004431 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4432 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4433 Result);
4434 }
4435
4436 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4437 // load the value at address GV, not the value of GV itself. This means that
4438 // the GlobalAddress must be in the base or index register of the address, not
4439 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4440 // The same applies for external symbols during PIC codegen
Dan Gohman36322c72008-10-18 02:06:02 +00004441 if (ExtraLoadRequired)
Dan Gohman12a9c082008-02-06 22:27:42 +00004442 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004443 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004444
Dan Gohman36322c72008-10-18 02:06:02 +00004445 // If there was a non-zero offset that we didn't fold, create an explicit
4446 // addition for it.
4447 if (Offset != 0)
4448 Result = DAG.getNode(ISD::ADD, getPointerTy(), Result,
4449 DAG.getConstant(Offset, getPointerTy()));
4450
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004451 return Result;
4452}
4453
Evan Cheng7f250d62008-09-24 00:05:32 +00004454SDValue
4455X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4456 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00004457 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
4458 return LowerGlobalAddress(GV, Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00004459}
4460
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004461// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004462static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004463LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004464 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004465 SDValue InFlag;
4466 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004467 DAG.getNode(X86ISD::GlobalBaseReg,
4468 PtrVT), InFlag);
4469 InFlag = Chain.getValue(1);
4470
4471 // emit leal symbol@TLSGD(,%ebx,1), %eax
4472 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004473 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004474 GA->getValueType(0),
4475 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004476 SDValue Ops[] = { Chain, TGA, InFlag };
4477 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004478 InFlag = Result.getValue(2);
4479 Chain = Result.getValue(1);
4480
4481 // call ___tls_get_addr. This function receives its argument in
4482 // the register EAX.
4483 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4484 InFlag = Chain.getValue(1);
4485
4486 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004487 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004488 DAG.getTargetExternalSymbol("___tls_get_addr",
4489 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004490 DAG.getRegister(X86::EAX, PtrVT),
4491 DAG.getRegister(X86::EBX, PtrVT),
4492 InFlag };
4493 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4494 InFlag = Chain.getValue(1);
4495
4496 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4497}
4498
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004499// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004500static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004501LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004502 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004503 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004504
4505 // emit leaq symbol@TLSGD(%rip), %rdi
4506 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004507 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004508 GA->getValueType(0),
4509 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004510 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4511 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004512 Chain = Result.getValue(1);
4513 InFlag = Result.getValue(2);
4514
aslb204cd52008-08-16 12:58:29 +00004515 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004516 // the register RDI.
4517 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4518 InFlag = Chain.getValue(1);
4519
4520 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004521 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004522 DAG.getTargetExternalSymbol("__tls_get_addr",
4523 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004524 DAG.getRegister(X86::RDI, PtrVT),
4525 InFlag };
4526 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4527 InFlag = Chain.getValue(1);
4528
4529 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4530}
4531
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004532// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4533// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004534static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004535 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004536 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004537 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004538 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4539 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004540 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004541 GA->getValueType(0),
4542 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004543 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004544
4545 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004546 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004547 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004548
4549 // The address of the thread local variable is the add of the thread
4550 // pointer with the offset of the variable.
4551 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4552}
4553
Dan Gohman8181bd12008-07-27 21:46:04 +00004554SDValue
4555X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004556 // TODO: implement the "local dynamic" model
4557 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004558 assert(Subtarget->isTargetELF() &&
4559 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004560 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4561 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4562 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004563 if (Subtarget->is64Bit()) {
4564 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4565 } else {
4566 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4567 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4568 else
4569 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4570 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004571}
4572
Dan Gohman8181bd12008-07-27 21:46:04 +00004573SDValue
4574X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004575 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4576 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004577 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4578 // With PIC, the address is actually $g + Offset.
4579 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4580 !Subtarget->isPICStyleRIPRel()) {
4581 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4582 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4583 Result);
4584 }
4585
4586 return Result;
4587}
4588
Dan Gohman8181bd12008-07-27 21:46:04 +00004589SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004590 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004591 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004592 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4593 // With PIC, the address is actually $g + Offset.
4594 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4595 !Subtarget->isPICStyleRIPRel()) {
4596 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4597 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4598 Result);
4599 }
4600
4601 return Result;
4602}
4603
Chris Lattner62814a32007-10-17 06:02:13 +00004604/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4605/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004606SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004607 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004608 MVT VT = Op.getValueType();
4609 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004610 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004611 SDValue ShOpLo = Op.getOperand(0);
4612 SDValue ShOpHi = Op.getOperand(1);
4613 SDValue ShAmt = Op.getOperand(2);
4614 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004615 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4616 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004617
Dan Gohman8181bd12008-07-27 21:46:04 +00004618 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004619 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004620 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4621 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004622 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004623 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4624 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004625 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004626
Dan Gohman8181bd12008-07-27 21:46:04 +00004627 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004628 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004629 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004630 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004631
Dan Gohman8181bd12008-07-27 21:46:04 +00004632 SDValue Hi, Lo;
4633 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4634 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4635 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004636
Chris Lattner62814a32007-10-17 06:02:13 +00004637 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004638 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4639 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004640 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004641 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4642 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004643 }
4644
Dan Gohman8181bd12008-07-27 21:46:04 +00004645 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004646 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004647}
4648
Dan Gohman8181bd12008-07-27 21:46:04 +00004649SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004650 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004651 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004652 "Unknown SINT_TO_FP to lower!");
4653
4654 // These are really Legal; caller falls through into that case.
4655 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004656 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004657 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4658 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004659 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004660
Duncan Sands92c43912008-06-06 12:08:01 +00004661 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004662 MachineFunction &MF = DAG.getMachineFunction();
4663 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004664 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4665 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004666 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004667 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004668
4669 // Build the FILD
4670 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004671 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004672 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004673 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4674 else
4675 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004676 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004677 Ops.push_back(Chain);
4678 Ops.push_back(StackSlot);
4679 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004680 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004681 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004682
Dale Johannesen2fc20782007-09-14 22:26:36 +00004683 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004684 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004685 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004686
4687 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4688 // shouldn't be necessary except that RFP cannot be live across
4689 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4690 MachineFunction &MF = DAG.getMachineFunction();
4691 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004692 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004693 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004694 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004695 Ops.push_back(Chain);
4696 Ops.push_back(Result);
4697 Ops.push_back(StackSlot);
4698 Ops.push_back(DAG.getValueType(Op.getValueType()));
4699 Ops.push_back(InFlag);
4700 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004701 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004702 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004703 }
4704
4705 return Result;
4706}
4707
Dale Johannesena359b8b2008-10-21 20:50:01 +00004708SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4709 MVT SrcVT = Op.getOperand(0).getValueType();
4710 assert(SrcVT.getSimpleVT() == MVT::i64 && "Unknown UINT_TO_FP to lower!");
4711
4712 // We only handle SSE2 f64 target here; caller can handle the rest.
4713 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4714 return SDValue();
4715
Dale Johannesenfb019af2008-10-21 23:07:49 +00004716 // This algorithm is not obvious. Here it is in C code, more or less:
4717/*
4718 double uint64_to_double( uint32_t hi, uint32_t lo )
4719 {
4720 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4721 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
4722
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004723 // copy ints to xmm registers
Dale Johannesenfb019af2008-10-21 23:07:49 +00004724 __m128i xh = _mm_cvtsi32_si128( hi );
4725 __m128i xl = _mm_cvtsi32_si128( lo );
4726
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004727 // combine into low half of a single xmm register
Dale Johannesenfb019af2008-10-21 23:07:49 +00004728 __m128i x = _mm_unpacklo_epi32( xh, xl );
4729 __m128d d;
4730 double sd;
4731
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004732 // merge in appropriate exponents to give the integer bits the
Dale Johannesenfb019af2008-10-21 23:07:49 +00004733 // right magnitude
4734 x = _mm_unpacklo_epi32( x, exp );
4735
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004736 // subtract away the biases to deal with the IEEE-754 double precision
4737 // implicit 1
Dale Johannesenfb019af2008-10-21 23:07:49 +00004738 d = _mm_sub_pd( (__m128d) x, bias );
4739
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004740 // All conversions up to here are exact. The correctly rounded result is
Dale Johannesenfb019af2008-10-21 23:07:49 +00004741 // calculated using the
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004742 // current rounding mode using the following horizontal add.
Dale Johannesenfb019af2008-10-21 23:07:49 +00004743 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4744 _mm_store_sd( &sd, d ); //since we are returning doubles in XMM, this
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004745 // store doesn't really need to be here (except maybe to zero the other
4746 // double)
Dale Johannesenfb019af2008-10-21 23:07:49 +00004747 return sd;
4748 }
4749*/
4750
Dale Johannesena359b8b2008-10-21 20:50:01 +00004751 // Build some magic constants.
4752 std::vector<Constant*>CV0;
4753 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4754 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4755 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4756 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4757 Constant *C0 = ConstantVector::get(CV0);
4758 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 4);
4759
4760 std::vector<Constant*>CV1;
4761 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4762 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4763 Constant *C1 = ConstantVector::get(CV1);
4764 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 4);
4765
4766 SmallVector<SDValue, 4> MaskVec;
4767 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
4768 MaskVec.push_back(DAG.getConstant(4, MVT::i32));
4769 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
4770 MaskVec.push_back(DAG.getConstant(5, MVT::i32));
4771 SDValue UnpcklMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, &MaskVec[0],
4772 MaskVec.size());
4773 SmallVector<SDValue, 4> MaskVec2;
Duncan Sandsca872ca2008-10-22 11:24:12 +00004774 MaskVec2.push_back(DAG.getConstant(1, MVT::i32));
4775 MaskVec2.push_back(DAG.getConstant(0, MVT::i32));
4776 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec2[0],
Dale Johannesena359b8b2008-10-21 20:50:01 +00004777 MaskVec2.size());
4778
4779 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004780 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4781 Op.getOperand(0),
4782 DAG.getIntPtrConstant(1)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004783 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004784 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4785 Op.getOperand(0),
4786 DAG.getIntPtrConstant(0)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004787 SDValue Unpck1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4788 XR1, XR2, UnpcklMask);
4789 SDValue CLod0 = DAG.getLoad(MVT::v4i32, DAG.getEntryNode(), CPIdx0,
4790 PseudoSourceValue::getConstantPool(), 0, false, 16);
4791 SDValue Unpck2 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4792 Unpck1, CLod0, UnpcklMask);
4793 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Unpck2);
4794 SDValue CLod1 = DAG.getLoad(MVT::v2f64, CLod0.getValue(1), CPIdx1,
4795 PseudoSourceValue::getConstantPool(), 0, false, 16);
4796 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::v2f64, XR2F, CLod1);
4797 // Add the halves; easiest way is to swap them into another reg first.
4798 SDValue Shuf = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2f64,
4799 Sub, Sub, ShufMask);
4800 SDValue Add = DAG.getNode(ISD::FADD, MVT::v2f64, Shuf, Sub);
4801 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64, Add,
4802 DAG.getIntPtrConstant(0));
4803}
4804
Dan Gohman8181bd12008-07-27 21:46:04 +00004805std::pair<SDValue,SDValue> X86TargetLowering::
4806FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004807 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4808 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004809 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004810
Dale Johannesen2fc20782007-09-14 22:26:36 +00004811 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004812 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004813 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004814 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004815 if (Subtarget->is64Bit() &&
4816 Op.getValueType() == MVT::i64 &&
4817 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004818 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004819
Evan Cheng05441e62007-10-15 20:11:21 +00004820 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4821 // stack slot.
4822 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004823 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004824 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004825 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004826 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004827 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004828 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4829 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4830 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4831 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004832 }
4833
Dan Gohman8181bd12008-07-27 21:46:04 +00004834 SDValue Chain = DAG.getEntryNode();
4835 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004836 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004837 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004838 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004839 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004840 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004841 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004842 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4843 };
4844 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4845 Chain = Value.getValue(1);
4846 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4847 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4848 }
4849
4850 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004851 SDValue Ops[] = { Chain, Value, StackSlot };
4852 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004853
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004854 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004855}
4856
Dan Gohman8181bd12008-07-27 21:46:04 +00004857SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4858 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4859 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004860 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004861
4862 // Load the result.
4863 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4864}
4865
Dan Gohman8181bd12008-07-27 21:46:04 +00004866SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004867 MVT VT = Op.getValueType();
4868 MVT EltVT = VT;
4869 if (VT.isVector())
4870 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004871 std::vector<Constant*> CV;
4872 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004873 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004874 CV.push_back(C);
4875 CV.push_back(C);
4876 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004877 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004878 CV.push_back(C);
4879 CV.push_back(C);
4880 CV.push_back(C);
4881 CV.push_back(C);
4882 }
Dan Gohman11821702007-07-27 17:16:43 +00004883 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004884 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4885 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004886 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004887 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004888 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4889}
4890
Dan Gohman8181bd12008-07-27 21:46:04 +00004891SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004892 MVT VT = Op.getValueType();
4893 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004894 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004895 if (VT.isVector()) {
4896 EltVT = VT.getVectorElementType();
4897 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004898 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004899 std::vector<Constant*> CV;
4900 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004901 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004902 CV.push_back(C);
4903 CV.push_back(C);
4904 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004905 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004906 CV.push_back(C);
4907 CV.push_back(C);
4908 CV.push_back(C);
4909 CV.push_back(C);
4910 }
Dan Gohman11821702007-07-27 17:16:43 +00004911 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004912 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4913 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004914 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004915 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004916 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004917 return DAG.getNode(ISD::BIT_CONVERT, VT,
4918 DAG.getNode(ISD::XOR, MVT::v2i64,
4919 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4920 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4921 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004922 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4923 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004924}
4925
Dan Gohman8181bd12008-07-27 21:46:04 +00004926SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4927 SDValue Op0 = Op.getOperand(0);
4928 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004929 MVT VT = Op.getValueType();
4930 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004931
4932 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004933 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004934 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4935 SrcVT = VT;
4936 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004937 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004938 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004939 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004940 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004941 }
4942
4943 // At this point the operands and the result should have the same
4944 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004945
4946 // First get the sign bit of second operand.
4947 std::vector<Constant*> CV;
4948 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004949 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4950 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004951 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004952 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4953 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4954 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4955 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004956 }
Dan Gohman11821702007-07-27 17:16:43 +00004957 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004958 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4959 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004960 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004961 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004962 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004963
4964 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004965 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004966 // Op0 is MVT::f32, Op1 is MVT::f64.
4967 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4968 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4969 DAG.getConstant(32, MVT::i32));
4970 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4971 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004972 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004973 }
4974
4975 // Clear first operand sign bit.
4976 CV.clear();
4977 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004978 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4979 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004980 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004981 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4982 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4983 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4984 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004985 }
Dan Gohman11821702007-07-27 17:16:43 +00004986 C = ConstantVector::get(CV);
4987 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004988 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004989 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004990 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004991 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004992
4993 // Or the value with the sign bit.
4994 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4995}
4996
Dan Gohman8181bd12008-07-27 21:46:04 +00004997SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004998 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004999 SDValue Cond;
5000 SDValue Op0 = Op.getOperand(0);
5001 SDValue Op1 = Op.getOperand(1);
5002 SDValue CC = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00005003 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00005004 unsigned X86CC;
5005
Evan Cheng950aac02007-09-25 01:57:46 +00005006 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00005007 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00005008 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
5009 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00005010 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00005011 }
Evan Cheng950aac02007-09-25 01:57:46 +00005012
Evan Cheng71343822008-10-15 02:05:31 +00005013 assert(0 && "Illegal SetCC!");
5014 return SDValue();
Evan Cheng950aac02007-09-25 01:57:46 +00005015}
5016
Dan Gohman8181bd12008-07-27 21:46:04 +00005017SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5018 SDValue Cond;
5019 SDValue Op0 = Op.getOperand(0);
5020 SDValue Op1 = Op.getOperand(1);
5021 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00005022 MVT VT = Op.getValueType();
5023 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5024 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5025
5026 if (isFP) {
5027 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00005028 MVT VT0 = Op0.getValueType();
5029 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5030 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00005031 bool Swap = false;
5032
5033 switch (SetCCOpcode) {
5034 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005035 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00005036 case ISD::SETEQ: SSECC = 0; break;
5037 case ISD::SETOGT:
5038 case ISD::SETGT: Swap = true; // Fallthrough
5039 case ISD::SETLT:
5040 case ISD::SETOLT: SSECC = 1; break;
5041 case ISD::SETOGE:
5042 case ISD::SETGE: Swap = true; // Fallthrough
5043 case ISD::SETLE:
5044 case ISD::SETOLE: SSECC = 2; break;
5045 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005046 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00005047 case ISD::SETNE: SSECC = 4; break;
5048 case ISD::SETULE: Swap = true;
5049 case ISD::SETUGE: SSECC = 5; break;
5050 case ISD::SETULT: Swap = true;
5051 case ISD::SETUGT: SSECC = 6; break;
5052 case ISD::SETO: SSECC = 7; break;
5053 }
5054 if (Swap)
5055 std::swap(Op0, Op1);
5056
Nate Begeman6357f9d2008-07-25 19:05:58 +00005057 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00005058 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00005059 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005060 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005061 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5062 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5063 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
5064 }
5065 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005066 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005067 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5068 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5069 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
5070 }
5071 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00005072 }
5073 // Handle all other FP comparisons here.
5074 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
5075 }
5076
5077 // We are handling one of the integer comparisons here. Since SSE only has
5078 // GT and EQ comparisons for integer, swapping operands and multiple
5079 // operations may be required for some comparisons.
5080 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5081 bool Swap = false, Invert = false, FlipSigns = false;
5082
5083 switch (VT.getSimpleVT()) {
5084 default: break;
5085 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5086 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5087 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5088 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5089 }
5090
5091 switch (SetCCOpcode) {
5092 default: break;
5093 case ISD::SETNE: Invert = true;
5094 case ISD::SETEQ: Opc = EQOpc; break;
5095 case ISD::SETLT: Swap = true;
5096 case ISD::SETGT: Opc = GTOpc; break;
5097 case ISD::SETGE: Swap = true;
5098 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5099 case ISD::SETULT: Swap = true;
5100 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5101 case ISD::SETUGE: Swap = true;
5102 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5103 }
5104 if (Swap)
5105 std::swap(Op0, Op1);
5106
5107 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5108 // bits of the inputs before performing those operations.
5109 if (FlipSigns) {
5110 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005111 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
5112 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
5113 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005114 SignBits.size());
5115 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
5116 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
5117 }
5118
Dan Gohman8181bd12008-07-27 21:46:04 +00005119 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005120
5121 // If the logical-not of the result is required, perform that now.
5122 if (Invert) {
5123 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005124 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
5125 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
5126 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005127 NegOnes.size());
5128 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
5129 }
5130 return Result;
5131}
Evan Cheng950aac02007-09-25 01:57:46 +00005132
Dan Gohman8181bd12008-07-27 21:46:04 +00005133SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005134 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005135 SDValue Cond = Op.getOperand(0);
5136 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005137
5138 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005139 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005140
Evan Cheng50d37ab2007-10-08 22:16:29 +00005141 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5142 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005143 if (Cond.getOpcode() == X86ISD::SETCC) {
5144 CC = Cond.getOperand(0);
5145
Dan Gohman8181bd12008-07-27 21:46:04 +00005146 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005147 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005148 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005149
Evan Cheng50d37ab2007-10-08 22:16:29 +00005150 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005151 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005152 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005153 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005154
Evan Cheng621216e2007-09-29 00:00:36 +00005155 if ((Opc == X86ISD::CMP ||
5156 Opc == X86ISD::COMI ||
5157 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005158 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005159 addTest = false;
5160 }
5161 }
5162
5163 if (addTest) {
5164 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00005165 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005166 }
5167
Duncan Sands92c43912008-06-06 12:08:01 +00005168 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005169 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005170 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005171 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5172 // condition is true.
5173 Ops.push_back(Op.getOperand(2));
5174 Ops.push_back(Op.getOperand(1));
5175 Ops.push_back(CC);
5176 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00005177 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005178}
5179
Dan Gohman8181bd12008-07-27 21:46:04 +00005180SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005181 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005182 SDValue Chain = Op.getOperand(0);
5183 SDValue Cond = Op.getOperand(1);
5184 SDValue Dest = Op.getOperand(2);
5185 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005186
5187 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005188 Cond = LowerSETCC(Cond, DAG);
Bill Wendlingd3511522008-12-02 01:06:39 +00005189 else if (Cond.getOpcode() == ISD::SADDO || Cond.getOpcode() == ISD::UADDO)
5190 Cond = LowerXADDO(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005191
Evan Cheng50d37ab2007-10-08 22:16:29 +00005192 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5193 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005194 if (Cond.getOpcode() == X86ISD::SETCC) {
5195 CC = Cond.getOperand(0);
5196
Dan Gohman8181bd12008-07-27 21:46:04 +00005197 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005198 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00005199 if (Opc == X86ISD::CMP ||
5200 Opc == X86ISD::COMI ||
5201 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005202 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005203 addTest = false;
Bill Wendlingd3511522008-12-02 01:06:39 +00005204 } else {
Bill Wendling809e7bd2008-12-03 08:32:02 +00005205 ConstantSDNode *CSDN = cast<ConstantSDNode>(CC.getNode());
5206
5207 switch (CSDN->getZExtValue()) {
5208 default: break;
5209 case X86::COND_O:
5210 case X86::COND_C:
5211 Cond = Cond.getNode()->getOperand(1);
5212 addTest = false;
5213 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00005214 }
Evan Cheng950aac02007-09-25 01:57:46 +00005215 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005216 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5217 // two branches instead of an explicit OR instruction with a
5218 // separate test.
5219 } else if (Cond.getOpcode() == ISD::OR &&
5220 Cond.hasOneUse() &&
5221 Cond.getOperand(0).getOpcode() == X86ISD::SETCC &&
5222 Cond.getOperand(0).hasOneUse() &&
5223 Cond.getOperand(1).getOpcode() == X86ISD::SETCC &&
5224 Cond.getOperand(1).hasOneUse()) {
5225 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5226 unsigned Opc = Cmp.getOpcode();
5227 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5228 (Opc == X86ISD::CMP ||
5229 Opc == X86ISD::COMI ||
5230 Opc == X86ISD::UCOMI)) {
5231 CC = Cond.getOperand(0).getOperand(0);
5232 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5233 Chain, Dest, CC, Cmp);
5234 CC = Cond.getOperand(1).getOperand(0);
5235 Cond = Cmp;
5236 addTest = false;
5237 }
5238 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5239 // two branches instead of an explicit AND instruction with a
5240 // separate test. However, we only do this if this block doesn't
5241 // have a fall-through edge, because this requires an explicit
5242 // jmp when the condition is false.
5243 } else if (Cond.getOpcode() == ISD::AND &&
5244 Cond.hasOneUse() &&
5245 Cond.getOperand(0).getOpcode() == X86ISD::SETCC &&
5246 Cond.getOperand(0).hasOneUse() &&
5247 Cond.getOperand(1).getOpcode() == X86ISD::SETCC &&
5248 Cond.getOperand(1).hasOneUse()) {
5249 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5250 unsigned Opc = Cmp.getOpcode();
5251 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5252 (Opc == X86ISD::CMP ||
5253 Opc == X86ISD::COMI ||
5254 Opc == X86ISD::UCOMI) &&
5255 Op.getNode()->hasOneUse()) {
5256 X86::CondCode CCode =
5257 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5258 CCode = X86::GetOppositeBranchCondition(CCode);
5259 CC = DAG.getConstant(CCode, MVT::i8);
5260 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5261 // Look for an unconditional branch following this conditional branch.
5262 // We need this because we need to reverse the successors in order
5263 // to implement FCMP_OEQ.
5264 if (User.getOpcode() == ISD::BR) {
5265 SDValue FalseBB = User.getOperand(1);
5266 SDValue NewBR =
5267 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5268 assert(NewBR == User);
5269 Dest = FalseBB;
5270
5271 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5272 Chain, Dest, CC, Cmp);
5273 X86::CondCode CCode =
5274 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5275 CCode = X86::GetOppositeBranchCondition(CCode);
5276 CC = DAG.getConstant(CCode, MVT::i8);
5277 Cond = Cmp;
5278 addTest = false;
5279 }
5280 }
Evan Cheng950aac02007-09-25 01:57:46 +00005281 }
5282
5283 if (addTest) {
5284 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005285 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005286 }
Evan Cheng621216e2007-09-29 00:00:36 +00005287 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005288 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005289}
5290
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005291
5292// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5293// Calls to _alloca is needed to probe the stack when allocating more than 4k
5294// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5295// that the guard pages used by the OS virtual memory manager are allocated in
5296// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005297SDValue
5298X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005299 SelectionDAG &DAG) {
5300 assert(Subtarget->isTargetCygMing() &&
5301 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005302
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005303 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005304 SDValue Chain = Op.getOperand(0);
5305 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005306 // FIXME: Ensure alignment here
5307
Dan Gohman8181bd12008-07-27 21:46:04 +00005308 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005309
Duncan Sands92c43912008-06-06 12:08:01 +00005310 MVT IntPtr = getPointerTy();
5311 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005312
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005313 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005314
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005315 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5316 Flag = Chain.getValue(1);
5317
5318 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005319 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005320 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005321 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005322 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005323 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005324 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325 Flag = Chain.getValue(1);
5326
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005327 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005328 DAG.getIntPtrConstant(0, true),
5329 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005330 Flag);
5331
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005332 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005333
Dan Gohman8181bd12008-07-27 21:46:04 +00005334 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005335 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005336}
5337
Dan Gohman8181bd12008-07-27 21:46:04 +00005338SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005339X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005340 SDValue Chain,
5341 SDValue Dst, SDValue Src,
5342 SDValue Size, unsigned Align,
5343 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005344 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005345 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005346
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005347 // If not DWORD aligned or size is more than the threshold, call the library.
5348 // The libc version is likely to be faster for these cases. It can use the
5349 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005350 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005351 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005352 ConstantSize->getZExtValue() >
5353 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005354 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005355
5356 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005357 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005358
Bill Wendling4b2e3782008-10-01 00:59:58 +00005359 if (const char *bzeroEntry = V &&
5360 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5361 MVT IntPtr = getPointerTy();
5362 const Type *IntPtrTy = TD->getIntPtrType();
5363 TargetLowering::ArgListTy Args;
5364 TargetLowering::ArgListEntry Entry;
5365 Entry.Node = Dst;
5366 Entry.Ty = IntPtrTy;
5367 Args.push_back(Entry);
5368 Entry.Node = Size;
5369 Args.push_back(Entry);
5370 std::pair<SDValue,SDValue> CallResult =
5371 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5372 CallingConv::C, false,
5373 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG);
5374 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005375 }
5376
Dan Gohmane8b391e2008-04-12 04:36:06 +00005377 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005378 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005379 }
5380
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005381 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005382 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005383 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005384 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005385 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005386 unsigned BytesLeft = 0;
5387 bool TwoRepStos = false;
5388 if (ValC) {
5389 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005390 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005391
5392 // If the value is a constant, then we can potentially use larger sets.
5393 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005394 case 2: // WORD aligned
5395 AVT = MVT::i16;
5396 ValReg = X86::AX;
5397 Val = (Val << 8) | Val;
5398 break;
5399 case 0: // DWORD aligned
5400 AVT = MVT::i32;
5401 ValReg = X86::EAX;
5402 Val = (Val << 8) | Val;
5403 Val = (Val << 16) | Val;
5404 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5405 AVT = MVT::i64;
5406 ValReg = X86::RAX;
5407 Val = (Val << 32) | Val;
5408 }
5409 break;
5410 default: // Byte aligned
5411 AVT = MVT::i8;
5412 ValReg = X86::AL;
5413 Count = DAG.getIntPtrConstant(SizeVal);
5414 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005415 }
5416
Duncan Sandsec142ee2008-06-08 20:54:56 +00005417 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005418 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005419 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5420 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005421 }
5422
5423 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5424 InFlag);
5425 InFlag = Chain.getValue(1);
5426 } else {
5427 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005428 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005429 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005430 InFlag = Chain.getValue(1);
5431 }
5432
5433 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5434 Count, InFlag);
5435 InFlag = Chain.getValue(1);
5436 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005437 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005438 InFlag = Chain.getValue(1);
5439
5440 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005441 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005442 Ops.push_back(Chain);
5443 Ops.push_back(DAG.getValueType(AVT));
5444 Ops.push_back(InFlag);
5445 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5446
5447 if (TwoRepStos) {
5448 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005449 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005450 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005451 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005452 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5453 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5454 Left, InFlag);
5455 InFlag = Chain.getValue(1);
5456 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5457 Ops.clear();
5458 Ops.push_back(Chain);
5459 Ops.push_back(DAG.getValueType(MVT::i8));
5460 Ops.push_back(InFlag);
5461 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5462 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005463 // Handle the last 1 - 7 bytes.
5464 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005465 MVT AddrVT = Dst.getValueType();
5466 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005467
5468 Chain = DAG.getMemset(Chain,
5469 DAG.getNode(ISD::ADD, AddrVT, Dst,
5470 DAG.getConstant(Offset, AddrVT)),
5471 Src,
5472 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005473 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005474 }
5475
Dan Gohmane8b391e2008-04-12 04:36:06 +00005476 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005477 return Chain;
5478}
5479
Dan Gohman8181bd12008-07-27 21:46:04 +00005480SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005481X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005482 SDValue Chain, SDValue Dst, SDValue Src,
5483 SDValue Size, unsigned Align,
5484 bool AlwaysInline,
5485 const Value *DstSV, uint64_t DstSVOff,
5486 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005487 // This requires the copy size to be a constant, preferrably
5488 // within a subtarget-specific limit.
5489 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5490 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005491 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005492 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005493 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005494 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005495
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005496 /// If not DWORD aligned, call the library.
5497 if ((Align & 3) != 0)
5498 return SDValue();
5499
5500 // DWORD aligned
5501 MVT AVT = MVT::i32;
5502 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005503 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005504
Duncan Sands92c43912008-06-06 12:08:01 +00005505 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005506 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005507 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005508 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005509
Dan Gohman8181bd12008-07-27 21:46:04 +00005510 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005511 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5512 Count, InFlag);
5513 InFlag = Chain.getValue(1);
5514 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005515 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005516 InFlag = Chain.getValue(1);
5517 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005518 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005519 InFlag = Chain.getValue(1);
5520
5521 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005522 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005523 Ops.push_back(Chain);
5524 Ops.push_back(DAG.getValueType(AVT));
5525 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005526 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005527
Dan Gohman8181bd12008-07-27 21:46:04 +00005528 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005529 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005530 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005531 // Handle the last 1 - 7 bytes.
5532 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005533 MVT DstVT = Dst.getValueType();
5534 MVT SrcVT = Src.getValueType();
5535 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005536 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005537 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005538 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005539 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005540 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005541 DAG.getConstant(BytesLeft, SizeVT),
5542 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005543 DstSV, DstSVOff + Offset,
5544 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005545 }
5546
Dan Gohmane8b391e2008-04-12 04:36:06 +00005547 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005548}
5549
Dan Gohman8181bd12008-07-27 21:46:04 +00005550SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005551 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005552
5553 if (!Subtarget->is64Bit()) {
5554 // vastart just stores the address of the VarArgsFrameIndex slot into the
5555 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005556 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005557 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005558 }
5559
5560 // __va_list_tag:
5561 // gp_offset (0 - 6 * 8)
5562 // fp_offset (48 - 48 + 8 * 16)
5563 // overflow_arg_area (point to parameters coming in memory).
5564 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005565 SmallVector<SDValue, 8> MemOps;
5566 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005567 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005568 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005569 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005570 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005571 MemOps.push_back(Store);
5572
5573 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005574 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005575 Store = DAG.getStore(Op.getOperand(0),
5576 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005577 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005578 MemOps.push_back(Store);
5579
5580 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005581 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005582 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005583 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005584 MemOps.push_back(Store);
5585
5586 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005587 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005588 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005589 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005590 MemOps.push_back(Store);
5591 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5592}
5593
Dan Gohman8181bd12008-07-27 21:46:04 +00005594SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005595 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5596 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005597 SDValue Chain = Op.getOperand(0);
5598 SDValue SrcPtr = Op.getOperand(1);
5599 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005600
5601 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5602 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005603 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005604}
5605
Dan Gohman8181bd12008-07-27 21:46:04 +00005606SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005607 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005608 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005609 SDValue Chain = Op.getOperand(0);
5610 SDValue DstPtr = Op.getOperand(1);
5611 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005612 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5613 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005614
Dan Gohman840ff5c2008-04-18 20:55:41 +00005615 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5616 DAG.getIntPtrConstant(24), 8, false,
5617 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005618}
5619
Dan Gohman8181bd12008-07-27 21:46:04 +00005620SDValue
5621X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005622 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005623 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005624 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005625 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005626 case Intrinsic::x86_sse_comieq_ss:
5627 case Intrinsic::x86_sse_comilt_ss:
5628 case Intrinsic::x86_sse_comile_ss:
5629 case Intrinsic::x86_sse_comigt_ss:
5630 case Intrinsic::x86_sse_comige_ss:
5631 case Intrinsic::x86_sse_comineq_ss:
5632 case Intrinsic::x86_sse_ucomieq_ss:
5633 case Intrinsic::x86_sse_ucomilt_ss:
5634 case Intrinsic::x86_sse_ucomile_ss:
5635 case Intrinsic::x86_sse_ucomigt_ss:
5636 case Intrinsic::x86_sse_ucomige_ss:
5637 case Intrinsic::x86_sse_ucomineq_ss:
5638 case Intrinsic::x86_sse2_comieq_sd:
5639 case Intrinsic::x86_sse2_comilt_sd:
5640 case Intrinsic::x86_sse2_comile_sd:
5641 case Intrinsic::x86_sse2_comigt_sd:
5642 case Intrinsic::x86_sse2_comige_sd:
5643 case Intrinsic::x86_sse2_comineq_sd:
5644 case Intrinsic::x86_sse2_ucomieq_sd:
5645 case Intrinsic::x86_sse2_ucomilt_sd:
5646 case Intrinsic::x86_sse2_ucomile_sd:
5647 case Intrinsic::x86_sse2_ucomigt_sd:
5648 case Intrinsic::x86_sse2_ucomige_sd:
5649 case Intrinsic::x86_sse2_ucomineq_sd: {
5650 unsigned Opc = 0;
5651 ISD::CondCode CC = ISD::SETCC_INVALID;
5652 switch (IntNo) {
5653 default: break;
5654 case Intrinsic::x86_sse_comieq_ss:
5655 case Intrinsic::x86_sse2_comieq_sd:
5656 Opc = X86ISD::COMI;
5657 CC = ISD::SETEQ;
5658 break;
5659 case Intrinsic::x86_sse_comilt_ss:
5660 case Intrinsic::x86_sse2_comilt_sd:
5661 Opc = X86ISD::COMI;
5662 CC = ISD::SETLT;
5663 break;
5664 case Intrinsic::x86_sse_comile_ss:
5665 case Intrinsic::x86_sse2_comile_sd:
5666 Opc = X86ISD::COMI;
5667 CC = ISD::SETLE;
5668 break;
5669 case Intrinsic::x86_sse_comigt_ss:
5670 case Intrinsic::x86_sse2_comigt_sd:
5671 Opc = X86ISD::COMI;
5672 CC = ISD::SETGT;
5673 break;
5674 case Intrinsic::x86_sse_comige_ss:
5675 case Intrinsic::x86_sse2_comige_sd:
5676 Opc = X86ISD::COMI;
5677 CC = ISD::SETGE;
5678 break;
5679 case Intrinsic::x86_sse_comineq_ss:
5680 case Intrinsic::x86_sse2_comineq_sd:
5681 Opc = X86ISD::COMI;
5682 CC = ISD::SETNE;
5683 break;
5684 case Intrinsic::x86_sse_ucomieq_ss:
5685 case Intrinsic::x86_sse2_ucomieq_sd:
5686 Opc = X86ISD::UCOMI;
5687 CC = ISD::SETEQ;
5688 break;
5689 case Intrinsic::x86_sse_ucomilt_ss:
5690 case Intrinsic::x86_sse2_ucomilt_sd:
5691 Opc = X86ISD::UCOMI;
5692 CC = ISD::SETLT;
5693 break;
5694 case Intrinsic::x86_sse_ucomile_ss:
5695 case Intrinsic::x86_sse2_ucomile_sd:
5696 Opc = X86ISD::UCOMI;
5697 CC = ISD::SETLE;
5698 break;
5699 case Intrinsic::x86_sse_ucomigt_ss:
5700 case Intrinsic::x86_sse2_ucomigt_sd:
5701 Opc = X86ISD::UCOMI;
5702 CC = ISD::SETGT;
5703 break;
5704 case Intrinsic::x86_sse_ucomige_ss:
5705 case Intrinsic::x86_sse2_ucomige_sd:
5706 Opc = X86ISD::UCOMI;
5707 CC = ISD::SETGE;
5708 break;
5709 case Intrinsic::x86_sse_ucomineq_ss:
5710 case Intrinsic::x86_sse2_ucomineq_sd:
5711 Opc = X86ISD::UCOMI;
5712 CC = ISD::SETNE;
5713 break;
5714 }
5715
5716 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005717 SDValue LHS = Op.getOperand(1);
5718 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005719 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5720
Dan Gohman8181bd12008-07-27 21:46:04 +00005721 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5722 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005723 DAG.getConstant(X86CC, MVT::i8), Cond);
5724 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005725 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005726
5727 // Fix vector shift instructions where the last operand is a non-immediate
5728 // i32 value.
5729 case Intrinsic::x86_sse2_pslli_w:
5730 case Intrinsic::x86_sse2_pslli_d:
5731 case Intrinsic::x86_sse2_pslli_q:
5732 case Intrinsic::x86_sse2_psrli_w:
5733 case Intrinsic::x86_sse2_psrli_d:
5734 case Intrinsic::x86_sse2_psrli_q:
5735 case Intrinsic::x86_sse2_psrai_w:
5736 case Intrinsic::x86_sse2_psrai_d:
5737 case Intrinsic::x86_mmx_pslli_w:
5738 case Intrinsic::x86_mmx_pslli_d:
5739 case Intrinsic::x86_mmx_pslli_q:
5740 case Intrinsic::x86_mmx_psrli_w:
5741 case Intrinsic::x86_mmx_psrli_d:
5742 case Intrinsic::x86_mmx_psrli_q:
5743 case Intrinsic::x86_mmx_psrai_w:
5744 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005745 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005746 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005747 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005748
5749 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005750 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005751 switch (IntNo) {
5752 case Intrinsic::x86_sse2_pslli_w:
5753 NewIntNo = Intrinsic::x86_sse2_psll_w;
5754 break;
5755 case Intrinsic::x86_sse2_pslli_d:
5756 NewIntNo = Intrinsic::x86_sse2_psll_d;
5757 break;
5758 case Intrinsic::x86_sse2_pslli_q:
5759 NewIntNo = Intrinsic::x86_sse2_psll_q;
5760 break;
5761 case Intrinsic::x86_sse2_psrli_w:
5762 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5763 break;
5764 case Intrinsic::x86_sse2_psrli_d:
5765 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5766 break;
5767 case Intrinsic::x86_sse2_psrli_q:
5768 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5769 break;
5770 case Intrinsic::x86_sse2_psrai_w:
5771 NewIntNo = Intrinsic::x86_sse2_psra_w;
5772 break;
5773 case Intrinsic::x86_sse2_psrai_d:
5774 NewIntNo = Intrinsic::x86_sse2_psra_d;
5775 break;
5776 default: {
5777 ShAmtVT = MVT::v2i32;
5778 switch (IntNo) {
5779 case Intrinsic::x86_mmx_pslli_w:
5780 NewIntNo = Intrinsic::x86_mmx_psll_w;
5781 break;
5782 case Intrinsic::x86_mmx_pslli_d:
5783 NewIntNo = Intrinsic::x86_mmx_psll_d;
5784 break;
5785 case Intrinsic::x86_mmx_pslli_q:
5786 NewIntNo = Intrinsic::x86_mmx_psll_q;
5787 break;
5788 case Intrinsic::x86_mmx_psrli_w:
5789 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5790 break;
5791 case Intrinsic::x86_mmx_psrli_d:
5792 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5793 break;
5794 case Intrinsic::x86_mmx_psrli_q:
5795 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5796 break;
5797 case Intrinsic::x86_mmx_psrai_w:
5798 NewIntNo = Intrinsic::x86_mmx_psra_w;
5799 break;
5800 case Intrinsic::x86_mmx_psrai_d:
5801 NewIntNo = Intrinsic::x86_mmx_psra_d;
5802 break;
5803 default: abort(); // Can't reach here.
5804 }
5805 break;
5806 }
5807 }
Duncan Sands92c43912008-06-06 12:08:01 +00005808 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005809 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5810 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5811 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5812 DAG.getConstant(NewIntNo, MVT::i32),
5813 Op.getOperand(1), ShAmt);
5814 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005815 }
5816}
5817
Dan Gohman8181bd12008-07-27 21:46:04 +00005818SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005819 // Depths > 0 not supported yet!
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005820 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005821 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005822
5823 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005824 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005825 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5826}
5827
Dan Gohman8181bd12008-07-27 21:46:04 +00005828SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00005829 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
5830 MFI->setFrameAddressIsTaken(true);
5831 MVT VT = Op.getValueType();
5832 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5833 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
5834 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), FrameReg, VT);
5835 while (Depth--)
5836 FrameAddr = DAG.getLoad(VT, DAG.getEntryNode(), FrameAddr, NULL, 0);
5837 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005838}
5839
Dan Gohman8181bd12008-07-27 21:46:04 +00005840SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005841 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005842 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005843}
5844
Dan Gohman8181bd12008-07-27 21:46:04 +00005845SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005846{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005847 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005848 SDValue Chain = Op.getOperand(0);
5849 SDValue Offset = Op.getOperand(1);
5850 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005851
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005852 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5853 getPointerTy());
5854 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005855
Dan Gohman8181bd12008-07-27 21:46:04 +00005856 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005857 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005858 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5859 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005860 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5861 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005862
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005863 return DAG.getNode(X86ISD::EH_RETURN,
5864 MVT::Other,
5865 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005866}
5867
Dan Gohman8181bd12008-07-27 21:46:04 +00005868SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005869 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005870 SDValue Root = Op.getOperand(0);
5871 SDValue Trmp = Op.getOperand(1); // trampoline
5872 SDValue FPtr = Op.getOperand(2); // nested function
5873 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005874
Dan Gohman12a9c082008-02-06 22:27:42 +00005875 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005876
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005877 const X86InstrInfo *TII =
5878 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5879
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005880 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005881 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005882
5883 // Large code-model.
5884
5885 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5886 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5887
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005888 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5889 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005890
5891 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5892
5893 // Load the pointer to the nested function into R11.
5894 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005895 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005896 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005897 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005898
5899 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005900 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005901
5902 // Load the 'nest' parameter value into R10.
5903 // R10 is specified in X86CallingConv.td
5904 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5905 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5906 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005907 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005908
5909 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005910 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005911
5912 // Jump to the nested function.
5913 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5914 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5915 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005916 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005917
5918 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5919 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5920 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005921 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005922
Dan Gohman8181bd12008-07-27 21:46:04 +00005923 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005924 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005925 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005926 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005927 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005928 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5929 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005930 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005931
5932 switch (CC) {
5933 default:
5934 assert(0 && "Unsupported calling convention");
5935 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005936 case CallingConv::X86_StdCall: {
5937 // Pass 'nest' parameter in ECX.
5938 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005939 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005940
5941 // Check that ECX wasn't needed by an 'inreg' parameter.
5942 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00005943 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005944
Chris Lattner1c8733e2008-03-12 17:45:29 +00005945 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005946 unsigned InRegCount = 0;
5947 unsigned Idx = 1;
5948
5949 for (FunctionType::param_iterator I = FTy->param_begin(),
5950 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00005951 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005952 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005953 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005954
5955 if (InRegCount > 2) {
5956 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5957 abort();
5958 }
5959 }
5960 break;
5961 }
5962 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00005963 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005964 // Pass 'nest' parameter in EAX.
5965 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005966 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005967 break;
5968 }
5969
Dan Gohman8181bd12008-07-27 21:46:04 +00005970 SDValue OutChains[4];
5971 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005972
5973 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5974 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5975
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005976 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005977 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005978 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005979 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005980
5981 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005982 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005983
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005984 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005985 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5986 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005987 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005988
5989 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005990 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005991
Dan Gohman8181bd12008-07-27 21:46:04 +00005992 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005993 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005994 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005995 }
5996}
5997
Dan Gohman8181bd12008-07-27 21:46:04 +00005998SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005999 /*
6000 The rounding mode is in bits 11:10 of FPSR, and has the following
6001 settings:
6002 00 Round to nearest
6003 01 Round to -inf
6004 10 Round to +inf
6005 11 Round to 0
6006
6007 FLT_ROUNDS, on the other hand, expects the following:
6008 -1 Undefined
6009 0 Round to 0
6010 1 Round to nearest
6011 2 Round to +inf
6012 3 Round to -inf
6013
6014 To perform the conversion, we do:
6015 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6016 */
6017
6018 MachineFunction &MF = DAG.getMachineFunction();
6019 const TargetMachine &TM = MF.getTarget();
6020 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6021 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00006022 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006023
6024 // Save FP Control Word to stack slot
6025 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00006026 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006027
Dan Gohman8181bd12008-07-27 21:46:04 +00006028 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00006029 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006030
6031 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00006032 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006033
6034 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00006035 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006036 DAG.getNode(ISD::SRL, MVT::i16,
6037 DAG.getNode(ISD::AND, MVT::i16,
6038 CWD, DAG.getConstant(0x800, MVT::i16)),
6039 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006040 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006041 DAG.getNode(ISD::SRL, MVT::i16,
6042 DAG.getNode(ISD::AND, MVT::i16,
6043 CWD, DAG.getConstant(0x400, MVT::i16)),
6044 DAG.getConstant(9, MVT::i8));
6045
Dan Gohman8181bd12008-07-27 21:46:04 +00006046 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006047 DAG.getNode(ISD::AND, MVT::i16,
6048 DAG.getNode(ISD::ADD, MVT::i16,
6049 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
6050 DAG.getConstant(1, MVT::i16)),
6051 DAG.getConstant(3, MVT::i16));
6052
6053
Duncan Sands92c43912008-06-06 12:08:01 +00006054 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006055 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6056}
6057
Dan Gohman8181bd12008-07-27 21:46:04 +00006058SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006059 MVT VT = Op.getValueType();
6060 MVT OpVT = VT;
6061 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006062
6063 Op = Op.getOperand(0);
6064 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006065 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00006066 OpVT = MVT::i32;
6067 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6068 }
Evan Cheng48679f42007-12-14 02:13:44 +00006069
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006070 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6071 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6072 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
6073
6074 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006075 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006076 Ops.push_back(Op);
6077 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6078 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6079 Ops.push_back(Op.getValue(1));
6080 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6081
6082 // Finally xor with NumBits-1.
6083 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
6084
Evan Cheng48679f42007-12-14 02:13:44 +00006085 if (VT == MVT::i8)
6086 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6087 return Op;
6088}
6089
Dan Gohman8181bd12008-07-27 21:46:04 +00006090SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006091 MVT VT = Op.getValueType();
6092 MVT OpVT = VT;
6093 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006094
6095 Op = Op.getOperand(0);
6096 if (VT == MVT::i8) {
6097 OpVT = MVT::i32;
6098 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6099 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006100
6101 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6102 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6103 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
6104
6105 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006106 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006107 Ops.push_back(Op);
6108 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6109 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6110 Ops.push_back(Op.getValue(1));
6111 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6112
Evan Cheng48679f42007-12-14 02:13:44 +00006113 if (VT == MVT::i8)
6114 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6115 return Op;
6116}
6117
Bill Wendlingd3511522008-12-02 01:06:39 +00006118SDValue X86TargetLowering::LowerXADDO(SDValue Op, SelectionDAG &DAG) {
6119 // Lower the "add with overflow" instruction into a regular "add" plus a
6120 // "setcc" instruction that checks the overflow flag. The "brcond" lowering
6121 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6122 // has only one use.
Bill Wendlingd06b4202008-11-26 22:37:40 +00006123 SDNode *N = Op.getNode();
Bill Wendlingd3511522008-12-02 01:06:39 +00006124 SDValue LHS = N->getOperand(0);
6125 SDValue RHS = N->getOperand(1);
Bill Wendlingd06b4202008-11-26 22:37:40 +00006126
Bill Wendlingd3511522008-12-02 01:06:39 +00006127 // Also sets EFLAGS.
6128 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
6129 SDValue Sum = DAG.getNode(ISD::ADD, VTs, LHS, RHS);
Bill Wendlingd06b4202008-11-26 22:37:40 +00006130
Bill Wendlingd3511522008-12-02 01:06:39 +00006131 SDValue SetCC =
6132 DAG.getNode(X86ISD::SETCC, N->getValueType(1),
6133 DAG.getConstant((Op.getOpcode() == ISD::SADDO) ?
6134 X86::COND_O : X86::COND_C,
6135 MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendlingd06b4202008-11-26 22:37:40 +00006136
Bill Wendlingd3511522008-12-02 01:06:39 +00006137 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6138 return Sum;
Bill Wendling4c134df2008-11-24 19:21:46 +00006139}
6140
Dan Gohman8181bd12008-07-27 21:46:04 +00006141SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006142 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00006143 unsigned Reg = 0;
6144 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006145 switch(T.getSimpleVT()) {
6146 default:
6147 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006148 case MVT::i8: Reg = X86::AL; size = 1; break;
6149 case MVT::i16: Reg = X86::AX; size = 2; break;
6150 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00006151 case MVT::i64:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006152 assert(Subtarget->is64Bit() && "Node not type legal!");
6153 Reg = X86::RAX; size = 8;
Andrew Lenharth81580822008-03-05 01:15:49 +00006154 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00006155 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006156 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00006157 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00006158 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006159 Op.getOperand(1),
6160 Op.getOperand(3),
6161 DAG.getTargetConstant(size, MVT::i8),
6162 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006163 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006164 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
6165 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006166 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
6167 return cpOut;
6168}
6169
Duncan Sands7d9834b2008-12-01 11:39:25 +00006170SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif825aa892008-08-28 23:19:51 +00006171 SelectionDAG &DAG) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006172 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharth81580822008-03-05 01:15:49 +00006173 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006174 SDValue TheChain = Op.getOperand(0);
6175 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6176 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
6177 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX, MVT::i64,
6178 rax.getValue(2));
6179 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
6180 DAG.getConstant(32, MVT::i8));
6181 SDValue Ops[] = {
6182 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp),
6183 rdx.getValue(1)
6184 };
6185 return DAG.getMergeValues(Ops, 2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006186}
6187
Dale Johannesen9011d872008-09-29 22:25:26 +00006188SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6189 SDNode *Node = Op.getNode();
6190 MVT T = Node->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006191 SDValue negOp = DAG.getNode(ISD::SUB, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006192 DAG.getConstant(0, T), Node->getOperand(2));
6193 return DAG.getAtomic((Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_8 ?
6194 ISD::ATOMIC_LOAD_ADD_8 :
6195 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_16 ?
6196 ISD::ATOMIC_LOAD_ADD_16 :
6197 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_32 ?
6198 ISD::ATOMIC_LOAD_ADD_32 :
6199 ISD::ATOMIC_LOAD_ADD_64),
6200 Node->getOperand(0),
6201 Node->getOperand(1), negOp,
6202 cast<AtomicSDNode>(Node)->getSrcValue(),
6203 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006204}
6205
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006206/// LowerOperation - Provide custom lowering hooks for some operations.
6207///
Dan Gohman8181bd12008-07-27 21:46:04 +00006208SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006209 switch (Op.getOpcode()) {
6210 default: assert(0 && "Should not custom lower this!");
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006211 case ISD::ATOMIC_CMP_SWAP_8:
6212 case ISD::ATOMIC_CMP_SWAP_16:
6213 case ISD::ATOMIC_CMP_SWAP_32:
Dale Johannesenbc187662008-08-28 02:44:49 +00006214 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006215 case ISD::ATOMIC_LOAD_SUB_8:
6216 case ISD::ATOMIC_LOAD_SUB_16:
Dale Johannesen9011d872008-09-29 22:25:26 +00006217 case ISD::ATOMIC_LOAD_SUB_32: return LowerLOAD_SUB(Op,DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006218 case ISD::ATOMIC_LOAD_SUB_64: return LowerLOAD_SUB(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006219 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6220 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6221 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6222 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6223 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6224 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6225 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6226 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006227 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006228 case ISD::SHL_PARTS:
6229 case ISD::SRA_PARTS:
6230 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6231 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00006232 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006233 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6234 case ISD::FABS: return LowerFABS(Op, DAG);
6235 case ISD::FNEG: return LowerFNEG(Op, DAG);
6236 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006237 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006238 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006239 case ISD::SELECT: return LowerSELECT(Op, DAG);
6240 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006241 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6242 case ISD::CALL: return LowerCALL(Op, DAG);
6243 case ISD::RET: return LowerRET(Op, DAG);
6244 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006245 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006246 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006247 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6248 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6249 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6250 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6251 case ISD::FRAME_TO_ARGS_OFFSET:
6252 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6253 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6254 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006255 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006256 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006257 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6258 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Bill Wendlingd3511522008-12-02 01:06:39 +00006259 case ISD::SADDO: return LowerXADDO(Op, DAG);
6260 case ISD::UADDO: return LowerXADDO(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006261 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006262 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006263}
6264
Duncan Sands7d9834b2008-12-01 11:39:25 +00006265void X86TargetLowering::
6266ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6267 SelectionDAG &DAG, unsigned NewOp) {
6268 MVT T = Node->getValueType(0);
6269 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6270
6271 SDValue Chain = Node->getOperand(0);
6272 SDValue In1 = Node->getOperand(1);
6273 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6274 Node->getOperand(2), DAG.getIntPtrConstant(0));
6275 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6276 Node->getOperand(2), DAG.getIntPtrConstant(1));
6277 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6278 // have a MemOperand. Pass the info through as a normal operand.
6279 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6280 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6281 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
6282 SDValue Result = DAG.getNode(NewOp, Tys, Ops, 5);
6283 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
6284 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6285 Results.push_back(Result.getValue(2));
6286}
6287
Duncan Sandsac496a12008-07-04 11:47:58 +00006288/// ReplaceNodeResults - Replace a node with an illegal result type
6289/// with a new node built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00006290void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6291 SmallVectorImpl<SDValue>&Results,
6292 SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006293 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006294 default:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006295 assert(false && "Do not know how to custom type legalize this operation!");
6296 return;
6297 case ISD::FP_TO_SINT: {
6298 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
6299 SDValue FIST = Vals.first, StackSlot = Vals.second;
6300 if (FIST.getNode() != 0) {
6301 MVT VT = N->getValueType(0);
6302 // Return a load from the stack slot.
6303 Results.push_back(DAG.getLoad(VT, FIST, StackSlot, NULL, 0));
6304 }
6305 return;
6306 }
6307 case ISD::READCYCLECOUNTER: {
6308 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6309 SDValue TheChain = N->getOperand(0);
6310 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6311 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
6312 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX, MVT::i32,
6313 eax.getValue(2));
6314 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6315 SDValue Ops[] = { eax, edx };
6316 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2));
6317 Results.push_back(edx.getValue(1));
6318 return;
6319 }
6320 case ISD::ATOMIC_CMP_SWAP_64: {
6321 MVT T = N->getValueType(0);
6322 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6323 SDValue cpInL, cpInH;
6324 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6325 DAG.getConstant(0, MVT::i32));
6326 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6327 DAG.getConstant(1, MVT::i32));
6328 cpInL = DAG.getCopyToReg(N->getOperand(0), X86::EAX, cpInL, SDValue());
6329 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX, cpInH,
6330 cpInL.getValue(1));
6331 SDValue swapInL, swapInH;
6332 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6333 DAG.getConstant(0, MVT::i32));
6334 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6335 DAG.getConstant(1, MVT::i32));
6336 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX, swapInL,
6337 cpInH.getValue(1));
6338 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX, swapInH,
6339 swapInL.getValue(1));
6340 SDValue Ops[] = { swapInH.getValue(0),
6341 N->getOperand(1),
6342 swapInH.getValue(1) };
6343 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6344 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
6345 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
6346 Result.getValue(1));
6347 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
6348 cpOutL.getValue(2));
6349 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
6350 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6351 Results.push_back(cpOutH.getValue(1));
6352 return;
6353 }
6354 case ISD::ATOMIC_LOAD_ADD_64:
6355 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6356 return;
6357 case ISD::ATOMIC_LOAD_AND_64:
6358 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6359 return;
6360 case ISD::ATOMIC_LOAD_NAND_64:
6361 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6362 return;
6363 case ISD::ATOMIC_LOAD_OR_64:
6364 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6365 return;
6366 case ISD::ATOMIC_LOAD_SUB_64:
6367 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6368 return;
6369 case ISD::ATOMIC_LOAD_XOR_64:
6370 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6371 return;
6372 case ISD::ATOMIC_SWAP_64:
6373 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6374 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006375 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006376}
6377
6378const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6379 switch (Opcode) {
6380 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006381 case X86ISD::BSF: return "X86ISD::BSF";
6382 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006383 case X86ISD::SHLD: return "X86ISD::SHLD";
6384 case X86ISD::SHRD: return "X86ISD::SHRD";
6385 case X86ISD::FAND: return "X86ISD::FAND";
6386 case X86ISD::FOR: return "X86ISD::FOR";
6387 case X86ISD::FXOR: return "X86ISD::FXOR";
6388 case X86ISD::FSRL: return "X86ISD::FSRL";
6389 case X86ISD::FILD: return "X86ISD::FILD";
6390 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6391 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6392 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6393 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6394 case X86ISD::FLD: return "X86ISD::FLD";
6395 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006396 case X86ISD::CALL: return "X86ISD::CALL";
6397 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6398 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6399 case X86ISD::CMP: return "X86ISD::CMP";
6400 case X86ISD::COMI: return "X86ISD::COMI";
6401 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6402 case X86ISD::SETCC: return "X86ISD::SETCC";
6403 case X86ISD::CMOV: return "X86ISD::CMOV";
6404 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6405 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6406 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6407 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006408 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6409 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006410 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006411 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006412 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6413 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006414 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6415 case X86ISD::FMAX: return "X86ISD::FMAX";
6416 case X86ISD::FMIN: return "X86ISD::FMIN";
6417 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6418 case X86ISD::FRCP: return "X86ISD::FRCP";
6419 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6420 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6421 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006422 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006423 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006424 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6425 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006426 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6427 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6428 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6429 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6430 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6431 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006432 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6433 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006434 case X86ISD::VSHL: return "X86ISD::VSHL";
6435 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006436 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6437 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6438 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6439 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6440 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6441 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6442 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6443 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6444 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6445 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006446 }
6447}
6448
6449// isLegalAddressingMode - Return true if the addressing mode represented
6450// by AM is legal for this target, for a load/store of the specified type.
6451bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6452 const Type *Ty) const {
6453 // X86 supports extremely general addressing modes.
6454
6455 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6456 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6457 return false;
6458
6459 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006460 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006461 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6462 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006463
6464 // X86-64 only supports addr of globals in small code model.
6465 if (Subtarget->is64Bit()) {
6466 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6467 return false;
6468 // If lower 4G is not available, then we must use rip-relative addressing.
6469 if (AM.BaseOffs || AM.Scale > 1)
6470 return false;
6471 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006472 }
6473
6474 switch (AM.Scale) {
6475 case 0:
6476 case 1:
6477 case 2:
6478 case 4:
6479 case 8:
6480 // These scales always work.
6481 break;
6482 case 3:
6483 case 5:
6484 case 9:
6485 // These scales are formed with basereg+scalereg. Only accept if there is
6486 // no basereg yet.
6487 if (AM.HasBaseReg)
6488 return false;
6489 break;
6490 default: // Other stuff never works.
6491 return false;
6492 }
6493
6494 return true;
6495}
6496
6497
Evan Cheng27a820a2007-10-26 01:56:11 +00006498bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6499 if (!Ty1->isInteger() || !Ty2->isInteger())
6500 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006501 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6502 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006503 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006504 return false;
6505 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006506}
6507
Duncan Sands92c43912008-06-06 12:08:01 +00006508bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6509 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006510 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006511 unsigned NumBits1 = VT1.getSizeInBits();
6512 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006513 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006514 return false;
6515 return Subtarget->is64Bit() || NumBits1 < 64;
6516}
Evan Cheng27a820a2007-10-26 01:56:11 +00006517
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006518/// isShuffleMaskLegal - Targets can use this to indicate that they only
6519/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6520/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6521/// are assumed to be legal.
6522bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006523X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006524 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006525 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006526 return (Mask.getNode()->getNumOperands() <= 4 ||
6527 isIdentityMask(Mask.getNode()) ||
6528 isIdentityMask(Mask.getNode(), true) ||
6529 isSplatMask(Mask.getNode()) ||
6530 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6531 X86::isUNPCKLMask(Mask.getNode()) ||
6532 X86::isUNPCKHMask(Mask.getNode()) ||
6533 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6534 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006535}
6536
Dan Gohman48d5f062008-04-09 20:09:42 +00006537bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006538X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006539 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006540 unsigned NumElts = BVOps.size();
6541 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006542 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006543 if (NumElts == 2) return true;
6544 if (NumElts == 4) {
6545 return (isMOVLMask(&BVOps[0], 4) ||
6546 isCommutedMOVL(&BVOps[0], 4, true) ||
6547 isSHUFPMask(&BVOps[0], 4) ||
6548 isCommutedSHUFP(&BVOps[0], 4));
6549 }
6550 return false;
6551}
6552
6553//===----------------------------------------------------------------------===//
6554// X86 Scheduler Hooks
6555//===----------------------------------------------------------------------===//
6556
Mon P Wang078a62d2008-05-05 19:05:59 +00006557// private utility function
6558MachineBasicBlock *
6559X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6560 MachineBasicBlock *MBB,
6561 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006562 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006563 unsigned LoadOpc,
6564 unsigned CXchgOpc,
6565 unsigned copyOpc,
6566 unsigned notOpc,
6567 unsigned EAXreg,
6568 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006569 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006570 // For the atomic bitwise operator, we generate
6571 // thisMBB:
6572 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006573 // ld t1 = [bitinstr.addr]
6574 // op t2 = t1, [bitinstr.val]
6575 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006576 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6577 // bz newMBB
6578 // fallthrough -->nextMBB
6579 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6580 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006581 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006582 ++MBBIter;
6583
6584 /// First build the CFG
6585 MachineFunction *F = MBB->getParent();
6586 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006587 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6588 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6589 F->insert(MBBIter, newMBB);
6590 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006591
6592 // Move all successors to thisMBB to nextMBB
6593 nextMBB->transferSuccessors(thisMBB);
6594
6595 // Update thisMBB to fall through to newMBB
6596 thisMBB->addSuccessor(newMBB);
6597
6598 // newMBB jumps to itself and fall through to nextMBB
6599 newMBB->addSuccessor(nextMBB);
6600 newMBB->addSuccessor(newMBB);
6601
6602 // Insert instructions into newMBB based on incoming instruction
6603 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6604 MachineOperand& destOper = bInstr->getOperand(0);
6605 MachineOperand* argOpers[6];
6606 int numArgs = bInstr->getNumOperands() - 1;
6607 for (int i=0; i < numArgs; ++i)
6608 argOpers[i] = &bInstr->getOperand(i+1);
6609
6610 // x86 address has 4 operands: base, index, scale, and displacement
6611 int lastAddrIndx = 3; // [0,3]
6612 int valArgIndx = 4;
6613
Dale Johannesend20e4452008-08-19 18:47:28 +00006614 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6615 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006616 for (int i=0; i <= lastAddrIndx; ++i)
6617 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006618
Dale Johannesend20e4452008-08-19 18:47:28 +00006619 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006620 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006621 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006622 }
6623 else
6624 tt = t1;
6625
Dale Johannesend20e4452008-08-19 18:47:28 +00006626 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006627 assert((argOpers[valArgIndx]->isReg() ||
6628 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006629 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006630 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006631 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6632 else
6633 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006634 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006635 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006636
Dale Johannesend20e4452008-08-19 18:47:28 +00006637 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006638 MIB.addReg(t1);
6639
Dale Johannesend20e4452008-08-19 18:47:28 +00006640 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006641 for (int i=0; i <= lastAddrIndx; ++i)
6642 (*MIB).addOperand(*argOpers[i]);
6643 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006644 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6645 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6646
Dale Johannesend20e4452008-08-19 18:47:28 +00006647 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6648 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006649
6650 // insert branch
6651 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6652
Dan Gohman221a4372008-07-07 23:14:23 +00006653 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006654 return nextMBB;
6655}
6656
Dale Johannesen44eb5372008-10-03 19:41:08 +00006657// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00006658MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00006659X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
6660 MachineBasicBlock *MBB,
6661 unsigned regOpcL,
6662 unsigned regOpcH,
6663 unsigned immOpcL,
6664 unsigned immOpcH,
6665 bool invSrc) {
6666 // For the atomic bitwise operator, we generate
6667 // thisMBB (instructions are in pairs, except cmpxchg8b)
6668 // ld t1,t2 = [bitinstr.addr]
6669 // newMBB:
6670 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
6671 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006672 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00006673 // mov ECX, EBX <- t5, t6
6674 // mov EAX, EDX <- t1, t2
6675 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
6676 // mov t3, t4 <- EAX, EDX
6677 // bz newMBB
6678 // result in out1, out2
6679 // fallthrough -->nextMBB
6680
6681 const TargetRegisterClass *RC = X86::GR32RegisterClass;
6682 const unsigned LoadOpc = X86::MOV32rm;
6683 const unsigned copyOpc = X86::MOV32rr;
6684 const unsigned NotOpc = X86::NOT32r;
6685 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6686 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6687 MachineFunction::iterator MBBIter = MBB;
6688 ++MBBIter;
6689
6690 /// First build the CFG
6691 MachineFunction *F = MBB->getParent();
6692 MachineBasicBlock *thisMBB = MBB;
6693 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6694 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6695 F->insert(MBBIter, newMBB);
6696 F->insert(MBBIter, nextMBB);
6697
6698 // Move all successors to thisMBB to nextMBB
6699 nextMBB->transferSuccessors(thisMBB);
6700
6701 // Update thisMBB to fall through to newMBB
6702 thisMBB->addSuccessor(newMBB);
6703
6704 // newMBB jumps to itself and fall through to nextMBB
6705 newMBB->addSuccessor(nextMBB);
6706 newMBB->addSuccessor(newMBB);
6707
6708 // Insert instructions into newMBB based on incoming instruction
6709 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
6710 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
6711 MachineOperand& dest1Oper = bInstr->getOperand(0);
6712 MachineOperand& dest2Oper = bInstr->getOperand(1);
6713 MachineOperand* argOpers[6];
6714 for (int i=0; i < 6; ++i)
6715 argOpers[i] = &bInstr->getOperand(i+2);
6716
6717 // x86 address has 4 operands: base, index, scale, and displacement
6718 int lastAddrIndx = 3; // [0,3]
6719
6720 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6721 MachineInstrBuilder MIB = BuildMI(thisMBB, TII->get(LoadOpc), t1);
6722 for (int i=0; i <= lastAddrIndx; ++i)
6723 (*MIB).addOperand(*argOpers[i]);
6724 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
6725 MIB = BuildMI(thisMBB, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006726 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00006727 for (int i=0; i <= lastAddrIndx-1; ++i)
6728 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006729 MachineOperand newOp3 = *(argOpers[3]);
6730 if (newOp3.isImm())
6731 newOp3.setImm(newOp3.getImm()+4);
6732 else
6733 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00006734 (*MIB).addOperand(newOp3);
6735
6736 // t3/4 are defined later, at the bottom of the loop
6737 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
6738 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
6739 BuildMI(newMBB, TII->get(X86::PHI), dest1Oper.getReg())
6740 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
6741 BuildMI(newMBB, TII->get(X86::PHI), dest2Oper.getReg())
6742 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
6743
6744 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
6745 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
6746 if (invSrc) {
6747 MIB = BuildMI(newMBB, TII->get(NotOpc), tt1).addReg(t1);
6748 MIB = BuildMI(newMBB, TII->get(NotOpc), tt2).addReg(t2);
6749 } else {
6750 tt1 = t1;
6751 tt2 = t2;
6752 }
6753
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006754 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00006755 "invalid operand");
6756 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
6757 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006758 if (argOpers[4]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006759 MIB = BuildMI(newMBB, TII->get(regOpcL), t5);
6760 else
6761 MIB = BuildMI(newMBB, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006762 if (regOpcL != X86::MOV32rr)
6763 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00006764 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006765 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
6766 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
6767 if (argOpers[5]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006768 MIB = BuildMI(newMBB, TII->get(regOpcH), t6);
6769 else
6770 MIB = BuildMI(newMBB, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006771 if (regOpcH != X86::MOV32rr)
6772 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006773 (*MIB).addOperand(*argOpers[5]);
6774
6775 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EAX);
6776 MIB.addReg(t1);
6777 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EDX);
6778 MIB.addReg(t2);
6779
6780 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EBX);
6781 MIB.addReg(t5);
6782 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::ECX);
6783 MIB.addReg(t6);
6784
6785 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG8B));
6786 for (int i=0; i <= lastAddrIndx; ++i)
6787 (*MIB).addOperand(*argOpers[i]);
6788
6789 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6790 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6791
6792 MIB = BuildMI(newMBB, TII->get(copyOpc), t3);
6793 MIB.addReg(X86::EAX);
6794 MIB = BuildMI(newMBB, TII->get(copyOpc), t4);
6795 MIB.addReg(X86::EDX);
6796
6797 // insert branch
6798 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6799
6800 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
6801 return nextMBB;
6802}
6803
6804// private utility function
6805MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00006806X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6807 MachineBasicBlock *MBB,
6808 unsigned cmovOpc) {
6809 // For the atomic min/max operator, we generate
6810 // thisMBB:
6811 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006812 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006813 // mov t2 = [min/max.val]
6814 // cmp t1, t2
6815 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006816 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006817 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6818 // bz newMBB
6819 // fallthrough -->nextMBB
6820 //
6821 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6822 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006823 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006824 ++MBBIter;
6825
6826 /// First build the CFG
6827 MachineFunction *F = MBB->getParent();
6828 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006829 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6830 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6831 F->insert(MBBIter, newMBB);
6832 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006833
6834 // Move all successors to thisMBB to nextMBB
6835 nextMBB->transferSuccessors(thisMBB);
6836
6837 // Update thisMBB to fall through to newMBB
6838 thisMBB->addSuccessor(newMBB);
6839
6840 // newMBB jumps to newMBB and fall through to nextMBB
6841 newMBB->addSuccessor(nextMBB);
6842 newMBB->addSuccessor(newMBB);
6843
6844 // Insert instructions into newMBB based on incoming instruction
6845 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6846 MachineOperand& destOper = mInstr->getOperand(0);
6847 MachineOperand* argOpers[6];
6848 int numArgs = mInstr->getNumOperands() - 1;
6849 for (int i=0; i < numArgs; ++i)
6850 argOpers[i] = &mInstr->getOperand(i+1);
6851
6852 // x86 address has 4 operands: base, index, scale, and displacement
6853 int lastAddrIndx = 3; // [0,3]
6854 int valArgIndx = 4;
6855
Mon P Wang318b0372008-05-05 22:56:23 +00006856 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6857 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006858 for (int i=0; i <= lastAddrIndx; ++i)
6859 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006860
Mon P Wang078a62d2008-05-05 19:05:59 +00006861 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006862 assert((argOpers[valArgIndx]->isReg() ||
6863 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006864 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00006865
6866 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006867 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006868 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6869 else
6870 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6871 (*MIB).addOperand(*argOpers[valArgIndx]);
6872
Mon P Wang318b0372008-05-05 22:56:23 +00006873 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6874 MIB.addReg(t1);
6875
Mon P Wang078a62d2008-05-05 19:05:59 +00006876 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6877 MIB.addReg(t1);
6878 MIB.addReg(t2);
6879
6880 // Generate movc
6881 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6882 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6883 MIB.addReg(t2);
6884 MIB.addReg(t1);
6885
6886 // Cmp and exchange if none has modified the memory location
6887 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6888 for (int i=0; i <= lastAddrIndx; ++i)
6889 (*MIB).addOperand(*argOpers[i]);
6890 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006891 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6892 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006893
6894 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6895 MIB.addReg(X86::EAX);
6896
6897 // insert branch
6898 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6899
Dan Gohman221a4372008-07-07 23:14:23 +00006900 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006901 return nextMBB;
6902}
6903
6904
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006905MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006906X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6907 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006908 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6909 switch (MI->getOpcode()) {
6910 default: assert(false && "Unexpected instr type to insert");
6911 case X86::CMOV_FR32:
6912 case X86::CMOV_FR64:
6913 case X86::CMOV_V4F32:
6914 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006915 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006916 // To "insert" a SELECT_CC instruction, we actually have to insert the
6917 // diamond control-flow pattern. The incoming instruction knows the
6918 // destination vreg to set, the condition code register to branch on, the
6919 // true/false values to select between, and a branch opcode to use.
6920 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006921 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006922 ++It;
6923
6924 // thisMBB:
6925 // ...
6926 // TrueVal = ...
6927 // cmpTY ccX, r1, r2
6928 // bCC copy1MBB
6929 // fallthrough --> copy0MBB
6930 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006931 MachineFunction *F = BB->getParent();
6932 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6933 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006934 unsigned Opc =
6935 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6936 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006937 F->insert(It, copy0MBB);
6938 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006939 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006940 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006941 sinkMBB->transferSuccessors(BB);
6942
6943 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006944 BB->addSuccessor(copy0MBB);
6945 BB->addSuccessor(sinkMBB);
6946
6947 // copy0MBB:
6948 // %FalseValue = ...
6949 // # fallthrough to sinkMBB
6950 BB = copy0MBB;
6951
6952 // Update machine-CFG edges
6953 BB->addSuccessor(sinkMBB);
6954
6955 // sinkMBB:
6956 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6957 // ...
6958 BB = sinkMBB;
6959 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6960 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6961 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6962
Dan Gohman221a4372008-07-07 23:14:23 +00006963 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006964 return BB;
6965 }
6966
6967 case X86::FP32_TO_INT16_IN_MEM:
6968 case X86::FP32_TO_INT32_IN_MEM:
6969 case X86::FP32_TO_INT64_IN_MEM:
6970 case X86::FP64_TO_INT16_IN_MEM:
6971 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006972 case X86::FP64_TO_INT64_IN_MEM:
6973 case X86::FP80_TO_INT16_IN_MEM:
6974 case X86::FP80_TO_INT32_IN_MEM:
6975 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006976 // Change the floating point control register to use "round towards zero"
6977 // mode when truncating to an integer value.
6978 MachineFunction *F = BB->getParent();
6979 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6980 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6981
6982 // Load the old value of the high byte of the control word...
6983 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006984 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006985 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6986
6987 // Set the high part to be round to zero...
6988 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6989 .addImm(0xC7F);
6990
6991 // Reload the modified control word now...
6992 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6993
6994 // Restore the memory image of control word to original value
6995 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6996 .addReg(OldCW);
6997
6998 // Get the X86 opcode to use.
6999 unsigned Opc;
7000 switch (MI->getOpcode()) {
7001 default: assert(0 && "illegal opcode!");
7002 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7003 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7004 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7005 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7006 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7007 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007008 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7009 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7010 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007011 }
7012
7013 X86AddressMode AM;
7014 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007015 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007016 AM.BaseType = X86AddressMode::RegBase;
7017 AM.Base.Reg = Op.getReg();
7018 } else {
7019 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00007020 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007021 }
7022 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007023 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007024 AM.Scale = Op.getImm();
7025 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007026 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007027 AM.IndexReg = Op.getImm();
7028 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007029 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007030 AM.GV = Op.getGlobal();
7031 } else {
7032 AM.Disp = Op.getImm();
7033 }
7034 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
7035 .addReg(MI->getOperand(4).getReg());
7036
7037 // Reload the original control word now.
7038 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7039
Dan Gohman221a4372008-07-07 23:14:23 +00007040 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007041 return BB;
7042 }
Mon P Wang078a62d2008-05-05 19:05:59 +00007043 case X86::ATOMAND32:
7044 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007045 X86::AND32ri, X86::MOV32rm,
7046 X86::LCMPXCHG32, X86::MOV32rr,
7047 X86::NOT32r, X86::EAX,
7048 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007049 case X86::ATOMOR32:
7050 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007051 X86::OR32ri, X86::MOV32rm,
7052 X86::LCMPXCHG32, X86::MOV32rr,
7053 X86::NOT32r, X86::EAX,
7054 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007055 case X86::ATOMXOR32:
7056 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007057 X86::XOR32ri, X86::MOV32rm,
7058 X86::LCMPXCHG32, X86::MOV32rr,
7059 X86::NOT32r, X86::EAX,
7060 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007061 case X86::ATOMNAND32:
7062 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007063 X86::AND32ri, X86::MOV32rm,
7064 X86::LCMPXCHG32, X86::MOV32rr,
7065 X86::NOT32r, X86::EAX,
7066 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00007067 case X86::ATOMMIN32:
7068 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7069 case X86::ATOMMAX32:
7070 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7071 case X86::ATOMUMIN32:
7072 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7073 case X86::ATOMUMAX32:
7074 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00007075
7076 case X86::ATOMAND16:
7077 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7078 X86::AND16ri, X86::MOV16rm,
7079 X86::LCMPXCHG16, X86::MOV16rr,
7080 X86::NOT16r, X86::AX,
7081 X86::GR16RegisterClass);
7082 case X86::ATOMOR16:
7083 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7084 X86::OR16ri, X86::MOV16rm,
7085 X86::LCMPXCHG16, X86::MOV16rr,
7086 X86::NOT16r, X86::AX,
7087 X86::GR16RegisterClass);
7088 case X86::ATOMXOR16:
7089 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7090 X86::XOR16ri, X86::MOV16rm,
7091 X86::LCMPXCHG16, X86::MOV16rr,
7092 X86::NOT16r, X86::AX,
7093 X86::GR16RegisterClass);
7094 case X86::ATOMNAND16:
7095 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7096 X86::AND16ri, X86::MOV16rm,
7097 X86::LCMPXCHG16, X86::MOV16rr,
7098 X86::NOT16r, X86::AX,
7099 X86::GR16RegisterClass, true);
7100 case X86::ATOMMIN16:
7101 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7102 case X86::ATOMMAX16:
7103 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7104 case X86::ATOMUMIN16:
7105 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7106 case X86::ATOMUMAX16:
7107 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7108
7109 case X86::ATOMAND8:
7110 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7111 X86::AND8ri, X86::MOV8rm,
7112 X86::LCMPXCHG8, X86::MOV8rr,
7113 X86::NOT8r, X86::AL,
7114 X86::GR8RegisterClass);
7115 case X86::ATOMOR8:
7116 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7117 X86::OR8ri, X86::MOV8rm,
7118 X86::LCMPXCHG8, X86::MOV8rr,
7119 X86::NOT8r, X86::AL,
7120 X86::GR8RegisterClass);
7121 case X86::ATOMXOR8:
7122 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7123 X86::XOR8ri, X86::MOV8rm,
7124 X86::LCMPXCHG8, X86::MOV8rr,
7125 X86::NOT8r, X86::AL,
7126 X86::GR8RegisterClass);
7127 case X86::ATOMNAND8:
7128 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7129 X86::AND8ri, X86::MOV8rm,
7130 X86::LCMPXCHG8, X86::MOV8rr,
7131 X86::NOT8r, X86::AL,
7132 X86::GR8RegisterClass, true);
7133 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00007134 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007135 case X86::ATOMAND64:
7136 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7137 X86::AND64ri32, X86::MOV64rm,
7138 X86::LCMPXCHG64, X86::MOV64rr,
7139 X86::NOT64r, X86::RAX,
7140 X86::GR64RegisterClass);
7141 case X86::ATOMOR64:
7142 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7143 X86::OR64ri32, X86::MOV64rm,
7144 X86::LCMPXCHG64, X86::MOV64rr,
7145 X86::NOT64r, X86::RAX,
7146 X86::GR64RegisterClass);
7147 case X86::ATOMXOR64:
7148 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7149 X86::XOR64ri32, X86::MOV64rm,
7150 X86::LCMPXCHG64, X86::MOV64rr,
7151 X86::NOT64r, X86::RAX,
7152 X86::GR64RegisterClass);
7153 case X86::ATOMNAND64:
7154 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7155 X86::AND64ri32, X86::MOV64rm,
7156 X86::LCMPXCHG64, X86::MOV64rr,
7157 X86::NOT64r, X86::RAX,
7158 X86::GR64RegisterClass, true);
7159 case X86::ATOMMIN64:
7160 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7161 case X86::ATOMMAX64:
7162 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7163 case X86::ATOMUMIN64:
7164 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7165 case X86::ATOMUMAX64:
7166 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00007167
7168 // This group does 64-bit operations on a 32-bit host.
7169 case X86::ATOMAND6432:
7170 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7171 X86::AND32rr, X86::AND32rr,
7172 X86::AND32ri, X86::AND32ri,
7173 false);
7174 case X86::ATOMOR6432:
7175 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7176 X86::OR32rr, X86::OR32rr,
7177 X86::OR32ri, X86::OR32ri,
7178 false);
7179 case X86::ATOMXOR6432:
7180 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7181 X86::XOR32rr, X86::XOR32rr,
7182 X86::XOR32ri, X86::XOR32ri,
7183 false);
7184 case X86::ATOMNAND6432:
7185 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7186 X86::AND32rr, X86::AND32rr,
7187 X86::AND32ri, X86::AND32ri,
7188 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00007189 case X86::ATOMADD6432:
7190 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7191 X86::ADD32rr, X86::ADC32rr,
7192 X86::ADD32ri, X86::ADC32ri,
7193 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00007194 case X86::ATOMSUB6432:
7195 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7196 X86::SUB32rr, X86::SBB32rr,
7197 X86::SUB32ri, X86::SBB32ri,
7198 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007199 case X86::ATOMSWAP6432:
7200 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7201 X86::MOV32rr, X86::MOV32rr,
7202 X86::MOV32ri, X86::MOV32ri,
7203 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007204 }
7205}
7206
7207//===----------------------------------------------------------------------===//
7208// X86 Optimization Hooks
7209//===----------------------------------------------------------------------===//
7210
Dan Gohman8181bd12008-07-27 21:46:04 +00007211void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00007212 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00007213 APInt &KnownZero,
7214 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007215 const SelectionDAG &DAG,
7216 unsigned Depth) const {
7217 unsigned Opc = Op.getOpcode();
7218 assert((Opc >= ISD::BUILTIN_OP_END ||
7219 Opc == ISD::INTRINSIC_WO_CHAIN ||
7220 Opc == ISD::INTRINSIC_W_CHAIN ||
7221 Opc == ISD::INTRINSIC_VOID) &&
7222 "Should use MaskedValueIsZero if you don't know whether Op"
7223 " is a target node!");
7224
Dan Gohman1d79e432008-02-13 23:07:24 +00007225 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007226 switch (Opc) {
7227 default: break;
7228 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007229 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7230 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007231 break;
7232 }
7233}
7234
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007235/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007236/// node is a GlobalAddress + offset.
7237bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7238 GlobalValue* &GA, int64_t &Offset) const{
7239 if (N->getOpcode() == X86ISD::Wrapper) {
7240 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007241 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00007242 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007243 return true;
7244 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007245 }
Evan Chengef7be082008-05-12 19:56:52 +00007246 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007247}
7248
Evan Chengef7be082008-05-12 19:56:52 +00007249static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7250 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007251 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007252 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007253 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007254 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007255 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007256 return false;
7257}
7258
Dan Gohman8181bd12008-07-27 21:46:04 +00007259static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007260 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007261 SDNode *&Base,
7262 SelectionDAG &DAG, MachineFrameInfo *MFI,
7263 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007264 Base = NULL;
7265 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007266 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007267 if (Idx.getOpcode() == ISD::UNDEF) {
7268 if (!Base)
7269 return false;
7270 continue;
7271 }
7272
Dan Gohman8181bd12008-07-27 21:46:04 +00007273 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007274 if (!Elt.getNode() ||
7275 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007276 return false;
7277 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007278 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007279 if (Base->getOpcode() == ISD::UNDEF)
7280 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007281 continue;
7282 }
7283 if (Elt.getOpcode() == ISD::UNDEF)
7284 continue;
7285
Gabor Greif1c80d112008-08-28 21:40:38 +00007286 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007287 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007288 return false;
7289 }
7290 return true;
7291}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007292
7293/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7294/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7295/// if the load addresses are consecutive, non-overlapping, and in the right
7296/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007297static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007298 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007299 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00007300 MVT VT = N->getValueType(0);
7301 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007302 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007303 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007304 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007305 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7306 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007307 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007308
Dan Gohman11821702007-07-27 17:16:43 +00007309 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007310 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007311 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00007312 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00007313 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
7314 LD->getSrcValueOffset(), LD->isVolatile(),
7315 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007316}
7317
Evan Chengb6290462008-05-12 23:04:07 +00007318/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007319static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng6617eed2008-09-24 23:26:36 +00007320 const X86Subtarget *Subtarget,
7321 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007322 unsigned NumOps = N->getNumOperands();
7323
Evan Chenge9b9c672008-05-09 21:53:03 +00007324 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007325 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007326 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007327
Duncan Sands92c43912008-06-06 12:08:01 +00007328 MVT VT = N->getValueType(0);
7329 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007330 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7331 // We are looking for load i64 and zero extend. We want to transform
7332 // it before legalizer has a chance to expand it. Also look for i64
7333 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007334 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007335 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007336 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007337 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007338 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007339
7340 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007341 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007342 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007343 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007344 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007345 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007346 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007347 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007348 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007349
7350 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007351 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007352
7353 // Load must not be an extload.
7354 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007355 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00007356
Evan Cheng6617eed2008-09-24 23:26:36 +00007357 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7358 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7359 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
7360 DAG.ReplaceAllUsesOfValueWith(SDValue(Base, 1), ResNode.getValue(1));
7361 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007362}
7363
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007364/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007365static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007366 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007367 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007368
7369 // If we have SSE[12] support, try to form min/max nodes.
7370 if (Subtarget->hasSSE2() &&
7371 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7372 if (Cond.getOpcode() == ISD::SETCC) {
7373 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007374 SDValue LHS = N->getOperand(1);
7375 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007376 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7377
7378 unsigned Opcode = 0;
7379 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7380 switch (CC) {
7381 default: break;
7382 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7383 case ISD::SETULE:
7384 case ISD::SETLE:
7385 if (!UnsafeFPMath) break;
7386 // FALL THROUGH.
7387 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7388 case ISD::SETLT:
7389 Opcode = X86ISD::FMIN;
7390 break;
7391
7392 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7393 case ISD::SETUGT:
7394 case ISD::SETGT:
7395 if (!UnsafeFPMath) break;
7396 // FALL THROUGH.
7397 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7398 case ISD::SETGE:
7399 Opcode = X86ISD::FMAX;
7400 break;
7401 }
7402 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7403 switch (CC) {
7404 default: break;
7405 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7406 case ISD::SETUGT:
7407 case ISD::SETGT:
7408 if (!UnsafeFPMath) break;
7409 // FALL THROUGH.
7410 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7411 case ISD::SETGE:
7412 Opcode = X86ISD::FMIN;
7413 break;
7414
7415 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7416 case ISD::SETULE:
7417 case ISD::SETLE:
7418 if (!UnsafeFPMath) break;
7419 // FALL THROUGH.
7420 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7421 case ISD::SETLT:
7422 Opcode = X86ISD::FMAX;
7423 break;
7424 }
7425 }
7426
7427 if (Opcode)
7428 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
7429 }
7430
7431 }
7432
Dan Gohman8181bd12008-07-27 21:46:04 +00007433 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007434}
7435
Chris Lattnerce84ae42008-02-22 02:09:43 +00007436/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007437static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007438 const X86Subtarget *Subtarget) {
7439 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7440 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007441 // A preferable solution to the general problem is to figure out the right
7442 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007443 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007444 if (St->getValue().getValueType().isVector() &&
7445 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007446 isa<LoadSDNode>(St->getValue()) &&
7447 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7448 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007449 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007450 LoadSDNode *Ld = 0;
7451 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007452 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007453 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007454 // Must be a store of a load. We currently handle two cases: the load
7455 // is a direct child, and it's under an intervening TokenFactor. It is
7456 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007457 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007458 Ld = cast<LoadSDNode>(St->getChain());
7459 else if (St->getValue().hasOneUse() &&
7460 ChainVal->getOpcode() == ISD::TokenFactor) {
7461 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007462 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007463 TokenFactorIndex = i;
7464 Ld = cast<LoadSDNode>(St->getValue());
7465 } else
7466 Ops.push_back(ChainVal->getOperand(i));
7467 }
7468 }
7469 if (Ld) {
7470 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7471 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007472 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007473 Ld->getBasePtr(), Ld->getSrcValue(),
7474 Ld->getSrcValueOffset(), Ld->isVolatile(),
7475 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007476 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007477 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00007478 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00007479 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7480 Ops.size());
7481 }
7482 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
7483 St->getSrcValue(), St->getSrcValueOffset(),
7484 St->isVolatile(), St->getAlignment());
7485 }
7486
7487 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00007488 SDValue LoAddr = Ld->getBasePtr();
7489 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007490 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007491
Dan Gohman8181bd12008-07-27 21:46:04 +00007492 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007493 Ld->getSrcValue(), Ld->getSrcValueOffset(),
7494 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007495 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007496 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
7497 Ld->isVolatile(),
7498 MinAlign(Ld->getAlignment(), 4));
7499
Dan Gohman8181bd12008-07-27 21:46:04 +00007500 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007501 if (TokenFactorIndex != -1) {
7502 Ops.push_back(LoLd);
7503 Ops.push_back(HiLd);
7504 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7505 Ops.size());
7506 }
7507
7508 LoAddr = St->getBasePtr();
7509 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007510 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007511
Dan Gohman8181bd12008-07-27 21:46:04 +00007512 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007513 St->getSrcValue(), St->getSrcValueOffset(),
7514 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007515 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00007516 St->getSrcValue(),
7517 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00007518 St->isVolatile(),
7519 MinAlign(St->getAlignment(), 4));
7520 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00007521 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00007522 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007523 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00007524}
7525
Chris Lattner470d5dc2008-01-25 06:14:17 +00007526/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7527/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007528static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00007529 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7530 // F[X]OR(0.0, x) -> x
7531 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00007532 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7533 if (C->getValueAPF().isPosZero())
7534 return N->getOperand(1);
7535 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7536 if (C->getValueAPF().isPosZero())
7537 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007538 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007539}
7540
7541/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007542static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007543 // FAND(0.0, x) -> 0.0
7544 // FAND(x, 0.0) -> 0.0
7545 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7546 if (C->getValueAPF().isPosZero())
7547 return N->getOperand(0);
7548 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7549 if (C->getValueAPF().isPosZero())
7550 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007551 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007552}
7553
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007554
Dan Gohman8181bd12008-07-27 21:46:04 +00007555SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00007556 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007557 SelectionDAG &DAG = DCI.DAG;
7558 switch (N->getOpcode()) {
7559 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007560 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7561 case ISD::BUILD_VECTOR:
7562 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007563 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007564 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007565 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007566 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7567 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007568 }
7569
Dan Gohman8181bd12008-07-27 21:46:04 +00007570 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007571}
7572
7573//===----------------------------------------------------------------------===//
7574// X86 Inline Assembly Support
7575//===----------------------------------------------------------------------===//
7576
7577/// getConstraintType - Given a constraint letter, return the type of
7578/// constraint it is for this target.
7579X86TargetLowering::ConstraintType
7580X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7581 if (Constraint.size() == 1) {
7582 switch (Constraint[0]) {
7583 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +00007584 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +00007585 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007586 case 'r':
7587 case 'R':
7588 case 'l':
7589 case 'q':
7590 case 'Q':
7591 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007592 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007593 case 'Y':
7594 return C_RegisterClass;
7595 default:
7596 break;
7597 }
7598 }
7599 return TargetLowering::getConstraintType(Constraint);
7600}
7601
Dale Johannesene99fc902008-01-29 02:21:21 +00007602/// LowerXConstraint - try to replace an X constraint, which matches anything,
7603/// with another that has more specific requirements based on the type of the
7604/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007605const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007606LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007607 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7608 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007609 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007610 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007611 return "Y";
7612 if (Subtarget->hasSSE1())
7613 return "x";
7614 }
7615
7616 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007617}
7618
Chris Lattnera531abc2007-08-25 00:47:38 +00007619/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7620/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007621void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007622 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00007623 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00007624 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007625 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007626 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007627
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007628 switch (Constraint) {
7629 default: break;
7630 case 'I':
7631 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007632 if (C->getZExtValue() <= 31) {
7633 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007634 break;
7635 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007636 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007637 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00007638 case 'J':
7639 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
7640 if (C->getZExtValue() <= 63) {
7641 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
7642 break;
7643 }
7644 }
7645 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007646 case 'N':
7647 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007648 if (C->getZExtValue() <= 255) {
7649 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007650 break;
7651 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007652 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007653 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007654 case 'i': {
7655 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007656 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007657 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007658 break;
7659 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007660
7661 // If we are in non-pic codegen mode, we allow the address of a global (with
7662 // an optional displacement) to be used with 'i'.
7663 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7664 int64_t Offset = 0;
7665
7666 // Match either (GA) or (GA+C)
7667 if (GA) {
7668 Offset = GA->getOffset();
7669 } else if (Op.getOpcode() == ISD::ADD) {
7670 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7671 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7672 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007673 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007674 } else {
7675 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7676 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7677 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007678 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007679 else
7680 C = 0, GA = 0;
7681 }
7682 }
7683
7684 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00007685 if (hasMemory)
Dan Gohman36322c72008-10-18 02:06:02 +00007686 Op = LowerGlobalAddress(GA->getGlobal(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00007687 else
7688 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7689 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007690 Result = Op;
7691 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007692 }
7693
7694 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007695 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007696 }
7697 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007698
Gabor Greif1c80d112008-08-28 21:40:38 +00007699 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007700 Ops.push_back(Result);
7701 return;
7702 }
Evan Cheng7f250d62008-09-24 00:05:32 +00007703 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
7704 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007705}
7706
7707std::vector<unsigned> X86TargetLowering::
7708getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007709 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007710 if (Constraint.size() == 1) {
7711 // FIXME: not handling fp-stack yet!
7712 switch (Constraint[0]) { // GCC X86 Constraint Letters
7713 default: break; // Unknown constraint letter
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007714 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7715 case 'Q': // Q_REGS
7716 if (VT == MVT::i32)
7717 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7718 else if (VT == MVT::i16)
7719 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7720 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007721 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007722 else if (VT == MVT::i64)
7723 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7724 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007725 }
7726 }
7727
7728 return std::vector<unsigned>();
7729}
7730
7731std::pair<unsigned, const TargetRegisterClass*>
7732X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007733 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007734 // First, see if this is a constraint that directly corresponds to an LLVM
7735 // register class.
7736 if (Constraint.size() == 1) {
7737 // GCC Constraint Letters
7738 switch (Constraint[0]) {
7739 default: break;
7740 case 'r': // GENERAL_REGS
7741 case 'R': // LEGACY_REGS
7742 case 'l': // INDEX_REGS
Chris Lattnerbbfea052008-10-17 18:15:05 +00007743 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007744 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00007745 if (VT == MVT::i16)
7746 return std::make_pair(0U, X86::GR16RegisterClass);
7747 if (VT == MVT::i32 || !Subtarget->is64Bit())
7748 return std::make_pair(0U, X86::GR32RegisterClass);
7749 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00007750 case 'f': // FP Stack registers.
7751 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7752 // value to the correct fpstack register class.
7753 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7754 return std::make_pair(0U, X86::RFP32RegisterClass);
7755 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7756 return std::make_pair(0U, X86::RFP64RegisterClass);
7757 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007758 case 'y': // MMX_REGS if MMX allowed.
7759 if (!Subtarget->hasMMX()) break;
7760 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007761 case 'Y': // SSE_REGS if SSE2 allowed
7762 if (!Subtarget->hasSSE2()) break;
7763 // FALL THROUGH.
7764 case 'x': // SSE_REGS if SSE1 allowed
7765 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007766
7767 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007768 default: break;
7769 // Scalar SSE types.
7770 case MVT::f32:
7771 case MVT::i32:
7772 return std::make_pair(0U, X86::FR32RegisterClass);
7773 case MVT::f64:
7774 case MVT::i64:
7775 return std::make_pair(0U, X86::FR64RegisterClass);
7776 // Vector types.
7777 case MVT::v16i8:
7778 case MVT::v8i16:
7779 case MVT::v4i32:
7780 case MVT::v2i64:
7781 case MVT::v4f32:
7782 case MVT::v2f64:
7783 return std::make_pair(0U, X86::VR128RegisterClass);
7784 }
7785 break;
7786 }
7787 }
7788
7789 // Use the default implementation in TargetLowering to convert the register
7790 // constraint into a member of a register class.
7791 std::pair<unsigned, const TargetRegisterClass*> Res;
7792 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7793
7794 // Not found as a standard register?
7795 if (Res.second == 0) {
7796 // GCC calls "st(0)" just plain "st".
7797 if (StringsEqualNoCase("{st}", Constraint)) {
7798 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007799 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007800 }
Dale Johannesen73920c02008-11-13 21:52:36 +00007801 // 'A' means EAX + EDX.
7802 if (Constraint == "A") {
7803 Res.first = X86::EAX;
7804 Res.second = X86::GRADRegisterClass;
7805 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007806 return Res;
7807 }
7808
7809 // Otherwise, check to see if this is a register class of the wrong value
7810 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7811 // turn into {ax},{dx}.
7812 if (Res.second->hasType(VT))
7813 return Res; // Correct type already, nothing to do.
7814
7815 // All of the single-register GCC register classes map their values onto
7816 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7817 // really want an 8-bit or 32-bit register, map to the appropriate register
7818 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007819 if (Res.second == X86::GR16RegisterClass) {
7820 if (VT == MVT::i8) {
7821 unsigned DestReg = 0;
7822 switch (Res.first) {
7823 default: break;
7824 case X86::AX: DestReg = X86::AL; break;
7825 case X86::DX: DestReg = X86::DL; break;
7826 case X86::CX: DestReg = X86::CL; break;
7827 case X86::BX: DestReg = X86::BL; break;
7828 }
7829 if (DestReg) {
7830 Res.first = DestReg;
7831 Res.second = Res.second = X86::GR8RegisterClass;
7832 }
7833 } else if (VT == MVT::i32) {
7834 unsigned DestReg = 0;
7835 switch (Res.first) {
7836 default: break;
7837 case X86::AX: DestReg = X86::EAX; break;
7838 case X86::DX: DestReg = X86::EDX; break;
7839 case X86::CX: DestReg = X86::ECX; break;
7840 case X86::BX: DestReg = X86::EBX; break;
7841 case X86::SI: DestReg = X86::ESI; break;
7842 case X86::DI: DestReg = X86::EDI; break;
7843 case X86::BP: DestReg = X86::EBP; break;
7844 case X86::SP: DestReg = X86::ESP; break;
7845 }
7846 if (DestReg) {
7847 Res.first = DestReg;
7848 Res.second = Res.second = X86::GR32RegisterClass;
7849 }
7850 } else if (VT == MVT::i64) {
7851 unsigned DestReg = 0;
7852 switch (Res.first) {
7853 default: break;
7854 case X86::AX: DestReg = X86::RAX; break;
7855 case X86::DX: DestReg = X86::RDX; break;
7856 case X86::CX: DestReg = X86::RCX; break;
7857 case X86::BX: DestReg = X86::RBX; break;
7858 case X86::SI: DestReg = X86::RSI; break;
7859 case X86::DI: DestReg = X86::RDI; break;
7860 case X86::BP: DestReg = X86::RBP; break;
7861 case X86::SP: DestReg = X86::RSP; break;
7862 }
7863 if (DestReg) {
7864 Res.first = DestReg;
7865 Res.second = Res.second = X86::GR64RegisterClass;
7866 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007867 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007868 } else if (Res.second == X86::FR32RegisterClass ||
7869 Res.second == X86::FR64RegisterClass ||
7870 Res.second == X86::VR128RegisterClass) {
7871 // Handle references to XMM physical registers that got mapped into the
7872 // wrong class. This can happen with constraints like {xmm0} where the
7873 // target independent register mapper will just pick the first match it can
7874 // find, ignoring the required type.
7875 if (VT == MVT::f32)
7876 Res.second = X86::FR32RegisterClass;
7877 else if (VT == MVT::f64)
7878 Res.second = X86::FR64RegisterClass;
7879 else if (X86::VR128RegisterClass->hasType(VT))
7880 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007881 }
7882
7883 return Res;
7884}
Mon P Wang1448aad2008-10-30 08:01:45 +00007885
7886//===----------------------------------------------------------------------===//
7887// X86 Widen vector type
7888//===----------------------------------------------------------------------===//
7889
7890/// getWidenVectorType: given a vector type, returns the type to widen
7891/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
7892/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +00007893/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +00007894/// scalarizing vs using the wider vector type.
7895
7896MVT X86TargetLowering::getWidenVectorType(MVT VT) {
7897 assert(VT.isVector());
7898 if (isTypeLegal(VT))
7899 return VT;
7900
7901 // TODO: In computeRegisterProperty, we can compute the list of legal vector
7902 // type based on element type. This would speed up our search (though
7903 // it may not be worth it since the size of the list is relatively
7904 // small).
7905 MVT EltVT = VT.getVectorElementType();
7906 unsigned NElts = VT.getVectorNumElements();
7907
7908 // On X86, it make sense to widen any vector wider than 1
7909 if (NElts <= 1)
7910 return MVT::Other;
7911
7912 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
7913 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
7914 MVT SVT = (MVT::SimpleValueType)nVT;
7915
7916 if (isTypeLegal(SVT) &&
7917 SVT.getVectorElementType() == EltVT &&
7918 SVT.getVectorNumElements() > NElts)
7919 return SVT;
7920 }
7921 return MVT::Other;
7922}