blob: 9c5458d54ac75cc7997fe66cf47c6df8abab48a2 [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
783 // We have target-specific dag combine patterns for the following nodes:
784 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000785 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000787 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788
789 computeRegisterProperties();
790
791 // FIXME: These should be based on subtarget info. Plus, the values should
792 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000793 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
794 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
795 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000797 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798}
799
Scott Michel502151f2008-03-10 15:42:14 +0000800
Dan Gohman8181bd12008-07-27 21:46:04 +0000801MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000802 return MVT::i8;
803}
804
805
Evan Cheng5a67b812008-01-23 23:17:41 +0000806/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
807/// the desired ByVal argument alignment.
808static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
809 if (MaxAlign == 16)
810 return;
811 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
812 if (VTy->getBitWidth() == 128)
813 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000814 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
815 unsigned EltAlign = 0;
816 getMaxByValAlign(ATy->getElementType(), EltAlign);
817 if (EltAlign > MaxAlign)
818 MaxAlign = EltAlign;
819 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
820 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
821 unsigned EltAlign = 0;
822 getMaxByValAlign(STy->getElementType(i), EltAlign);
823 if (EltAlign > MaxAlign)
824 MaxAlign = EltAlign;
825 if (MaxAlign == 16)
826 break;
827 }
828 }
829 return;
830}
831
832/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
833/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000834/// that contain SSE vectors are placed at 16-byte boundaries while the rest
835/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000836unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000837 if (Subtarget->is64Bit()) {
838 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000839 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000840 if (TyAlign > 8)
841 return TyAlign;
842 return 8;
843 }
844
Evan Cheng5a67b812008-01-23 23:17:41 +0000845 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000846 if (Subtarget->hasSSE1())
847 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000848 return Align;
849}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850
Evan Cheng8c590372008-05-15 08:39:06 +0000851/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000852/// and store operations as a result of memset, memcpy, and memmove
853/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000854/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000855MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000856X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
857 bool isSrcConst, bool isSrcStr) const {
Chris Lattnerf0bf1062008-10-28 05:49:35 +0000858 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
859 // linux. This is because the stack realignment code can't handle certain
860 // cases like PR2962. This should be removed when PR2962 is fixed.
861 if (Subtarget->getStackAlignment() >= 16) {
862 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
863 return MVT::v4i32;
864 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
865 return MVT::v4f32;
866 }
Evan Cheng8c590372008-05-15 08:39:06 +0000867 if (Subtarget->is64Bit() && Size >= 8)
868 return MVT::i64;
869 return MVT::i32;
870}
871
872
Evan Cheng6fb06762007-11-09 01:32:10 +0000873/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
874/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000875SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000876 SelectionDAG &DAG) const {
877 if (usesGlobalOffsetTable())
878 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
879 if (!Subtarget->isPICStyleRIPRel())
880 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
881 return Table;
882}
883
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884//===----------------------------------------------------------------------===//
885// Return Value Calling Convention Implementation
886//===----------------------------------------------------------------------===//
887
888#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000889
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000891SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000892 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
893
894 SmallVector<CCValAssign, 16> RVLocs;
895 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
896 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
897 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000898 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000899
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000900 // If this is the first return lowered for this function, add the regs to the
901 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000902 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 for (unsigned i = 0; i != RVLocs.size(); ++i)
904 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000905 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000907 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000909 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000910 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000911 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000912 SDValue TailCall = Chain;
913 SDValue TargetAddress = TailCall.getOperand(1);
914 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000915 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000916 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000917 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000918 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000919 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
920 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000921 assert(StackAdjustment.getOpcode() == ISD::Constant &&
922 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000923
Dan Gohman8181bd12008-07-27 21:46:04 +0000924 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000925 Operands.push_back(Chain.getOperand(0));
926 Operands.push_back(TargetAddress);
927 Operands.push_back(StackAdjustment);
928 // Copy registers used by the call. Last operand is a flag so it is not
929 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000930 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000931 Operands.push_back(Chain.getOperand(i));
932 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000933 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
934 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000935 }
936
937 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000938 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000939
Dan Gohman8181bd12008-07-27 21:46:04 +0000940 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000941 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
942 // Operand #1 = Bytes To Pop
943 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
944
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000945 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000946 for (unsigned i = 0; i != RVLocs.size(); ++i) {
947 CCValAssign &VA = RVLocs[i];
948 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000949 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000950
Chris Lattnerb56cc342008-03-11 03:23:40 +0000951 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
952 // the RET instruction and handled by the FP Stackifier.
953 if (RVLocs[i].getLocReg() == X86::ST0 ||
954 RVLocs[i].getLocReg() == X86::ST1) {
955 // If this is a copy from an xmm register to ST(0), use an FPExtend to
956 // change the value to the FP stack register class.
957 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
958 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
959 RetOps.push_back(ValToCopy);
960 // Don't emit a copytoreg.
961 continue;
962 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000963
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000964 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000965 Flag = Chain.getValue(1);
966 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000967
968 // The x86-64 ABI for returning structs by value requires that we copy
969 // the sret argument into %rax for the return. We saved the argument into
970 // a virtual register in the entry block, so now we copy the value out
971 // and into %rax.
972 if (Subtarget->is64Bit() &&
973 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
974 MachineFunction &MF = DAG.getMachineFunction();
975 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
976 unsigned Reg = FuncInfo->getSRetReturnReg();
977 if (!Reg) {
978 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
979 FuncInfo->setSRetReturnReg(Reg);
980 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000981 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000982
983 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
984 Flag = Chain.getValue(1);
985 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986
Chris Lattnerb56cc342008-03-11 03:23:40 +0000987 RetOps[0] = Chain; // Update chain.
988
989 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +0000990 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +0000991 RetOps.push_back(Flag);
992
993 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994}
995
996
997/// LowerCallResult - Lower the result values of an ISD::CALL into the
998/// appropriate copies out of appropriate physical registers. This assumes that
999/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1000/// being lowered. The returns a SDNode with the same number of values as the
1001/// ISD::CALL.
1002SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +00001003LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004 unsigned CallingConv, SelectionDAG &DAG) {
1005
1006 // Assign locations to each value returned by this call.
1007 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +00001008 bool isVarArg = TheCall->isVarArg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
1010 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1011
Dan Gohman8181bd12008-07-27 21:46:04 +00001012 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013
1014 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001015 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00001016 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001017
1018 // If this is a call to a function that returns an fp value on the floating
1019 // point stack, but where we prefer to use the value in xmm registers, copy
1020 // 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 +00001021 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1022 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001023 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1024 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001027 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1028 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001029 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001030 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001031
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001032 if (CopyVT != RVLocs[i].getValVT()) {
1033 // Round the F80 the right size, which also moves to the appropriate xmm
1034 // register.
1035 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1036 // This truncation won't change the value.
1037 DAG.getIntPtrConstant(1));
1038 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001039
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001040 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001041 }
Duncan Sands698842f2008-07-02 17:40:58 +00001042
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001043 // Merge everything together with a MERGE_VALUES node.
1044 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +00001045 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
Gabor Greif1c80d112008-08-28 21:40:38 +00001046 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047}
1048
1049
1050//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001051// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001052//===----------------------------------------------------------------------===//
1053// StdCall calling convention seems to be standard for many Windows' API
1054// routines and around. It differs from C calling convention just a little:
1055// callee should clean up the stack, not caller. Symbols should be also
1056// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001057// For info on fast calling convention see Fast Calling Convention (tail call)
1058// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001059
1060/// AddLiveIn - This helper function adds the specified physical register to the
1061/// MachineFunction as a live in value. It also creates a corresponding virtual
1062/// register for it.
1063static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1064 const TargetRegisterClass *RC) {
1065 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001066 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1067 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068 return VReg;
1069}
1070
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001071/// CallIsStructReturn - Determines whether a CALL node uses struct return
1072/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001073static bool CallIsStructReturn(CallSDNode *TheCall) {
1074 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001075 if (!NumOps)
1076 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001077
Dan Gohman705e3f72008-09-13 01:54:27 +00001078 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001079}
1080
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001081/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1082/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001083static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001084 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001085 if (!NumArgs)
1086 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001087
1088 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001089}
1090
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001091/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1092/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001093/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001094bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001095 if (IsVarArg)
1096 return false;
1097
Dan Gohman705e3f72008-09-13 01:54:27 +00001098 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001099 default:
1100 return false;
1101 case CallingConv::X86_StdCall:
1102 return !Subtarget->is64Bit();
1103 case CallingConv::X86_FastCall:
1104 return !Subtarget->is64Bit();
1105 case CallingConv::Fast:
1106 return PerformTailCallOpt;
1107 }
1108}
1109
Dan Gohman705e3f72008-09-13 01:54:27 +00001110/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1111/// given CallingConvention value.
1112CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001113 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001114 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001115 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001116 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1117 return CC_X86_64_TailCall;
1118 else
1119 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001120 }
1121
Gordon Henriksen18ace102008-01-05 16:56:59 +00001122 if (CC == CallingConv::X86_FastCall)
1123 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001124 else if (CC == CallingConv::Fast)
1125 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001126 else
1127 return CC_X86_32_C;
1128}
1129
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001130/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1131/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001132NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001133X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001134 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001135 if (CC == CallingConv::X86_FastCall)
1136 return FastCall;
1137 else if (CC == CallingConv::X86_StdCall)
1138 return StdCall;
1139 return None;
1140}
1141
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001142
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001143/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1144/// in a register before calling.
1145bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1146 return !IsTailCall && !Is64Bit &&
1147 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1148 Subtarget->isPICStyleGOT();
1149}
1150
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001151/// CallRequiresFnAddressInReg - Check whether the call requires the function
1152/// address to be loaded in a register.
1153bool
1154X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1155 return !Is64Bit && IsTailCall &&
1156 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1157 Subtarget->isPICStyleGOT();
1158}
1159
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001160/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1161/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001162/// the specific parameter attribute. The copy will be passed as a byval
1163/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001164static SDValue
1165CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001166 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001167 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001168 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001169 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001170}
1171
Dan Gohman8181bd12008-07-27 21:46:04 +00001172SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001173 const CCValAssign &VA,
1174 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001175 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001176 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001177 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001178 ISD::ArgFlagsTy Flags =
1179 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001180 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001181 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001182
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001183 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1184 // changed with more analysis.
1185 // In case of tail call optimization mark all arguments mutable. Since they
1186 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001187 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001188 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001189 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001190 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001191 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001192 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001193 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001194}
1195
Dan Gohman8181bd12008-07-27 21:46:04 +00001196SDValue
1197X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001198 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001199 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1200
1201 const Function* Fn = MF.getFunction();
1202 if (Fn->hasExternalLinkage() &&
1203 Subtarget->isTargetCygMing() &&
1204 Fn->getName() == "main")
1205 FuncInfo->setForceFramePointer(true);
1206
1207 // Decorate the function name.
1208 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1209
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001210 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001211 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001212 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001213 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001214 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001215 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001216
1217 assert(!(isVarArg && CC == CallingConv::Fast) &&
1218 "Var args not supported with calling convention fastcc");
1219
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001220 // Assign locations to all of the incoming arguments.
1221 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001222 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001223 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001224
Dan Gohman8181bd12008-07-27 21:46:04 +00001225 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001226 unsigned LastVal = ~0U;
1227 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1228 CCValAssign &VA = ArgLocs[i];
1229 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1230 // places.
1231 assert(VA.getValNo() != LastVal &&
1232 "Don't support value assigned to multiple locs yet");
1233 LastVal = VA.getValNo();
1234
1235 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001236 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001237 TargetRegisterClass *RC;
1238 if (RegVT == MVT::i32)
1239 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001240 else if (Is64Bit && RegVT == MVT::i64)
1241 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001242 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001243 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001244 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001245 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001246 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001247 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001248 else if (RegVT.isVector()) {
1249 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001250 if (!Is64Bit)
1251 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1252 else {
1253 // Darwin calling convention passes MMX values in either GPRs or
1254 // XMMs in x86-64. Other targets pass them in memory.
1255 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1256 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1257 RegVT = MVT::v2i64;
1258 } else {
1259 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1260 RegVT = MVT::i64;
1261 }
1262 }
1263 } else {
1264 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001266
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001267 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001268 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269
1270 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1271 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1272 // right size.
1273 if (VA.getLocInfo() == CCValAssign::SExt)
1274 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1275 DAG.getValueType(VA.getValVT()));
1276 else if (VA.getLocInfo() == CCValAssign::ZExt)
1277 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1278 DAG.getValueType(VA.getValVT()));
1279
1280 if (VA.getLocInfo() != CCValAssign::Full)
1281 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1282
Gordon Henriksen18ace102008-01-05 16:56:59 +00001283 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001284 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001285 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001286 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1287 else if (RC == X86::VR128RegisterClass) {
1288 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1289 DAG.getConstant(0, MVT::i64));
1290 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1291 }
1292 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001293
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001294 ArgValues.push_back(ArgValue);
1295 } else {
1296 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001297 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 }
1299 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001300
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001301 // The x86-64 ABI for returning structs by value requires that we copy
1302 // the sret argument into %rax for the return. Save the argument into
1303 // a virtual register so that we can access it from the return points.
1304 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1305 MachineFunction &MF = DAG.getMachineFunction();
1306 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1307 unsigned Reg = FuncInfo->getSRetReturnReg();
1308 if (!Reg) {
1309 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1310 FuncInfo->setSRetReturnReg(Reg);
1311 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001312 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001313 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1314 }
1315
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001317 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001318 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001319 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320
1321 // If the function takes variable number of arguments, make a frame index for
1322 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001323 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001324 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1325 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1326 }
1327 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001328 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1329
1330 // FIXME: We should really autogenerate these arrays
1331 static const unsigned GPR64ArgRegsWin64[] = {
1332 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001333 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001334 static const unsigned XMMArgRegsWin64[] = {
1335 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1336 };
1337 static const unsigned GPR64ArgRegs64Bit[] = {
1338 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1339 };
1340 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001341 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1342 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1343 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001344 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1345
1346 if (IsWin64) {
1347 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1348 GPR64ArgRegs = GPR64ArgRegsWin64;
1349 XMMArgRegs = XMMArgRegsWin64;
1350 } else {
1351 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1352 GPR64ArgRegs = GPR64ArgRegs64Bit;
1353 XMMArgRegs = XMMArgRegs64Bit;
1354 }
1355 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1356 TotalNumIntRegs);
1357 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1358 TotalNumXMMRegs);
1359
Gordon Henriksen18ace102008-01-05 16:56:59 +00001360 // For X86-64, if there are vararg parameters that are passed via
1361 // registers, then we must store them to their spots on the stack so they
1362 // may be loaded by deferencing the result of va_next.
1363 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001364 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1365 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1366 TotalNumXMMRegs * 16, 16);
1367
Gordon Henriksen18ace102008-01-05 16:56:59 +00001368 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001369 SmallVector<SDValue, 8> MemOps;
1370 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1371 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001372 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001373 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001374 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1375 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001376 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1377 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001378 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001379 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001380 MemOps.push_back(Store);
1381 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001382 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001383 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001384
Gordon Henriksen18ace102008-01-05 16:56:59 +00001385 // Now store the XMM (fp + vector) parameter registers.
1386 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001387 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001388 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001389 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1390 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001391 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1392 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001393 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001394 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001395 MemOps.push_back(Store);
1396 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001397 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001398 }
1399 if (!MemOps.empty())
1400 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1401 &MemOps[0], MemOps.size());
1402 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001403 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001404
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001405 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001406
Gordon Henriksen18ace102008-01-05 16:56:59 +00001407 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001408 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001409 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001410 BytesCallerReserves = 0;
1411 } else {
1412 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001414 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001415 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001416 BytesCallerReserves = StackSize;
1417 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001418
Gordon Henriksen18ace102008-01-05 16:56:59 +00001419 if (!Is64Bit) {
1420 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1421 if (CC == CallingConv::X86_FastCall)
1422 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1423 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001424
Anton Korobeynikove844e472007-08-15 17:12:32 +00001425 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001426
1427 // Return the new list of results.
Gabor Greif1c80d112008-08-28 21:40:38 +00001428 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif46bf5472008-08-26 22:36:50 +00001429 ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001430}
1431
Dan Gohman8181bd12008-07-27 21:46:04 +00001432SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001433X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001434 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001435 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001436 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001437 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001438 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001439 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001440 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001441 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001442 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001443 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001444 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001445 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001446}
1447
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001448/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1449/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001450SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001451X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001452 SDValue &OutRetAddr,
1453 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001454 bool IsTailCall,
1455 bool Is64Bit,
1456 int FPDiff) {
1457 if (!IsTailCall || FPDiff==0) return Chain;
1458
1459 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001460 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001461 OutRetAddr = getReturnAddressFrameIndex(DAG);
1462 // Load the "old" Return address.
1463 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001464 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001465}
1466
1467/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1468/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001469static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001470EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001471 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001472 bool Is64Bit, int FPDiff) {
1473 // Store the return address to the appropriate stack slot.
1474 if (!FPDiff) return Chain;
1475 // Calculate the new stack slot for the return address.
1476 int SlotSize = Is64Bit ? 8 : 4;
1477 int NewReturnAddrFI =
1478 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001479 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001480 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001481 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001482 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001483 return Chain;
1484}
1485
Dan Gohman8181bd12008-07-27 21:46:04 +00001486SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001487 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001488 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1489 SDValue Chain = TheCall->getChain();
1490 unsigned CC = TheCall->getCallingConv();
1491 bool isVarArg = TheCall->isVarArg();
1492 bool IsTailCall = TheCall->isTailCall() &&
1493 CC == CallingConv::Fast && PerformTailCallOpt;
1494 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001495 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001496 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001497
1498 assert(!(isVarArg && CC == CallingConv::Fast) &&
1499 "Var args not supported with calling convention fastcc");
1500
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501 // Analyze operands of the call, assigning locations to each operand.
1502 SmallVector<CCValAssign, 16> ArgLocs;
1503 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001504 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001505
1506 // Get a count of how many bytes are to be pushed on the stack.
1507 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001508 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001509 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001510
Gordon Henriksen18ace102008-01-05 16:56:59 +00001511 int FPDiff = 0;
1512 if (IsTailCall) {
1513 // Lower arguments at fp - stackoffset + fpdiff.
1514 unsigned NumBytesCallerPushed =
1515 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1516 FPDiff = NumBytesCallerPushed - NumBytes;
1517
1518 // Set the delta of movement of the returnaddr stackslot.
1519 // But only set if delta is greater than previous delta.
1520 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1521 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1522 }
1523
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001524 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001525
Dan Gohman8181bd12008-07-27 21:46:04 +00001526 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001527 // Load return adress for tail calls.
1528 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1529 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001530
Dan Gohman8181bd12008-07-27 21:46:04 +00001531 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1532 SmallVector<SDValue, 8> MemOpChains;
1533 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001534
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001535 // Walk the register/memloc assignments, inserting copies/loads. In the case
1536 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001537 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1538 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001539 SDValue Arg = TheCall->getArg(i);
1540 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1541 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001542
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001543 // Promote the value if needed.
1544 switch (VA.getLocInfo()) {
1545 default: assert(0 && "Unknown loc info!");
1546 case CCValAssign::Full: break;
1547 case CCValAssign::SExt:
1548 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1549 break;
1550 case CCValAssign::ZExt:
1551 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1552 break;
1553 case CCValAssign::AExt:
1554 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1555 break;
1556 }
1557
1558 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001559 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001560 MVT RegVT = VA.getLocVT();
1561 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001562 switch (VA.getLocReg()) {
1563 default:
1564 break;
1565 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1566 case X86::R8: {
1567 // Special case: passing MMX values in GPR registers.
1568 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1569 break;
1570 }
1571 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1572 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1573 // Special case: passing MMX values in XMM registers.
1574 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1575 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1576 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1577 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1578 getMOVLMask(2, DAG));
1579 break;
1580 }
1581 }
1582 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001583 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1584 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001585 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001586 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001587 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001588 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1589
Dan Gohman705e3f72008-09-13 01:54:27 +00001590 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1591 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001592 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001593 }
1594 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001595
1596 if (!MemOpChains.empty())
1597 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1598 &MemOpChains[0], MemOpChains.size());
1599
1600 // Build a sequence of copy-to-reg nodes chained together with token chain
1601 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001602 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001603 // Tail call byval lowering might overwrite argument registers so in case of
1604 // tail call optimization the copies to registers are lowered later.
1605 if (!IsTailCall)
1606 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1607 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1608 InFlag);
1609 InFlag = Chain.getValue(1);
1610 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001611
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001612 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001613 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001614 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1615 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1616 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1617 InFlag);
1618 InFlag = Chain.getValue(1);
1619 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001620 // If we are tail calling and generating PIC/GOT style code load the address
1621 // of the callee into ecx. The value in ecx is used as target of the tail
1622 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1623 // calls on PIC/GOT architectures. Normally we would just put the address of
1624 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1625 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001626 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001627 // Note: The actual moving to ecx is done further down.
1628 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001629 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001630 !G->getGlobal()->hasProtectedVisibility())
1631 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001632 else if (isa<ExternalSymbolSDNode>(Callee))
1633 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001634 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001635
Gordon Henriksen18ace102008-01-05 16:56:59 +00001636 if (Is64Bit && isVarArg) {
1637 // From AMD64 ABI document:
1638 // For calls that may call functions that use varargs or stdargs
1639 // (prototype-less calls or calls to functions containing ellipsis (...) in
1640 // the declaration) %al is used as hidden argument to specify the number
1641 // of SSE registers used. The contents of %al do not need to match exactly
1642 // the number of registers, but must be an ubound on the number of SSE
1643 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001644
1645 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001646 // Count the number of XMM registers allocated.
1647 static const unsigned XMMArgRegs[] = {
1648 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1649 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1650 };
1651 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1652
1653 Chain = DAG.getCopyToReg(Chain, X86::AL,
1654 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1655 InFlag = Chain.getValue(1);
1656 }
1657
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001658
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001659 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001660 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001661 SmallVector<SDValue, 8> MemOpChains2;
1662 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001663 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001664 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001665 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001666 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1667 CCValAssign &VA = ArgLocs[i];
1668 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001669 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001670 SDValue Arg = TheCall->getArg(i);
1671 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001672 // Create frame index.
1673 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001674 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001675 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001676 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001677
Duncan Sandsc93fae32008-03-21 09:14:45 +00001678 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001679 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001680 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001681 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001682 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1683 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1684
1685 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001686 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001687 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001688 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001689 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001690 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001691 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001692 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001693 }
1694 }
1695
1696 if (!MemOpChains2.empty())
1697 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001698 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001699
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001700 // Copy arguments to their registers.
1701 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1702 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1703 InFlag);
1704 InFlag = Chain.getValue(1);
1705 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001706 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001707
Gordon Henriksen18ace102008-01-05 16:56:59 +00001708 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001709 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1710 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001711 }
1712
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001713 // If the callee is a GlobalAddress node (quite common, every direct call is)
1714 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1715 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1716 // We should use extra load for direct calls to dllimported functions in
1717 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001718 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1719 getTargetMachine(), true))
Dan Gohman36322c72008-10-18 02:06:02 +00001720 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1721 G->getOffset());
Bill Wendlingfef06052008-09-16 21:48:12 +00001722 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1723 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001724 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001725 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001726
1727 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001728 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001729 Callee,InFlag);
1730 Callee = DAG.getRegister(Opc, getPointerTy());
1731 // Add register as live out.
1732 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001733 }
1734
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001735 // Returns a chain & a flag for retval copy to use.
1736 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001737 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001738
1739 if (IsTailCall) {
1740 Ops.push_back(Chain);
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001741 Ops.push_back(DAG.getIntPtrConstant(NumBytes, true));
1742 Ops.push_back(DAG.getIntPtrConstant(0, true));
Gabor Greif1c80d112008-08-28 21:40:38 +00001743 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001744 Ops.push_back(InFlag);
1745 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1746 InFlag = Chain.getValue(1);
1747
1748 // Returns a chain & a flag for retval copy to use.
1749 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1750 Ops.clear();
1751 }
1752
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001753 Ops.push_back(Chain);
1754 Ops.push_back(Callee);
1755
Gordon Henriksen18ace102008-01-05 16:56:59 +00001756 if (IsTailCall)
1757 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001758
Gordon Henriksen18ace102008-01-05 16:56:59 +00001759 // Add argument registers to the end of the list so that they are known live
1760 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001761 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1762 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1763 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001764
Evan Cheng8ba45e62008-03-18 23:36:35 +00001765 // Add an implicit use GOT pointer in EBX.
1766 if (!IsTailCall && !Is64Bit &&
1767 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1768 Subtarget->isPICStyleGOT())
1769 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1770
1771 // Add an implicit use of AL for x86 vararg functions.
1772 if (Is64Bit && isVarArg)
1773 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1774
Gabor Greif1c80d112008-08-28 21:40:38 +00001775 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001776 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001777
Gordon Henriksen18ace102008-01-05 16:56:59 +00001778 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001779 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001780 "Flag must be set. Depend on flag being set in LowerRET");
1781 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman705e3f72008-09-13 01:54:27 +00001782 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001783
Gabor Greif1c80d112008-08-28 21:40:38 +00001784 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001785 }
1786
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001787 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001788 InFlag = Chain.getValue(1);
1789
1790 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001791 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001792 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001793 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001794 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001795 // If this is is a call to a struct-return function, the callee
1796 // pops the hidden struct pointer, so we have to push it back.
1797 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001798 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001799 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001800 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001801
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001802 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001803 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001804 DAG.getIntPtrConstant(NumBytes, true),
1805 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1806 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001807 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001808 InFlag = Chain.getValue(1);
1809
1810 // Handle result values, copying them out of physregs into vregs that we
1811 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001812 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001813 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001814}
1815
1816
1817//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001818// Fast Calling Convention (tail call) implementation
1819//===----------------------------------------------------------------------===//
1820
1821// Like std call, callee cleans arguments, convention except that ECX is
1822// reserved for storing the tail called function address. Only 2 registers are
1823// free for argument passing (inreg). Tail call optimization is performed
1824// provided:
1825// * tailcallopt is enabled
1826// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001827// On X86_64 architecture with GOT-style position independent code only local
1828// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001829// To keep the stack aligned according to platform abi the function
1830// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1831// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001832// If a tail called function callee has more arguments than the caller the
1833// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001834// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001835// original REtADDR, but before the saved framepointer or the spilled registers
1836// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1837// stack layout:
1838// arg1
1839// arg2
1840// RETADDR
1841// [ new RETADDR
1842// move area ]
1843// (possible EBP)
1844// ESI
1845// EDI
1846// local1 ..
1847
1848/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1849/// for a 16 byte align requirement.
1850unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1851 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001852 MachineFunction &MF = DAG.getMachineFunction();
1853 const TargetMachine &TM = MF.getTarget();
1854 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1855 unsigned StackAlignment = TFI.getStackAlignment();
1856 uint64_t AlignMask = StackAlignment - 1;
1857 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001858 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001859 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1860 // Number smaller than 12 so just add the difference.
1861 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1862 } else {
1863 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1864 Offset = ((~AlignMask) & Offset) + StackAlignment +
1865 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001866 }
Evan Chengded8f902008-09-07 09:07:23 +00001867 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001868}
1869
1870/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001871/// following the call is a return. A function is eligible if caller/callee
1872/// calling conventions match, currently only fastcc supports tail calls, and
1873/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001874bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001875 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001876 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001877 if (!PerformTailCallOpt)
1878 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001879
Dan Gohman705e3f72008-09-13 01:54:27 +00001880 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001881 MachineFunction &MF = DAG.getMachineFunction();
1882 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001883 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001884 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001885 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001886 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001887 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001888 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001889 return true;
1890
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001891 // Can only do local tail calls (in same module, hidden or protected) on
1892 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001893 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1894 return G->getGlobal()->hasHiddenVisibility()
1895 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001896 }
1897 }
Evan Chenge7a87392007-11-02 01:26:22 +00001898
1899 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001900}
1901
Dan Gohmanca4857a2008-09-03 23:12:08 +00001902FastISel *
1903X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001904 MachineModuleInfo *mmo,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001905 DenseMap<const Value *, unsigned> &vm,
1906 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001907 MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00001908 DenseMap<const AllocaInst *, int> &am
1909#ifndef NDEBUG
1910 , SmallSet<Instruction*, 8> &cil
1911#endif
1912 ) {
1913 return X86::createFastISel(mf, mmo, vm, bm, am
1914#ifndef NDEBUG
1915 , cil
1916#endif
1917 );
Dan Gohman97805ee2008-08-19 21:32:53 +00001918}
1919
1920
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001921//===----------------------------------------------------------------------===//
1922// Other Lowering Hooks
1923//===----------------------------------------------------------------------===//
1924
1925
Dan Gohman8181bd12008-07-27 21:46:04 +00001926SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001927 MachineFunction &MF = DAG.getMachineFunction();
1928 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1929 int ReturnAddrIndex = FuncInfo->getRAIndex();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001930 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikove844e472007-08-15 17:12:32 +00001931
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001932 if (ReturnAddrIndex == 0) {
1933 // Set up a frame object for the return address.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001934 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001935 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001936 }
1937
1938 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1939}
1940
1941
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001942/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1943/// specific condition code. It returns a false if it cannot do a direct
1944/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1945/// needed.
1946static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001947 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001948 SelectionDAG &DAG) {
1949 X86CC = X86::COND_INVALID;
1950 if (!isFP) {
1951 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1952 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1953 // X > -1 -> X == 0, jump !sign.
1954 RHS = DAG.getConstant(0, RHS.getValueType());
1955 X86CC = X86::COND_NS;
1956 return true;
1957 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1958 // X < 0 -> X == 0, jump on sign.
1959 X86CC = X86::COND_S;
1960 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001961 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00001962 // X < 1 -> X <= 0
1963 RHS = DAG.getConstant(0, RHS.getValueType());
1964 X86CC = X86::COND_LE;
1965 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001966 }
1967 }
1968
1969 switch (SetCCOpcode) {
1970 default: break;
1971 case ISD::SETEQ: X86CC = X86::COND_E; break;
1972 case ISD::SETGT: X86CC = X86::COND_G; break;
1973 case ISD::SETGE: X86CC = X86::COND_GE; break;
1974 case ISD::SETLT: X86CC = X86::COND_L; break;
1975 case ISD::SETLE: X86CC = X86::COND_LE; break;
1976 case ISD::SETNE: X86CC = X86::COND_NE; break;
1977 case ISD::SETULT: X86CC = X86::COND_B; break;
1978 case ISD::SETUGT: X86CC = X86::COND_A; break;
1979 case ISD::SETULE: X86CC = X86::COND_BE; break;
1980 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1981 }
1982 } else {
Duncan Sandsc2a04622008-10-24 13:03:10 +00001983 // First determine if it is required or is profitable to flip the operands.
1984
1985 // If LHS is a foldable load, but RHS is not, flip the condition.
1986 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1987 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1988 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1989 std::swap(LHS, RHS);
1990 }
1991
Evan Chengb488ca32008-08-29 23:22:12 +00001992 switch (SetCCOpcode) {
1993 default: break;
1994 case ISD::SETOLT:
1995 case ISD::SETOLE:
1996 case ISD::SETUGT:
1997 case ISD::SETUGE:
Duncan Sandsc2a04622008-10-24 13:03:10 +00001998 std::swap(LHS, RHS);
Evan Chengb488ca32008-08-29 23:22:12 +00001999 break;
2000 }
2001
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002002 // On a floating point condition, the flags are set as follows:
2003 // ZF PF CF op
2004 // 0 | 0 | 0 | X > Y
2005 // 0 | 0 | 1 | X < Y
2006 // 1 | 0 | 0 | X == Y
2007 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002008 switch (SetCCOpcode) {
2009 default: break;
2010 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00002011 case ISD::SETEQ:
2012 X86CC = X86::COND_E;
2013 break;
2014 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002015 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00002016 case ISD::SETGT:
2017 X86CC = X86::COND_A;
2018 break;
2019 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002020 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00002021 case ISD::SETGE:
2022 X86CC = X86::COND_AE;
2023 break;
2024 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002025 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00002026 case ISD::SETLT:
2027 X86CC = X86::COND_B;
2028 break;
2029 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002030 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00002031 case ISD::SETLE:
2032 X86CC = X86::COND_BE;
2033 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002034 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00002035 case ISD::SETNE:
2036 X86CC = X86::COND_NE;
2037 break;
2038 case ISD::SETUO:
2039 X86CC = X86::COND_P;
2040 break;
2041 case ISD::SETO:
2042 X86CC = X86::COND_NP;
2043 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002044 }
Evan Chengfc937c92008-08-28 23:48:31 +00002045 }
2046
Evan Chengc6162692008-08-29 22:13:21 +00002047 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002048}
2049
2050/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2051/// code. Current x86 isa includes the following FP cmov instructions:
2052/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2053static bool hasFPCMov(unsigned X86CC) {
2054 switch (X86CC) {
2055 default:
2056 return false;
2057 case X86::COND_B:
2058 case X86::COND_BE:
2059 case X86::COND_E:
2060 case X86::COND_P:
2061 case X86::COND_A:
2062 case X86::COND_AE:
2063 case X86::COND_NE:
2064 case X86::COND_NP:
2065 return true;
2066 }
2067}
2068
2069/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2070/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002071static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002072 if (Op.getOpcode() == ISD::UNDEF)
2073 return true;
2074
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002075 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002076 return (Val >= Low && Val < Hi);
2077}
2078
2079/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2080/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002081static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002082 if (Op.getOpcode() == ISD::UNDEF)
2083 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002084 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002085}
2086
2087/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2088/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2089bool X86::isPSHUFDMask(SDNode *N) {
2090 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2091
Dan Gohman7dc19012007-08-02 21:17:01 +00002092 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002093 return false;
2094
2095 // Check if the value doesn't reference the second vector.
2096 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002097 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002098 if (Arg.getOpcode() == ISD::UNDEF) continue;
2099 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002100 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002101 return false;
2102 }
2103
2104 return true;
2105}
2106
2107/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2108/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2109bool X86::isPSHUFHWMask(SDNode *N) {
2110 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2111
2112 if (N->getNumOperands() != 8)
2113 return false;
2114
2115 // Lower quadword copied in order.
2116 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002117 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002118 if (Arg.getOpcode() == ISD::UNDEF) continue;
2119 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002120 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002121 return false;
2122 }
2123
2124 // Upper quadword shuffled.
2125 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002126 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002127 if (Arg.getOpcode() == ISD::UNDEF) continue;
2128 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002129 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002130 if (Val < 4 || Val > 7)
2131 return false;
2132 }
2133
2134 return true;
2135}
2136
2137/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2138/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2139bool X86::isPSHUFLWMask(SDNode *N) {
2140 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2141
2142 if (N->getNumOperands() != 8)
2143 return false;
2144
2145 // Upper quadword copied in order.
2146 for (unsigned i = 4; i != 8; ++i)
2147 if (!isUndefOrEqual(N->getOperand(i), i))
2148 return false;
2149
2150 // Lower quadword shuffled.
2151 for (unsigned i = 0; i != 4; ++i)
2152 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2153 return false;
2154
2155 return true;
2156}
2157
2158/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2159/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002160static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002161 if (NumElems != 2 && NumElems != 4) return false;
2162
2163 unsigned Half = NumElems / 2;
2164 for (unsigned i = 0; i < Half; ++i)
2165 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2166 return false;
2167 for (unsigned i = Half; i < NumElems; ++i)
2168 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2169 return false;
2170
2171 return true;
2172}
2173
2174bool X86::isSHUFPMask(SDNode *N) {
2175 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2176 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2177}
2178
2179/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2180/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2181/// half elements to come from vector 1 (which would equal the dest.) and
2182/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002183static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002184 if (NumOps != 2 && NumOps != 4) return false;
2185
2186 unsigned Half = NumOps / 2;
2187 for (unsigned i = 0; i < Half; ++i)
2188 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2189 return false;
2190 for (unsigned i = Half; i < NumOps; ++i)
2191 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2192 return false;
2193 return true;
2194}
2195
2196static bool isCommutedSHUFP(SDNode *N) {
2197 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2198 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2199}
2200
2201/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2202/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2203bool X86::isMOVHLPSMask(SDNode *N) {
2204 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2205
2206 if (N->getNumOperands() != 4)
2207 return false;
2208
2209 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2210 return isUndefOrEqual(N->getOperand(0), 6) &&
2211 isUndefOrEqual(N->getOperand(1), 7) &&
2212 isUndefOrEqual(N->getOperand(2), 2) &&
2213 isUndefOrEqual(N->getOperand(3), 3);
2214}
2215
2216/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2217/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2218/// <2, 3, 2, 3>
2219bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2220 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2221
2222 if (N->getNumOperands() != 4)
2223 return false;
2224
2225 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2226 return isUndefOrEqual(N->getOperand(0), 2) &&
2227 isUndefOrEqual(N->getOperand(1), 3) &&
2228 isUndefOrEqual(N->getOperand(2), 2) &&
2229 isUndefOrEqual(N->getOperand(3), 3);
2230}
2231
2232/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2233/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2234bool X86::isMOVLPMask(SDNode *N) {
2235 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2236
2237 unsigned NumElems = N->getNumOperands();
2238 if (NumElems != 2 && NumElems != 4)
2239 return false;
2240
2241 for (unsigned i = 0; i < NumElems/2; ++i)
2242 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2243 return false;
2244
2245 for (unsigned i = NumElems/2; i < NumElems; ++i)
2246 if (!isUndefOrEqual(N->getOperand(i), i))
2247 return false;
2248
2249 return true;
2250}
2251
2252/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2253/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2254/// and MOVLHPS.
2255bool X86::isMOVHPMask(SDNode *N) {
2256 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2257
2258 unsigned NumElems = N->getNumOperands();
2259 if (NumElems != 2 && NumElems != 4)
2260 return false;
2261
2262 for (unsigned i = 0; i < NumElems/2; ++i)
2263 if (!isUndefOrEqual(N->getOperand(i), i))
2264 return false;
2265
2266 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002267 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002268 if (!isUndefOrEqual(Arg, i + NumElems))
2269 return false;
2270 }
2271
2272 return true;
2273}
2274
2275/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2276/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002277bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002278 bool V2IsSplat = false) {
2279 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2280 return false;
2281
2282 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002283 SDValue BitI = Elts[i];
2284 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002285 if (!isUndefOrEqual(BitI, j))
2286 return false;
2287 if (V2IsSplat) {
2288 if (isUndefOrEqual(BitI1, NumElts))
2289 return false;
2290 } else {
2291 if (!isUndefOrEqual(BitI1, j + NumElts))
2292 return false;
2293 }
2294 }
2295
2296 return true;
2297}
2298
2299bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2300 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2301 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2302}
2303
2304/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2305/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002306bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002307 bool V2IsSplat = false) {
2308 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2309 return false;
2310
2311 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002312 SDValue BitI = Elts[i];
2313 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002314 if (!isUndefOrEqual(BitI, j + NumElts/2))
2315 return false;
2316 if (V2IsSplat) {
2317 if (isUndefOrEqual(BitI1, NumElts))
2318 return false;
2319 } else {
2320 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2321 return false;
2322 }
2323 }
2324
2325 return true;
2326}
2327
2328bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2329 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2330 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2331}
2332
2333/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2334/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2335/// <0, 0, 1, 1>
2336bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2337 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2338
2339 unsigned NumElems = N->getNumOperands();
2340 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2341 return false;
2342
2343 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002344 SDValue BitI = N->getOperand(i);
2345 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002346
2347 if (!isUndefOrEqual(BitI, j))
2348 return false;
2349 if (!isUndefOrEqual(BitI1, j))
2350 return false;
2351 }
2352
2353 return true;
2354}
2355
2356/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2357/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2358/// <2, 2, 3, 3>
2359bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2360 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2361
2362 unsigned NumElems = N->getNumOperands();
2363 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2364 return false;
2365
2366 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002367 SDValue BitI = N->getOperand(i);
2368 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002369
2370 if (!isUndefOrEqual(BitI, j))
2371 return false;
2372 if (!isUndefOrEqual(BitI1, j))
2373 return false;
2374 }
2375
2376 return true;
2377}
2378
2379/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2380/// specifies a shuffle of elements that is suitable for input to MOVSS,
2381/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002382static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002383 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002384 return false;
2385
2386 if (!isUndefOrEqual(Elts[0], NumElts))
2387 return false;
2388
2389 for (unsigned i = 1; i < NumElts; ++i) {
2390 if (!isUndefOrEqual(Elts[i], i))
2391 return false;
2392 }
2393
2394 return true;
2395}
2396
2397bool X86::isMOVLMask(SDNode *N) {
2398 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2399 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2400}
2401
2402/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2403/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2404/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002405static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002406 bool V2IsSplat = false,
2407 bool V2IsUndef = false) {
2408 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2409 return false;
2410
2411 if (!isUndefOrEqual(Ops[0], 0))
2412 return false;
2413
2414 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002415 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2417 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2418 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2419 return false;
2420 }
2421
2422 return true;
2423}
2424
2425static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2426 bool V2IsUndef = false) {
2427 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2428 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2429 V2IsSplat, V2IsUndef);
2430}
2431
2432/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2433/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2434bool X86::isMOVSHDUPMask(SDNode *N) {
2435 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2436
2437 if (N->getNumOperands() != 4)
2438 return false;
2439
2440 // Expect 1, 1, 3, 3
2441 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002442 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002443 if (Arg.getOpcode() == ISD::UNDEF) continue;
2444 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002445 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002446 if (Val != 1) return false;
2447 }
2448
2449 bool HasHi = false;
2450 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002451 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002452 if (Arg.getOpcode() == ISD::UNDEF) continue;
2453 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002454 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002455 if (Val != 3) return false;
2456 HasHi = true;
2457 }
2458
2459 // Don't use movshdup if it can be done with a shufps.
2460 return HasHi;
2461}
2462
2463/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2464/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2465bool X86::isMOVSLDUPMask(SDNode *N) {
2466 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2467
2468 if (N->getNumOperands() != 4)
2469 return false;
2470
2471 // Expect 0, 0, 2, 2
2472 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002473 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002474 if (Arg.getOpcode() == ISD::UNDEF) continue;
2475 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002476 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002477 if (Val != 0) return false;
2478 }
2479
2480 bool HasHi = false;
2481 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002482 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002483 if (Arg.getOpcode() == ISD::UNDEF) continue;
2484 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002485 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002486 if (Val != 2) return false;
2487 HasHi = true;
2488 }
2489
2490 // Don't use movshdup if it can be done with a shufps.
2491 return HasHi;
2492}
2493
2494/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2495/// specifies a identity operation on the LHS or RHS.
2496static bool isIdentityMask(SDNode *N, bool RHS = false) {
2497 unsigned NumElems = N->getNumOperands();
2498 for (unsigned i = 0; i < NumElems; ++i)
2499 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2500 return false;
2501 return true;
2502}
2503
2504/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2505/// a splat of a single element.
2506static bool isSplatMask(SDNode *N) {
2507 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2508
2509 // This is a splat operation if each element of the permute is the same, and
2510 // if the value doesn't reference the second vector.
2511 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002512 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002513 unsigned i = 0;
2514 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002515 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002516 if (isa<ConstantSDNode>(Elt)) {
2517 ElementBase = Elt;
2518 break;
2519 }
2520 }
2521
Gabor Greif1c80d112008-08-28 21:40:38 +00002522 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002523 return false;
2524
2525 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002526 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002527 if (Arg.getOpcode() == ISD::UNDEF) continue;
2528 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2529 if (Arg != ElementBase) return false;
2530 }
2531
2532 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002533 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002534}
2535
2536/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2537/// a splat of a single element and it's a 2 or 4 element mask.
2538bool X86::isSplatMask(SDNode *N) {
2539 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2540
2541 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2542 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2543 return false;
2544 return ::isSplatMask(N);
2545}
2546
2547/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2548/// specifies a splat of zero element.
2549bool X86::isSplatLoMask(SDNode *N) {
2550 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2551
2552 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2553 if (!isUndefOrEqual(N->getOperand(i), 0))
2554 return false;
2555 return true;
2556}
2557
Evan Chenga2497eb2008-09-25 20:50:48 +00002558/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2559/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2560bool X86::isMOVDDUPMask(SDNode *N) {
2561 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2562
2563 unsigned e = N->getNumOperands() / 2;
2564 for (unsigned i = 0; i < e; ++i)
2565 if (!isUndefOrEqual(N->getOperand(i), i))
2566 return false;
2567 for (unsigned i = 0; i < e; ++i)
2568 if (!isUndefOrEqual(N->getOperand(e+i), i))
2569 return false;
2570 return true;
2571}
2572
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002573/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2574/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2575/// instructions.
2576unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2577 unsigned NumOperands = N->getNumOperands();
2578 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2579 unsigned Mask = 0;
2580 for (unsigned i = 0; i < NumOperands; ++i) {
2581 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002582 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002583 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002584 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002585 if (Val >= NumOperands) Val -= NumOperands;
2586 Mask |= Val;
2587 if (i != NumOperands - 1)
2588 Mask <<= Shift;
2589 }
2590
2591 return Mask;
2592}
2593
2594/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2595/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2596/// instructions.
2597unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2598 unsigned Mask = 0;
2599 // 8 nodes, but we only care about the last 4.
2600 for (unsigned i = 7; i >= 4; --i) {
2601 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002602 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002603 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002604 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002605 Mask |= (Val - 4);
2606 if (i != 4)
2607 Mask <<= 2;
2608 }
2609
2610 return Mask;
2611}
2612
2613/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2614/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2615/// instructions.
2616unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2617 unsigned Mask = 0;
2618 // 8 nodes, but we only care about the first 4.
2619 for (int i = 3; i >= 0; --i) {
2620 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002621 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002622 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002623 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002624 Mask |= Val;
2625 if (i != 0)
2626 Mask <<= 2;
2627 }
2628
2629 return Mask;
2630}
2631
2632/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2633/// specifies a 8 element shuffle that can be broken into a pair of
2634/// PSHUFHW and PSHUFLW.
2635static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2636 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2637
2638 if (N->getNumOperands() != 8)
2639 return false;
2640
2641 // Lower quadword shuffled.
2642 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002643 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002644 if (Arg.getOpcode() == ISD::UNDEF) continue;
2645 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002646 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002647 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002648 return false;
2649 }
2650
2651 // Upper quadword shuffled.
2652 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002653 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002654 if (Arg.getOpcode() == ISD::UNDEF) continue;
2655 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002656 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002657 if (Val < 4 || Val > 7)
2658 return false;
2659 }
2660
2661 return true;
2662}
2663
Chris Lattnere6aa3862007-11-25 00:24:49 +00002664/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002666static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2667 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002668 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002669 MVT VT = Op.getValueType();
2670 MVT MaskVT = Mask.getValueType();
2671 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002672 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002673 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002674
2675 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002676 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002677 if (Arg.getOpcode() == ISD::UNDEF) {
2678 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2679 continue;
2680 }
2681 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002682 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002683 if (Val < NumElems)
2684 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2685 else
2686 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2687 }
2688
2689 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002690 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002691 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2692}
2693
Evan Chenga6769df2007-12-07 21:30:01 +00002694/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2695/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002696static
Dan Gohman8181bd12008-07-27 21:46:04 +00002697SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002698 MVT MaskVT = Mask.getValueType();
2699 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002700 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002701 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002702 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002703 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002704 if (Arg.getOpcode() == ISD::UNDEF) {
2705 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2706 continue;
2707 }
2708 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002709 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002710 if (Val < NumElems)
2711 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2712 else
2713 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2714 }
2715 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2716}
2717
2718
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002719/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2720/// match movhlps. The lower half elements should come from upper half of
2721/// V1 (and in order), and the upper half elements should come from the upper
2722/// half of V2 (and in order).
2723static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2724 unsigned NumElems = Mask->getNumOperands();
2725 if (NumElems != 4)
2726 return false;
2727 for (unsigned i = 0, e = 2; i != e; ++i)
2728 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2729 return false;
2730 for (unsigned i = 2; i != 4; ++i)
2731 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2732 return false;
2733 return true;
2734}
2735
2736/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002737/// is promoted to a vector. It also returns the LoadSDNode by reference if
2738/// required.
2739static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002740 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2741 return false;
2742 N = N->getOperand(0).getNode();
2743 if (!ISD::isNON_EXTLoad(N))
2744 return false;
2745 if (LD)
2746 *LD = cast<LoadSDNode>(N);
2747 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002748}
2749
2750/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2751/// match movlp{s|d}. The lower half elements should come from lower half of
2752/// V1 (and in order), and the upper half elements should come from the upper
2753/// half of V2 (and in order). And since V1 will become the source of the
2754/// MOVLP, it must be either a vector load or a scalar load to vector.
2755static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2756 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2757 return false;
2758 // Is V2 is a vector load, don't do this transformation. We will try to use
2759 // load folding shufps op.
2760 if (ISD::isNON_EXTLoad(V2))
2761 return false;
2762
2763 unsigned NumElems = Mask->getNumOperands();
2764 if (NumElems != 2 && NumElems != 4)
2765 return false;
2766 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2767 if (!isUndefOrEqual(Mask->getOperand(i), i))
2768 return false;
2769 for (unsigned i = NumElems/2; i != NumElems; ++i)
2770 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2771 return false;
2772 return true;
2773}
2774
2775/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2776/// all the same.
2777static bool isSplatVector(SDNode *N) {
2778 if (N->getOpcode() != ISD::BUILD_VECTOR)
2779 return false;
2780
Dan Gohman8181bd12008-07-27 21:46:04 +00002781 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002782 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2783 if (N->getOperand(i) != SplatValue)
2784 return false;
2785 return true;
2786}
2787
2788/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2789/// to an undef.
2790static bool isUndefShuffle(SDNode *N) {
2791 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2792 return false;
2793
Dan Gohman8181bd12008-07-27 21:46:04 +00002794 SDValue V1 = N->getOperand(0);
2795 SDValue V2 = N->getOperand(1);
2796 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002797 unsigned NumElems = Mask.getNumOperands();
2798 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002799 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002800 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002801 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002802 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2803 return false;
2804 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2805 return false;
2806 }
2807 }
2808 return true;
2809}
2810
2811/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2812/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002813static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002814 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002815 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002816 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002817 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002818}
2819
2820/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2821/// to an zero vector.
2822static bool isZeroShuffle(SDNode *N) {
2823 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2824 return false;
2825
Dan Gohman8181bd12008-07-27 21:46:04 +00002826 SDValue V1 = N->getOperand(0);
2827 SDValue V2 = N->getOperand(1);
2828 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002829 unsigned NumElems = Mask.getNumOperands();
2830 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002831 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002832 if (Arg.getOpcode() == ISD::UNDEF)
2833 continue;
2834
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002835 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002836 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002837 unsigned Opc = V1.getNode()->getOpcode();
2838 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002839 continue;
2840 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002841 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002842 return false;
2843 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002844 unsigned Opc = V2.getNode()->getOpcode();
2845 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002846 continue;
2847 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002848 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002849 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002850 }
2851 }
2852 return true;
2853}
2854
2855/// getZeroVector - Returns a vector of specified type with all zero elements.
2856///
Dan Gohman8181bd12008-07-27 21:46:04 +00002857static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002858 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002859
2860 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2861 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002862 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002863 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002864 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002865 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002866 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002867 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002868 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002869 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002870 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002871 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2872 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002873 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002874}
2875
Chris Lattnere6aa3862007-11-25 00:24:49 +00002876/// getOnesVector - Returns a vector of specified type with all bits set.
2877///
Dan Gohman8181bd12008-07-27 21:46:04 +00002878static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002879 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002880
2881 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2882 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002883 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2884 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002885 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002886 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2887 else // SSE
2888 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2889 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2890}
2891
2892
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002893/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2894/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002895static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002896 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2897
2898 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002899 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002900 unsigned NumElems = Mask.getNumOperands();
2901 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002902 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002903 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002904 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002905 if (Val > NumElems) {
2906 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2907 Changed = true;
2908 }
2909 }
2910 MaskVec.push_back(Arg);
2911 }
2912
2913 if (Changed)
2914 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2915 &MaskVec[0], MaskVec.size());
2916 return Mask;
2917}
2918
2919/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2920/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002921static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002922 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2923 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002924
Dan Gohman8181bd12008-07-27 21:46:04 +00002925 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002926 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2927 for (unsigned i = 1; i != NumElems; ++i)
2928 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2929 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2930}
2931
2932/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2933/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002934static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002935 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2936 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002937 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002938 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2939 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2940 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2941 }
2942 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2943}
2944
2945/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2946/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002947static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002948 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2949 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002950 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002951 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002952 for (unsigned i = 0; i != Half; ++i) {
2953 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2954 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2955 }
2956 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2957}
2958
Chris Lattner2d91b962008-03-09 01:05:04 +00002959/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2960/// element #0 of a vector with the specified index, leaving the rest of the
2961/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002962static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002963 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002964 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2965 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002966 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002967 // Element #0 of the result gets the elt we are replacing.
2968 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2969 for (unsigned i = 1; i != NumElems; ++i)
2970 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2971 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2972}
2973
Evan Chengbf8b2c52008-04-05 00:30:36 +00002974/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002975static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002976 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2977 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002978 if (PVT == VT)
2979 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002980 SDValue V1 = Op.getOperand(0);
2981 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002982 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002983 // Special handling of v4f32 -> v4i32.
2984 if (VT != MVT::v4f32) {
2985 Mask = getUnpacklMask(NumElems, DAG);
2986 while (NumElems > 4) {
2987 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2988 NumElems >>= 1;
2989 }
Evan Cheng8c590372008-05-15 08:39:06 +00002990 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002991 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002992
Evan Chengbf8b2c52008-04-05 00:30:36 +00002993 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002994 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002995 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002996 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2997}
2998
Evan Chenga2497eb2008-09-25 20:50:48 +00002999/// isVectorLoad - Returns true if the node is a vector load, a scalar
3000/// load that's promoted to vector, or a load bitcasted.
3001static bool isVectorLoad(SDValue Op) {
3002 assert(Op.getValueType().isVector() && "Expected a vector type");
3003 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
3004 Op.getOpcode() == ISD::BIT_CONVERT) {
3005 return isa<LoadSDNode>(Op.getOperand(0));
3006 }
3007 return isa<LoadSDNode>(Op);
3008}
3009
3010
3011/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3012///
3013static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3014 SelectionDAG &DAG, bool HasSSE3) {
3015 // If we have sse3 and shuffle has more than one use or input is a load, then
3016 // use movddup. Otherwise, use movlhps.
3017 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3018 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3019 MVT VT = Op.getValueType();
3020 if (VT == PVT)
3021 return Op;
3022 unsigned NumElems = PVT.getVectorNumElements();
3023 if (NumElems == 2) {
3024 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3025 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
3026 } else {
3027 assert(NumElems == 4);
3028 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3029 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
3030 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst0, Cst1, Cst0, Cst1);
3031 }
3032
3033 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
3034 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
3035 DAG.getNode(ISD::UNDEF, PVT), Mask);
3036 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3037}
3038
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003039/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003040/// vector of zero or undef vector. This produces a shuffle where the low
3041/// element of V2 is swizzled into the zero/undef vector, landing at element
3042/// 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 +00003043static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003044 bool isZero, bool HasSSE2,
3045 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003046 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003047 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00003048 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003049 unsigned NumElems = V2.getValueType().getVectorNumElements();
3050 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3051 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003052 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003053 for (unsigned i = 0; i != NumElems; ++i)
3054 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3055 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3056 else
3057 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003058 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003059 &MaskVec[0], MaskVec.size());
3060 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3061}
3062
Evan Chengdea99362008-05-29 08:22:04 +00003063/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3064/// a shuffle that is zero.
3065static
Dan Gohman8181bd12008-07-27 21:46:04 +00003066unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003067 unsigned NumElems, bool Low,
3068 SelectionDAG &DAG) {
3069 unsigned NumZeros = 0;
3070 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003071 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003072 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003073 if (Idx.getOpcode() == ISD::UNDEF) {
3074 ++NumZeros;
3075 continue;
3076 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003077 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3078 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003079 ++NumZeros;
3080 else
3081 break;
3082 }
3083 return NumZeros;
3084}
3085
3086/// isVectorShift - Returns true if the shuffle can be implemented as a
3087/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003088static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3089 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003090 unsigned NumElems = Mask.getNumOperands();
3091
3092 isLeft = true;
3093 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3094 if (!NumZeros) {
3095 isLeft = false;
3096 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3097 if (!NumZeros)
3098 return false;
3099 }
3100
3101 bool SeenV1 = false;
3102 bool SeenV2 = false;
3103 for (unsigned i = NumZeros; i < NumElems; ++i) {
3104 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003105 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003106 if (Idx.getOpcode() == ISD::UNDEF)
3107 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003108 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003109 if (Index < NumElems)
3110 SeenV1 = true;
3111 else {
3112 Index -= NumElems;
3113 SeenV2 = true;
3114 }
3115 if (Index != Val)
3116 return false;
3117 }
3118 if (SeenV1 && SeenV2)
3119 return false;
3120
3121 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3122 ShAmt = NumZeros;
3123 return true;
3124}
3125
3126
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003127/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3128///
Dan Gohman8181bd12008-07-27 21:46:04 +00003129static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003130 unsigned NumNonZero, unsigned NumZero,
3131 SelectionDAG &DAG, TargetLowering &TLI) {
3132 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003133 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003134
Dan Gohman8181bd12008-07-27 21:46:04 +00003135 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003136 bool First = true;
3137 for (unsigned i = 0; i < 16; ++i) {
3138 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3139 if (ThisIsNonZero && First) {
3140 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003141 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003142 else
3143 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3144 First = false;
3145 }
3146
3147 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003148 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003149 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3150 if (LastIsNonZero) {
3151 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3152 }
3153 if (ThisIsNonZero) {
3154 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3155 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3156 ThisElt, DAG.getConstant(8, MVT::i8));
3157 if (LastIsNonZero)
3158 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3159 } else
3160 ThisElt = LastElt;
3161
Gabor Greif1c80d112008-08-28 21:40:38 +00003162 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003163 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003164 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003165 }
3166 }
3167
3168 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3169}
3170
3171/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3172///
Dan Gohman8181bd12008-07-27 21:46:04 +00003173static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003174 unsigned NumNonZero, unsigned NumZero,
3175 SelectionDAG &DAG, TargetLowering &TLI) {
3176 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003177 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003178
Dan Gohman8181bd12008-07-27 21:46:04 +00003179 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003180 bool First = true;
3181 for (unsigned i = 0; i < 8; ++i) {
3182 bool isNonZero = (NonZeros & (1 << i)) != 0;
3183 if (isNonZero) {
3184 if (First) {
3185 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003186 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003187 else
3188 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3189 First = false;
3190 }
3191 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003192 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003193 }
3194 }
3195
3196 return V;
3197}
3198
Evan Chengdea99362008-05-29 08:22:04 +00003199/// getVShift - Return a vector logical shift node.
3200///
Dan Gohman8181bd12008-07-27 21:46:04 +00003201static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003202 unsigned NumBits, SelectionDAG &DAG,
3203 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003204 bool isMMX = VT.getSizeInBits() == 64;
3205 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003206 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3207 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3208 return DAG.getNode(ISD::BIT_CONVERT, VT,
3209 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003210 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003211}
3212
Dan Gohman8181bd12008-07-27 21:46:04 +00003213SDValue
3214X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003215 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003216 if (ISD::isBuildVectorAllZeros(Op.getNode())
3217 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003218 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3219 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3220 // eliminated on x86-32 hosts.
3221 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3222 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003223
Gabor Greif1c80d112008-08-28 21:40:38 +00003224 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003225 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003226 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003227 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003228
Duncan Sands92c43912008-06-06 12:08:01 +00003229 MVT VT = Op.getValueType();
3230 MVT EVT = VT.getVectorElementType();
3231 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003232
3233 unsigned NumElems = Op.getNumOperands();
3234 unsigned NumZero = 0;
3235 unsigned NumNonZero = 0;
3236 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003237 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003238 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003239 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003240 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003241 if (Elt.getOpcode() == ISD::UNDEF)
3242 continue;
3243 Values.insert(Elt);
3244 if (Elt.getOpcode() != ISD::Constant &&
3245 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003246 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003247 if (isZeroNode(Elt))
3248 NumZero++;
3249 else {
3250 NonZeros |= (1 << i);
3251 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003252 }
3253 }
3254
3255 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003256 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3257 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003258 }
3259
Chris Lattner66a4dda2008-03-09 05:42:06 +00003260 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003261 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003262 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003263 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003264
Chris Lattner2d91b962008-03-09 01:05:04 +00003265 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3266 // the value are obviously zero, truncate the value to i32 and do the
3267 // insertion that way. Only do this if the value is non-constant or if the
3268 // value is a constant being inserted into element 0. It is cheaper to do
3269 // a constant pool load than it is to do a movd + shuffle.
3270 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3271 (!IsAllConstants || Idx == 0)) {
3272 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3273 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003274 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3275 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003276
3277 // Truncate the value (which may itself be a constant) to i32, and
3278 // convert it to a vector with movd (S2V+shuffle to zero extend).
3279 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3280 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003281 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3282 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003283
3284 // Now we have our 32-bit value zero extended in the low element of
3285 // a vector. If Idx != 0, swizzle it into place.
3286 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003287 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003288 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3289 getSwapEltZeroMask(VecElts, Idx, DAG)
3290 };
3291 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3292 }
3293 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3294 }
3295 }
3296
Chris Lattnerac914892008-03-08 22:59:52 +00003297 // If we have a constant or non-constant insertion into the low element of
3298 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3299 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3300 // depending on what the source datatype is. Because we can only get here
3301 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3302 if (Idx == 0 &&
3303 // Don't do this for i64 values on x86-32.
3304 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003305 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003306 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003307 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3308 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003309 }
Evan Chengdea99362008-05-29 08:22:04 +00003310
3311 // Is it a vector logical left shift?
3312 if (NumElems == 2 && Idx == 1 &&
3313 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003314 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003315 return getVShift(true, VT,
3316 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3317 NumBits/2, DAG, *this);
3318 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003319
3320 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003321 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003322
Chris Lattnerac914892008-03-08 22:59:52 +00003323 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3324 // is a non-constant being inserted into an element other than the low one,
3325 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3326 // movd/movss) to move this into the low element, then shuffle it into
3327 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003328 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003329 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3330
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003331 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003332 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3333 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003334 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3335 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003336 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003337 for (unsigned i = 0; i < NumElems; i++)
3338 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003339 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003340 &MaskVec[0], MaskVec.size());
3341 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3342 DAG.getNode(ISD::UNDEF, VT), Mask);
3343 }
3344 }
3345
Chris Lattner66a4dda2008-03-09 05:42:06 +00003346 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3347 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003348 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003349
Dan Gohman21463242007-07-24 22:55:08 +00003350 // A vector full of immediates; various special cases are already
3351 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003352 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003353 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003354
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003355 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003356 if (EVTBits == 64) {
3357 if (NumNonZero == 1) {
3358 // One half is zero or undef.
3359 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003360 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003361 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003362 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3363 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003364 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003365 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003366 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003367
3368 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3369 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003370 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003371 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003372 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003373 }
3374
3375 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003376 SDValue V = LowerBuildVectorv8i16(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 element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003382 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003383 V.resize(NumElems);
3384 if (NumElems == 4 && NumZero > 0) {
3385 for (unsigned i = 0; i < 4; ++i) {
3386 bool isZero = !(NonZeros & (1 << i));
3387 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003388 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003389 else
3390 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3391 }
3392
3393 for (unsigned i = 0; i < 2; ++i) {
3394 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3395 default: break;
3396 case 0:
3397 V[i] = V[i*2]; // Must be a zero vector.
3398 break;
3399 case 1:
3400 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3401 getMOVLMask(NumElems, DAG));
3402 break;
3403 case 2:
3404 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3405 getMOVLMask(NumElems, DAG));
3406 break;
3407 case 3:
3408 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3409 getUnpacklMask(NumElems, DAG));
3410 break;
3411 }
3412 }
3413
Duncan Sands92c43912008-06-06 12:08:01 +00003414 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3415 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003416 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003417 bool Reverse = (NonZeros & 0x3) == 2;
3418 for (unsigned i = 0; i < 2; ++i)
3419 if (Reverse)
3420 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3421 else
3422 MaskVec.push_back(DAG.getConstant(i, EVT));
3423 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3424 for (unsigned i = 0; i < 2; ++i)
3425 if (Reverse)
3426 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3427 else
3428 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003429 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003430 &MaskVec[0], MaskVec.size());
3431 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3432 }
3433
3434 if (Values.size() > 2) {
3435 // Expand into a number of unpckl*.
3436 // e.g. for v4f32
3437 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3438 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3439 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003440 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003441 for (unsigned i = 0; i < NumElems; ++i)
3442 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3443 NumElems >>= 1;
3444 while (NumElems != 0) {
3445 for (unsigned i = 0; i < NumElems; ++i)
3446 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3447 UnpckMask);
3448 NumElems >>= 1;
3449 }
3450 return V[0];
3451 }
3452
Dan Gohman8181bd12008-07-27 21:46:04 +00003453 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003454}
3455
Evan Chengfca29242007-12-07 08:07:39 +00003456static
Dan Gohman8181bd12008-07-27 21:46:04 +00003457SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003458 SDValue PermMask, SelectionDAG &DAG,
3459 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003460 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003461 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3462 MVT MaskEVT = MaskVT.getVectorElementType();
3463 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003464 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3465 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003466
3467 // First record which half of which vector the low elements come from.
3468 SmallVector<unsigned, 4> LowQuad(4);
3469 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003470 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003471 if (Elt.getOpcode() == ISD::UNDEF)
3472 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003473 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003474 int QuadIdx = EltIdx / 4;
3475 ++LowQuad[QuadIdx];
3476 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003477
Evan Cheng75184a92007-12-11 01:46:18 +00003478 int BestLowQuad = -1;
3479 unsigned MaxQuad = 1;
3480 for (unsigned i = 0; i < 4; ++i) {
3481 if (LowQuad[i] > MaxQuad) {
3482 BestLowQuad = i;
3483 MaxQuad = LowQuad[i];
3484 }
Evan Chengfca29242007-12-07 08:07:39 +00003485 }
3486
Evan Cheng75184a92007-12-11 01:46:18 +00003487 // Record which half of which vector the high elements come from.
3488 SmallVector<unsigned, 4> HighQuad(4);
3489 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003490 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003491 if (Elt.getOpcode() == ISD::UNDEF)
3492 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003493 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003494 int QuadIdx = EltIdx / 4;
3495 ++HighQuad[QuadIdx];
3496 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003497
Evan Cheng75184a92007-12-11 01:46:18 +00003498 int BestHighQuad = -1;
3499 MaxQuad = 1;
3500 for (unsigned i = 0; i < 4; ++i) {
3501 if (HighQuad[i] > MaxQuad) {
3502 BestHighQuad = i;
3503 MaxQuad = HighQuad[i];
3504 }
3505 }
3506
3507 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3508 if (BestLowQuad != -1 || BestHighQuad != -1) {
3509 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003510 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003511
Evan Cheng75184a92007-12-11 01:46:18 +00003512 if (BestLowQuad != -1)
3513 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3514 else
3515 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003516
Evan Cheng75184a92007-12-11 01:46:18 +00003517 if (BestHighQuad != -1)
3518 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3519 else
3520 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003521
Dan Gohman8181bd12008-07-27 21:46:04 +00003522 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003523 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3524 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3525 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3526 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3527
3528 // Now sort high and low parts separately.
3529 BitVector InOrder(8);
3530 if (BestLowQuad != -1) {
3531 // Sort lower half in order using PSHUFLW.
3532 MaskVec.clear();
3533 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003534
Evan Cheng75184a92007-12-11 01:46:18 +00003535 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003536 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003537 if (Elt.getOpcode() == ISD::UNDEF) {
3538 MaskVec.push_back(Elt);
3539 InOrder.set(i);
3540 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003541 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003542 if (EltIdx != i)
3543 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003544
Evan Cheng75184a92007-12-11 01:46:18 +00003545 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003546
Evan Cheng75184a92007-12-11 01:46:18 +00003547 // If this element is in the right place after this shuffle, then
3548 // remember it.
3549 if ((int)(EltIdx / 4) == BestLowQuad)
3550 InOrder.set(i);
3551 }
3552 }
3553 if (AnyOutOrder) {
3554 for (unsigned i = 4; i != 8; ++i)
3555 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003556 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003557 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3558 }
3559 }
3560
3561 if (BestHighQuad != -1) {
3562 // Sort high half in order using PSHUFHW if possible.
3563 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003564
Evan Cheng75184a92007-12-11 01:46:18 +00003565 for (unsigned i = 0; i != 4; ++i)
3566 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003567
Evan Cheng75184a92007-12-11 01:46:18 +00003568 bool AnyOutOrder = false;
3569 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003570 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003571 if (Elt.getOpcode() == ISD::UNDEF) {
3572 MaskVec.push_back(Elt);
3573 InOrder.set(i);
3574 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003575 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003576 if (EltIdx != i)
3577 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003578
Evan Cheng75184a92007-12-11 01:46:18 +00003579 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003580
Evan Cheng75184a92007-12-11 01:46:18 +00003581 // If this element is in the right place after this shuffle, then
3582 // remember it.
3583 if ((int)(EltIdx / 4) == BestHighQuad)
3584 InOrder.set(i);
3585 }
3586 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003587
Evan Cheng75184a92007-12-11 01:46:18 +00003588 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003589 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003590 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3591 }
3592 }
3593
3594 // The other elements are put in the right place using pextrw and pinsrw.
3595 for (unsigned i = 0; i != 8; ++i) {
3596 if (InOrder[i])
3597 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003598 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003599 if (Elt.getOpcode() == ISD::UNDEF)
3600 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003601 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003602 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003603 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3604 DAG.getConstant(EltIdx, PtrVT))
3605 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3606 DAG.getConstant(EltIdx - 8, PtrVT));
3607 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3608 DAG.getConstant(i, PtrVT));
3609 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003610
Evan Cheng75184a92007-12-11 01:46:18 +00003611 return NewV;
3612 }
3613
Bill Wendling2c7cd592008-08-21 22:35:37 +00003614 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3615 // few as possible. First, let's find out how many elements are already in the
3616 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003617 unsigned V1InOrder = 0;
3618 unsigned V1FromV1 = 0;
3619 unsigned V2InOrder = 0;
3620 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003621 SmallVector<SDValue, 8> V1Elts;
3622 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003623 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003624 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003625 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003626 V1Elts.push_back(Elt);
3627 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003628 ++V1InOrder;
3629 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003630 continue;
3631 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003632 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003633 if (EltIdx == i) {
3634 V1Elts.push_back(Elt);
3635 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3636 ++V1InOrder;
3637 } else if (EltIdx == i+8) {
3638 V1Elts.push_back(Elt);
3639 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3640 ++V2InOrder;
3641 } else if (EltIdx < 8) {
3642 V1Elts.push_back(Elt);
3643 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003644 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003645 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3646 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003647 }
3648 }
3649
3650 if (V2InOrder > V1InOrder) {
3651 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3652 std::swap(V1, V2);
3653 std::swap(V1Elts, V2Elts);
3654 std::swap(V1FromV1, V2FromV2);
3655 }
3656
Evan Cheng75184a92007-12-11 01:46:18 +00003657 if ((V1FromV1 + V1InOrder) != 8) {
3658 // Some elements are from V2.
3659 if (V1FromV1) {
3660 // If there are elements that are from V1 but out of place,
3661 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003662 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003663 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003664 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003665 if (Elt.getOpcode() == ISD::UNDEF) {
3666 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3667 continue;
3668 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003669 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003670 if (EltIdx >= 8)
3671 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3672 else
3673 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3674 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003675 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003676 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003677 }
Evan Cheng75184a92007-12-11 01:46:18 +00003678
3679 NewV = V1;
3680 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003681 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003682 if (Elt.getOpcode() == ISD::UNDEF)
3683 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003684 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003685 if (EltIdx < 8)
3686 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003687 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003688 DAG.getConstant(EltIdx - 8, PtrVT));
3689 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3690 DAG.getConstant(i, PtrVT));
3691 }
3692 return NewV;
3693 } else {
3694 // All elements are from V1.
3695 NewV = V1;
3696 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003697 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003698 if (Elt.getOpcode() == ISD::UNDEF)
3699 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003700 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003701 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003702 DAG.getConstant(EltIdx, PtrVT));
3703 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3704 DAG.getConstant(i, PtrVT));
3705 }
3706 return NewV;
3707 }
3708}
3709
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003710/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3711/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3712/// done when every pair / quad of shuffle mask elements point to elements in
3713/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003714/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3715static
Dan Gohman8181bd12008-07-27 21:46:04 +00003716SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003717 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003718 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003719 TargetLowering &TLI) {
3720 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003721 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003722 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003723 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003724 MVT NewVT = MaskVT;
3725 switch (VT.getSimpleVT()) {
3726 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003727 case MVT::v4f32: NewVT = MVT::v2f64; break;
3728 case MVT::v4i32: NewVT = MVT::v2i64; break;
3729 case MVT::v8i16: NewVT = MVT::v4i32; break;
3730 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003731 }
3732
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003733 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003734 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003735 NewVT = MVT::v2i64;
3736 else
3737 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003738 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003739 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003740 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003741 for (unsigned i = 0; i < NumElems; i += Scale) {
3742 unsigned StartIdx = ~0U;
3743 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003744 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003745 if (Elt.getOpcode() == ISD::UNDEF)
3746 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003747 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003748 if (StartIdx == ~0U)
3749 StartIdx = EltIdx - (EltIdx % Scale);
3750 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003751 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003752 }
3753 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003754 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003755 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003756 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003757 }
3758
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003759 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3760 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3761 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3762 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3763 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003764}
3765
Evan Chenge9b9c672008-05-09 21:53:03 +00003766/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003767///
Dan Gohman8181bd12008-07-27 21:46:04 +00003768static SDValue getVZextMovL(MVT VT, MVT OpVT,
3769 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003770 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003771 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3772 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003773 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003774 LD = dyn_cast<LoadSDNode>(SrcOp);
3775 if (!LD) {
3776 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3777 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003778 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003779 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3780 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3781 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3782 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3783 // PR2108
3784 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3785 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003786 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003787 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003788 SrcOp.getOperand(0)
3789 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003790 }
3791 }
3792 }
3793
3794 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003795 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003796 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3797}
3798
Evan Chengf50554e2008-07-22 21:13:36 +00003799/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3800/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003801static SDValue
3802LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3803 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003804 MVT MaskVT = PermMask.getValueType();
3805 MVT MaskEVT = MaskVT.getVectorElementType();
3806 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003807 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003808 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003809 unsigned NumHi = 0;
3810 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003811 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003812 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003813 if (Elt.getOpcode() == ISD::UNDEF) {
3814 Locs[i] = std::make_pair(-1, -1);
3815 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003816 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003817 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003818 if (Val < 4) {
3819 Locs[i] = std::make_pair(0, NumLo);
3820 Mask1[NumLo] = Elt;
3821 NumLo++;
3822 } else {
3823 Locs[i] = std::make_pair(1, NumHi);
3824 if (2+NumHi < 4)
3825 Mask1[2+NumHi] = Elt;
3826 NumHi++;
3827 }
3828 }
3829 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003830
Evan Chengf50554e2008-07-22 21:13:36 +00003831 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003832 // If no more than two elements come from either vector. This can be
3833 // implemented with two shuffles. First shuffle gather the elements.
3834 // The second shuffle, which takes the first shuffle as both of its
3835 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003836 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3837 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3838 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003839
Dan Gohman8181bd12008-07-27 21:46:04 +00003840 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003841 for (unsigned i = 0; i != 4; ++i) {
3842 if (Locs[i].first == -1)
3843 continue;
3844 else {
3845 unsigned Idx = (i < 2) ? 0 : 4;
3846 Idx += Locs[i].first * 2 + Locs[i].second;
3847 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3848 }
3849 }
3850
3851 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3852 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3853 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003854 } else if (NumLo == 3 || NumHi == 3) {
3855 // Otherwise, we must have three elements from one vector, call it X, and
3856 // one element from the other, call it Y. First, use a shufps to build an
3857 // intermediate vector with the one element from Y and the element from X
3858 // that will be in the same half in the final destination (the indexes don't
3859 // matter). Then, use a shufps to build the final vector, taking the half
3860 // containing the element from Y from the intermediate, and the other half
3861 // from X.
3862 if (NumHi == 3) {
3863 // Normalize it so the 3 elements come from V1.
3864 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3865 std::swap(V1, V2);
3866 }
3867
3868 // Find the element from V2.
3869 unsigned HiIndex;
3870 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003871 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003872 if (Elt.getOpcode() == ISD::UNDEF)
3873 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003874 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003875 if (Val >= 4)
3876 break;
3877 }
3878
3879 Mask1[0] = PermMask.getOperand(HiIndex);
3880 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3881 Mask1[2] = PermMask.getOperand(HiIndex^1);
3882 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3883 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3884 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3885
3886 if (HiIndex >= 2) {
3887 Mask1[0] = PermMask.getOperand(0);
3888 Mask1[1] = PermMask.getOperand(1);
3889 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3890 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3891 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3892 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3893 } else {
3894 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3895 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3896 Mask1[2] = PermMask.getOperand(2);
3897 Mask1[3] = PermMask.getOperand(3);
3898 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003899 Mask1[2] =
3900 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3901 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003902 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003903 Mask1[3] =
3904 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3905 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003906 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3907 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3908 }
Evan Chengf50554e2008-07-22 21:13:36 +00003909 }
3910
3911 // Break it into (shuffle shuffle_hi, shuffle_lo).
3912 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003913 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3914 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3915 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003916 unsigned MaskIdx = 0;
3917 unsigned LoIdx = 0;
3918 unsigned HiIdx = 2;
3919 for (unsigned i = 0; i != 4; ++i) {
3920 if (i == 2) {
3921 MaskPtr = &HiMask;
3922 MaskIdx = 1;
3923 LoIdx = 0;
3924 HiIdx = 2;
3925 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003926 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003927 if (Elt.getOpcode() == ISD::UNDEF) {
3928 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003929 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003930 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3931 (*MaskPtr)[LoIdx] = Elt;
3932 LoIdx++;
3933 } else {
3934 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3935 (*MaskPtr)[HiIdx] = Elt;
3936 HiIdx++;
3937 }
3938 }
3939
Dan Gohman8181bd12008-07-27 21:46:04 +00003940 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003941 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3942 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003943 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003944 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3945 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003946 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003947 for (unsigned i = 0; i != 4; ++i) {
3948 if (Locs[i].first == -1) {
3949 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3950 } else {
3951 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3952 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3953 }
3954 }
3955 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3956 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3957 &MaskOps[0], MaskOps.size()));
3958}
3959
Dan Gohman8181bd12008-07-27 21:46:04 +00003960SDValue
3961X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3962 SDValue V1 = Op.getOperand(0);
3963 SDValue V2 = Op.getOperand(1);
3964 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003965 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003966 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003967 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003968 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3969 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3970 bool V1IsSplat = false;
3971 bool V2IsSplat = false;
3972
Gabor Greif1c80d112008-08-28 21:40:38 +00003973 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003974 return DAG.getNode(ISD::UNDEF, VT);
3975
Gabor Greif1c80d112008-08-28 21:40:38 +00003976 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003977 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003978
Gabor Greif1c80d112008-08-28 21:40:38 +00003979 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003980 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003981 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003982 return V2;
3983
Evan Chengae6c9212008-09-25 23:35:16 +00003984 // Canonicalize movddup shuffles.
3985 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Chengbdd9d9f2008-10-06 21:13:08 +00003986 VT.getSizeInBits() == 128 &&
Evan Chengae6c9212008-09-25 23:35:16 +00003987 X86::isMOVDDUPMask(PermMask.getNode()))
3988 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
3989
Gabor Greif1c80d112008-08-28 21:40:38 +00003990 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003991 if (isMMX || NumElems < 4) return Op;
3992 // Promote it to a v4{if}32 splat.
3993 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003994 }
3995
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003996 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3997 // do it!
3998 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003999 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004000 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004001 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
4002 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4003 // FIXME: Figure out a cleaner way to do this.
4004 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004005 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004006 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004007 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004008 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004009 SDValue NewV1 = NewOp.getOperand(0);
4010 SDValue NewV2 = NewOp.getOperand(1);
4011 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00004012 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004013 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00004014 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004015 }
4016 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004017 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004018 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004019 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004020 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004021 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00004022 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004023 }
4024 }
4025
Evan Chengdea99362008-05-29 08:22:04 +00004026 // Check if this can be converted into a logical shift.
4027 bool isLeft = false;
4028 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004029 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00004030 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4031 if (isShift && ShVal.hasOneUse()) {
4032 // If the shifted value has multiple uses, it may be cheaper to use
4033 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004034 MVT EVT = VT.getVectorElementType();
4035 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004036 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4037 }
4038
Gabor Greif1c80d112008-08-28 21:40:38 +00004039 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004040 if (V1IsUndef)
4041 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004042 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004043 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004044 if (!isMMX)
4045 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004046 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004047
Gabor Greif1c80d112008-08-28 21:40:38 +00004048 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4049 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4050 X86::isMOVHLPSMask(PermMask.getNode()) ||
4051 X86::isMOVHPMask(PermMask.getNode()) ||
4052 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004053 return Op;
4054
Gabor Greif1c80d112008-08-28 21:40:38 +00004055 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4056 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004057 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4058
Evan Chengdea99362008-05-29 08:22:04 +00004059 if (isShift) {
4060 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004061 MVT EVT = VT.getVectorElementType();
4062 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004063 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4064 }
4065
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004066 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004067 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4068 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004069 V1IsSplat = isSplatVector(V1.getNode());
4070 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004071
4072 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004073 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4074 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4075 std::swap(V1IsSplat, V2IsSplat);
4076 std::swap(V1IsUndef, V2IsUndef);
4077 Commuted = true;
4078 }
4079
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004080 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004081 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004082 if (V2IsUndef) return V1;
4083 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4084 if (V2IsSplat) {
4085 // V2 is a splat, so the mask may be malformed. That is, it may point
4086 // to any V2 element. The instruction selectior won't like this. Get
4087 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004088 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004089 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004090 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4091 }
4092 return Op;
4093 }
4094
Gabor Greif1c80d112008-08-28 21:40:38 +00004095 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4096 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4097 X86::isUNPCKLMask(PermMask.getNode()) ||
4098 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004099 return Op;
4100
4101 if (V2IsSplat) {
4102 // Normalize mask so all entries that point to V2 points to its first
4103 // element then try to match unpck{h|l} again. If match, return a
4104 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004105 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004106 if (NewMask.getNode() != PermMask.getNode()) {
4107 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004108 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004109 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004110 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004111 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004112 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4113 }
4114 }
4115 }
4116
4117 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004118 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004119 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4120
4121 if (Commuted) {
4122 // Commute is back and try unpck* again.
4123 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004124 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4125 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4126 X86::isUNPCKLMask(PermMask.getNode()) ||
4127 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004128 return Op;
4129 }
4130
Evan Chengbf8b2c52008-04-05 00:30:36 +00004131 // Try PSHUF* first, then SHUFP*.
4132 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4133 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004134 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004135 if (V2.getOpcode() != ISD::UNDEF)
4136 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4137 DAG.getNode(ISD::UNDEF, VT), PermMask);
4138 return Op;
4139 }
4140
4141 if (!isMMX) {
4142 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004143 (X86::isPSHUFDMask(PermMask.getNode()) ||
4144 X86::isPSHUFHWMask(PermMask.getNode()) ||
4145 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004146 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004147 if (VT == MVT::v4f32) {
4148 RVT = MVT::v4i32;
4149 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4150 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4151 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4152 } else if (V2.getOpcode() != ISD::UNDEF)
4153 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4154 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4155 if (RVT != VT)
4156 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004157 return Op;
4158 }
4159
Evan Chengbf8b2c52008-04-05 00:30:36 +00004160 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004161 if (X86::isSHUFPMask(PermMask.getNode()) ||
4162 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004163 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004164 }
4165
Evan Cheng75184a92007-12-11 01:46:18 +00004166 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4167 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004168 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004169 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004170 return NewOp;
4171 }
4172
Evan Chengf50554e2008-07-22 21:13:36 +00004173 // Handle all 4 wide cases with a number of shuffles except for MMX.
4174 if (NumElems == 4 && !isMMX)
4175 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004176
Dan Gohman8181bd12008-07-27 21:46:04 +00004177 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004178}
4179
Dan Gohman8181bd12008-07-27 21:46:04 +00004180SDValue
4181X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004182 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004183 MVT VT = Op.getValueType();
4184 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004185 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004186 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004187 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004188 DAG.getValueType(VT));
4189 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004190 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004191 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, 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);
Evan Cheng6c249332008-03-24 21:52:23 +00004196 } else if (VT == MVT::f32) {
4197 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4198 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004199 // result has a single use which is a store or a bitcast to i32. And in
4200 // the case of a store, it's not worth it if the index is a constant 0,
4201 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004202 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004203 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004204 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004205 if ((User->getOpcode() != ISD::STORE ||
4206 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4207 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004208 (User->getOpcode() != ISD::BIT_CONVERT ||
4209 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004210 return SDValue();
4211 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004212 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4213 Op.getOperand(1));
4214 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004215 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004216 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004217}
4218
4219
Dan Gohman8181bd12008-07-27 21:46:04 +00004220SDValue
4221X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004222 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004223 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004224
Evan Cheng6c249332008-03-24 21:52:23 +00004225 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004226 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004227 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004228 return Res;
4229 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004230
Duncan Sands92c43912008-06-06 12:08:01 +00004231 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004232 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004233 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004234 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004235 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004236 if (Idx == 0)
4237 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4238 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4239 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4240 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004241 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004242 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004243 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004244 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004245 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004246 DAG.getValueType(VT));
4247 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004248 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004249 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004250 if (Idx == 0)
4251 return Op;
4252 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004253 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004254 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004255 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004256 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004257 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004258 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004259 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004260 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004261 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004262 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004263 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004264 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004265 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004266 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4267 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4268 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004269 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004270 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004271 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4272 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4273 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004274 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004275 if (Idx == 0)
4276 return Op;
4277
4278 // UNPCKHPD the element to the lowest double word, then movsd.
4279 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4280 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004281 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004282 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004283 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004284 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004285 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004286 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004287 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004288 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004289 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4290 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4291 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004292 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004293 }
4294
Dan Gohman8181bd12008-07-27 21:46:04 +00004295 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004296}
4297
Dan Gohman8181bd12008-07-27 21:46:04 +00004298SDValue
4299X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004300 MVT VT = Op.getValueType();
4301 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004302
Dan Gohman8181bd12008-07-27 21:46:04 +00004303 SDValue N0 = Op.getOperand(0);
4304 SDValue N1 = Op.getOperand(1);
4305 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004306
Dan Gohman5a7af042008-08-14 22:53:18 +00004307 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4308 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004309 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004310 : X86ISD::PINSRW;
4311 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4312 // argument.
4313 if (N1.getValueType() != MVT::i32)
4314 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4315 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004316 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begemand77e59e2008-02-11 04:19:36 +00004317 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004318 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004319 // Bits [7:6] of the constant are the source select. This will always be
4320 // zero here. The DAG Combiner may combine an extract_elt index into these
4321 // bits. For example (insert (extract, 3), 2) could be matched by putting
4322 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4323 // Bits [5:4] of the constant are the destination select. This is the
4324 // value of the incoming immediate.
4325 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4326 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004327 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begemand77e59e2008-02-11 04:19:36 +00004328 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4329 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004330 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004331}
4332
Dan Gohman8181bd12008-07-27 21:46:04 +00004333SDValue
4334X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004335 MVT VT = Op.getValueType();
4336 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004337
4338 if (Subtarget->hasSSE41())
4339 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4340
Evan Chenge12a7eb2007-12-12 07:55:34 +00004341 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004342 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004343
Dan Gohman8181bd12008-07-27 21:46:04 +00004344 SDValue N0 = Op.getOperand(0);
4345 SDValue N1 = Op.getOperand(1);
4346 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004347
Duncan Sands92c43912008-06-06 12:08:01 +00004348 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004349 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4350 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004351 if (N1.getValueType() != MVT::i32)
4352 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4353 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004354 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004355 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004356 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004357 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004358}
4359
Dan Gohman8181bd12008-07-27 21:46:04 +00004360SDValue
4361X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004362 if (Op.getValueType() == MVT::v2f32)
4363 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4364 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4365 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4366 Op.getOperand(0))));
4367
Dan Gohman8181bd12008-07-27 21:46:04 +00004368 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004369 MVT VT = MVT::v2i32;
4370 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004371 default: break;
4372 case MVT::v16i8:
4373 case MVT::v8i16:
4374 VT = MVT::v4i32;
4375 break;
4376 }
4377 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4378 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004379}
4380
Bill Wendlingfef06052008-09-16 21:48:12 +00004381// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4382// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4383// one of the above mentioned nodes. It has to be wrapped because otherwise
4384// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4385// be used to form addressing mode. These wrapped nodes will be selected
4386// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004387SDValue
4388X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004389 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004390 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004391 getPointerTy(),
4392 CP->getAlignment());
4393 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4394 // With PIC, the address is actually $g + Offset.
4395 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4396 !Subtarget->isPICStyleRIPRel()) {
4397 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4398 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4399 Result);
4400 }
4401
4402 return Result;
4403}
4404
Dan Gohman8181bd12008-07-27 21:46:04 +00004405SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004406X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
Dan Gohman36322c72008-10-18 02:06:02 +00004407 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00004408 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00004409 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4410 bool ExtraLoadRequired =
4411 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4412
4413 // Create the TargetGlobalAddress node, folding in the constant
4414 // offset if it is legal.
4415 SDValue Result;
Dan Gohman3d5257c2008-10-21 03:38:42 +00004416 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +00004417 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4418 Offset = 0;
4419 } else
4420 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004421 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00004422
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423 // With PIC, the address is actually $g + Offset.
Dan Gohman36322c72008-10-18 02:06:02 +00004424 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004425 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4426 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4427 Result);
4428 }
4429
4430 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4431 // load the value at address GV, not the value of GV itself. This means that
4432 // the GlobalAddress must be in the base or index register of the address, not
4433 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4434 // The same applies for external symbols during PIC codegen
Dan Gohman36322c72008-10-18 02:06:02 +00004435 if (ExtraLoadRequired)
Dan Gohman12a9c082008-02-06 22:27:42 +00004436 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004437 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004438
Dan Gohman36322c72008-10-18 02:06:02 +00004439 // If there was a non-zero offset that we didn't fold, create an explicit
4440 // addition for it.
4441 if (Offset != 0)
4442 Result = DAG.getNode(ISD::ADD, getPointerTy(), Result,
4443 DAG.getConstant(Offset, getPointerTy()));
4444
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004445 return Result;
4446}
4447
Evan Cheng7f250d62008-09-24 00:05:32 +00004448SDValue
4449X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4450 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00004451 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
4452 return LowerGlobalAddress(GV, Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00004453}
4454
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004455// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004456static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004457LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004458 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004459 SDValue InFlag;
4460 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004461 DAG.getNode(X86ISD::GlobalBaseReg,
4462 PtrVT), InFlag);
4463 InFlag = Chain.getValue(1);
4464
4465 // emit leal symbol@TLSGD(,%ebx,1), %eax
4466 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004467 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004468 GA->getValueType(0),
4469 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004470 SDValue Ops[] = { Chain, TGA, InFlag };
4471 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004472 InFlag = Result.getValue(2);
4473 Chain = Result.getValue(1);
4474
4475 // call ___tls_get_addr. This function receives its argument in
4476 // the register EAX.
4477 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4478 InFlag = Chain.getValue(1);
4479
4480 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004481 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004482 DAG.getTargetExternalSymbol("___tls_get_addr",
4483 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004484 DAG.getRegister(X86::EAX, PtrVT),
4485 DAG.getRegister(X86::EBX, PtrVT),
4486 InFlag };
4487 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4488 InFlag = Chain.getValue(1);
4489
4490 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4491}
4492
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004493// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004494static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004495LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004496 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004497 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004498
4499 // emit leaq symbol@TLSGD(%rip), %rdi
4500 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004501 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004502 GA->getValueType(0),
4503 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004504 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4505 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004506 Chain = Result.getValue(1);
4507 InFlag = Result.getValue(2);
4508
aslb204cd52008-08-16 12:58:29 +00004509 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004510 // the register RDI.
4511 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4512 InFlag = Chain.getValue(1);
4513
4514 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004515 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004516 DAG.getTargetExternalSymbol("__tls_get_addr",
4517 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004518 DAG.getRegister(X86::RDI, PtrVT),
4519 InFlag };
4520 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4521 InFlag = Chain.getValue(1);
4522
4523 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4524}
4525
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004526// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4527// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004528static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004529 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004530 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004531 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004532 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4533 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004534 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004535 GA->getValueType(0),
4536 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004537 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004538
4539 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004540 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004541 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004542
4543 // The address of the thread local variable is the add of the thread
4544 // pointer with the offset of the variable.
4545 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4546}
4547
Dan Gohman8181bd12008-07-27 21:46:04 +00004548SDValue
4549X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004550 // TODO: implement the "local dynamic" model
4551 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004552 assert(Subtarget->isTargetELF() &&
4553 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004554 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4555 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4556 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004557 if (Subtarget->is64Bit()) {
4558 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4559 } else {
4560 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4561 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4562 else
4563 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4564 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004565}
4566
Dan Gohman8181bd12008-07-27 21:46:04 +00004567SDValue
4568X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004569 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4570 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004571 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4572 // With PIC, the address is actually $g + Offset.
4573 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4574 !Subtarget->isPICStyleRIPRel()) {
4575 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4576 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4577 Result);
4578 }
4579
4580 return Result;
4581}
4582
Dan Gohman8181bd12008-07-27 21:46:04 +00004583SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004584 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004585 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004586 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4587 // With PIC, the address is actually $g + Offset.
4588 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4589 !Subtarget->isPICStyleRIPRel()) {
4590 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4591 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4592 Result);
4593 }
4594
4595 return Result;
4596}
4597
Chris Lattner62814a32007-10-17 06:02:13 +00004598/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4599/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004600SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004601 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004602 MVT VT = Op.getValueType();
4603 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004604 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004605 SDValue ShOpLo = Op.getOperand(0);
4606 SDValue ShOpHi = Op.getOperand(1);
4607 SDValue ShAmt = Op.getOperand(2);
4608 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004609 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4610 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004611
Dan Gohman8181bd12008-07-27 21:46:04 +00004612 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004613 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004614 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4615 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004616 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004617 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4618 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004619 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004620
Dan Gohman8181bd12008-07-27 21:46:04 +00004621 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004622 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004623 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004624 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004625
Dan Gohman8181bd12008-07-27 21:46:04 +00004626 SDValue Hi, Lo;
4627 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4628 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4629 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004630
Chris Lattner62814a32007-10-17 06:02:13 +00004631 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004632 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4633 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004634 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004635 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4636 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004637 }
4638
Dan Gohman8181bd12008-07-27 21:46:04 +00004639 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004640 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004641}
4642
Dan Gohman8181bd12008-07-27 21:46:04 +00004643SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004644 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004645 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004646 "Unknown SINT_TO_FP to lower!");
4647
4648 // These are really Legal; caller falls through into that case.
4649 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004650 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004651 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4652 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004653 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004654
Duncan Sands92c43912008-06-06 12:08:01 +00004655 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004656 MachineFunction &MF = DAG.getMachineFunction();
4657 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004658 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4659 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004660 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004661 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004662
4663 // Build the FILD
4664 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004665 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004666 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004667 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4668 else
4669 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004670 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004671 Ops.push_back(Chain);
4672 Ops.push_back(StackSlot);
4673 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004674 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004675 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004676
Dale Johannesen2fc20782007-09-14 22:26:36 +00004677 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004678 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004679 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004680
4681 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4682 // shouldn't be necessary except that RFP cannot be live across
4683 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4684 MachineFunction &MF = DAG.getMachineFunction();
4685 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004686 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004687 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004688 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004689 Ops.push_back(Chain);
4690 Ops.push_back(Result);
4691 Ops.push_back(StackSlot);
4692 Ops.push_back(DAG.getValueType(Op.getValueType()));
4693 Ops.push_back(InFlag);
4694 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004695 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004696 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004697 }
4698
4699 return Result;
4700}
4701
Dale Johannesena359b8b2008-10-21 20:50:01 +00004702SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4703 MVT SrcVT = Op.getOperand(0).getValueType();
4704 assert(SrcVT.getSimpleVT() == MVT::i64 && "Unknown UINT_TO_FP to lower!");
4705
4706 // We only handle SSE2 f64 target here; caller can handle the rest.
4707 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4708 return SDValue();
4709
Dale Johannesenfb019af2008-10-21 23:07:49 +00004710 // This algorithm is not obvious. Here it is in C code, more or less:
4711/*
4712 double uint64_to_double( uint32_t hi, uint32_t lo )
4713 {
4714 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4715 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
4716
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004717 // copy ints to xmm registers
Dale Johannesenfb019af2008-10-21 23:07:49 +00004718 __m128i xh = _mm_cvtsi32_si128( hi );
4719 __m128i xl = _mm_cvtsi32_si128( lo );
4720
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004721 // combine into low half of a single xmm register
Dale Johannesenfb019af2008-10-21 23:07:49 +00004722 __m128i x = _mm_unpacklo_epi32( xh, xl );
4723 __m128d d;
4724 double sd;
4725
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004726 // merge in appropriate exponents to give the integer bits the
Dale Johannesenfb019af2008-10-21 23:07:49 +00004727 // right magnitude
4728 x = _mm_unpacklo_epi32( x, exp );
4729
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004730 // subtract away the biases to deal with the IEEE-754 double precision
4731 // implicit 1
Dale Johannesenfb019af2008-10-21 23:07:49 +00004732 d = _mm_sub_pd( (__m128d) x, bias );
4733
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004734 // All conversions up to here are exact. The correctly rounded result is
Dale Johannesenfb019af2008-10-21 23:07:49 +00004735 // calculated using the
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004736 // current rounding mode using the following horizontal add.
Dale Johannesenfb019af2008-10-21 23:07:49 +00004737 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4738 _mm_store_sd( &sd, d ); //since we are returning doubles in XMM, this
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004739 // store doesn't really need to be here (except maybe to zero the other
4740 // double)
Dale Johannesenfb019af2008-10-21 23:07:49 +00004741 return sd;
4742 }
4743*/
4744
Dale Johannesena359b8b2008-10-21 20:50:01 +00004745 // Build some magic constants.
4746 std::vector<Constant*>CV0;
4747 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4748 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4749 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4750 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4751 Constant *C0 = ConstantVector::get(CV0);
4752 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 4);
4753
4754 std::vector<Constant*>CV1;
4755 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4756 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4757 Constant *C1 = ConstantVector::get(CV1);
4758 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 4);
4759
4760 SmallVector<SDValue, 4> MaskVec;
4761 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
4762 MaskVec.push_back(DAG.getConstant(4, MVT::i32));
4763 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
4764 MaskVec.push_back(DAG.getConstant(5, MVT::i32));
4765 SDValue UnpcklMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, &MaskVec[0],
4766 MaskVec.size());
4767 SmallVector<SDValue, 4> MaskVec2;
Duncan Sandsca872ca2008-10-22 11:24:12 +00004768 MaskVec2.push_back(DAG.getConstant(1, MVT::i32));
4769 MaskVec2.push_back(DAG.getConstant(0, MVT::i32));
4770 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec2[0],
Dale Johannesena359b8b2008-10-21 20:50:01 +00004771 MaskVec2.size());
4772
4773 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004774 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4775 Op.getOperand(0),
4776 DAG.getIntPtrConstant(1)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004777 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004778 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4779 Op.getOperand(0),
4780 DAG.getIntPtrConstant(0)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004781 SDValue Unpck1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4782 XR1, XR2, UnpcklMask);
4783 SDValue CLod0 = DAG.getLoad(MVT::v4i32, DAG.getEntryNode(), CPIdx0,
4784 PseudoSourceValue::getConstantPool(), 0, false, 16);
4785 SDValue Unpck2 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4786 Unpck1, CLod0, UnpcklMask);
4787 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Unpck2);
4788 SDValue CLod1 = DAG.getLoad(MVT::v2f64, CLod0.getValue(1), CPIdx1,
4789 PseudoSourceValue::getConstantPool(), 0, false, 16);
4790 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::v2f64, XR2F, CLod1);
4791 // Add the halves; easiest way is to swap them into another reg first.
4792 SDValue Shuf = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2f64,
4793 Sub, Sub, ShufMask);
4794 SDValue Add = DAG.getNode(ISD::FADD, MVT::v2f64, Shuf, Sub);
4795 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64, Add,
4796 DAG.getIntPtrConstant(0));
4797}
4798
Dan Gohman8181bd12008-07-27 21:46:04 +00004799std::pair<SDValue,SDValue> X86TargetLowering::
4800FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004801 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4802 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004803 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004804
Dale Johannesen2fc20782007-09-14 22:26:36 +00004805 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004806 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004807 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004808 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004809 if (Subtarget->is64Bit() &&
4810 Op.getValueType() == MVT::i64 &&
4811 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004812 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004813
Evan Cheng05441e62007-10-15 20:11:21 +00004814 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4815 // stack slot.
4816 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004817 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004818 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004819 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004820 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004821 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004822 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4823 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4824 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4825 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004826 }
4827
Dan Gohman8181bd12008-07-27 21:46:04 +00004828 SDValue Chain = DAG.getEntryNode();
4829 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004830 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004831 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004832 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004833 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004834 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004835 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004836 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4837 };
4838 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4839 Chain = Value.getValue(1);
4840 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4841 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4842 }
4843
4844 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004845 SDValue Ops[] = { Chain, Value, StackSlot };
4846 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004847
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004848 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004849}
4850
Dan Gohman8181bd12008-07-27 21:46:04 +00004851SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4852 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4853 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004854 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004855
4856 // Load the result.
4857 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4858}
4859
4860SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004861 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4862 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004863 if (FIST.getNode() == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004864
4865 MVT VT = N->getValueType(0);
4866
4867 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004868 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004869
Duncan Sands698842f2008-07-02 17:40:58 +00004870 // Use MERGE_VALUES to drop the chain result value and get a node with one
4871 // result. This requires turning off getMergeValues simplification, since
4872 // otherwise it will give us Res back.
Gabor Greif1c80d112008-08-28 21:40:38 +00004873 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf19591c2008-06-30 10:19:09 +00004874}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004875
Dan Gohman8181bd12008-07-27 21:46:04 +00004876SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004877 MVT VT = Op.getValueType();
4878 MVT EltVT = VT;
4879 if (VT.isVector())
4880 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004881 std::vector<Constant*> CV;
4882 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004883 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004884 CV.push_back(C);
4885 CV.push_back(C);
4886 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004887 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004888 CV.push_back(C);
4889 CV.push_back(C);
4890 CV.push_back(C);
4891 CV.push_back(C);
4892 }
Dan Gohman11821702007-07-27 17:16:43 +00004893 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004894 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4895 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004896 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004897 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004898 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4899}
4900
Dan Gohman8181bd12008-07-27 21:46:04 +00004901SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004902 MVT VT = Op.getValueType();
4903 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004904 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004905 if (VT.isVector()) {
4906 EltVT = VT.getVectorElementType();
4907 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004908 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004909 std::vector<Constant*> CV;
4910 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004911 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004912 CV.push_back(C);
4913 CV.push_back(C);
4914 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004915 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004916 CV.push_back(C);
4917 CV.push_back(C);
4918 CV.push_back(C);
4919 CV.push_back(C);
4920 }
Dan Gohman11821702007-07-27 17:16:43 +00004921 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004922 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4923 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004924 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004925 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004926 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004927 return DAG.getNode(ISD::BIT_CONVERT, VT,
4928 DAG.getNode(ISD::XOR, MVT::v2i64,
4929 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4930 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4931 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004932 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4933 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004934}
4935
Dan Gohman8181bd12008-07-27 21:46:04 +00004936SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4937 SDValue Op0 = Op.getOperand(0);
4938 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004939 MVT VT = Op.getValueType();
4940 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004941
4942 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004943 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004944 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4945 SrcVT = VT;
4946 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004947 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004948 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004949 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004950 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004951 }
4952
4953 // At this point the operands and the result should have the same
4954 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004955
4956 // First get the sign bit of second operand.
4957 std::vector<Constant*> CV;
4958 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004959 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4960 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004961 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004962 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4963 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4964 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4965 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004966 }
Dan Gohman11821702007-07-27 17:16:43 +00004967 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004968 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4969 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004970 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004971 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004972 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004973
4974 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004975 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004976 // Op0 is MVT::f32, Op1 is MVT::f64.
4977 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4978 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4979 DAG.getConstant(32, MVT::i32));
4980 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4981 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004982 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004983 }
4984
4985 // Clear first operand sign bit.
4986 CV.clear();
4987 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004988 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4989 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004990 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004991 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4992 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4993 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4994 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004995 }
Dan Gohman11821702007-07-27 17:16:43 +00004996 C = ConstantVector::get(CV);
4997 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004998 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004999 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005000 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00005001 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002
5003 // Or the value with the sign bit.
5004 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
5005}
5006
Dan Gohman8181bd12008-07-27 21:46:04 +00005007SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00005008 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00005009 SDValue Cond;
5010 SDValue Op0 = Op.getOperand(0);
5011 SDValue Op1 = Op.getOperand(1);
5012 SDValue CC = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00005013 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00005014 unsigned X86CC;
5015
Evan Cheng950aac02007-09-25 01:57:46 +00005016 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00005017 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00005018 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
5019 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00005020 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00005021 }
Evan Cheng950aac02007-09-25 01:57:46 +00005022
Evan Cheng71343822008-10-15 02:05:31 +00005023 assert(0 && "Illegal SetCC!");
5024 return SDValue();
Evan Cheng950aac02007-09-25 01:57:46 +00005025}
5026
Dan Gohman8181bd12008-07-27 21:46:04 +00005027SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5028 SDValue Cond;
5029 SDValue Op0 = Op.getOperand(0);
5030 SDValue Op1 = Op.getOperand(1);
5031 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00005032 MVT VT = Op.getValueType();
5033 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5034 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5035
5036 if (isFP) {
5037 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00005038 MVT VT0 = Op0.getValueType();
5039 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5040 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00005041 bool Swap = false;
5042
5043 switch (SetCCOpcode) {
5044 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005045 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00005046 case ISD::SETEQ: SSECC = 0; break;
5047 case ISD::SETOGT:
5048 case ISD::SETGT: Swap = true; // Fallthrough
5049 case ISD::SETLT:
5050 case ISD::SETOLT: SSECC = 1; break;
5051 case ISD::SETOGE:
5052 case ISD::SETGE: Swap = true; // Fallthrough
5053 case ISD::SETLE:
5054 case ISD::SETOLE: SSECC = 2; break;
5055 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005056 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00005057 case ISD::SETNE: SSECC = 4; break;
5058 case ISD::SETULE: Swap = true;
5059 case ISD::SETUGE: SSECC = 5; break;
5060 case ISD::SETULT: Swap = true;
5061 case ISD::SETUGT: SSECC = 6; break;
5062 case ISD::SETO: SSECC = 7; break;
5063 }
5064 if (Swap)
5065 std::swap(Op0, Op1);
5066
Nate Begeman6357f9d2008-07-25 19:05:58 +00005067 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00005068 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00005069 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005070 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005071 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5072 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5073 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
5074 }
5075 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005076 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005077 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5078 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5079 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
5080 }
5081 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00005082 }
5083 // Handle all other FP comparisons here.
5084 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
5085 }
5086
5087 // We are handling one of the integer comparisons here. Since SSE only has
5088 // GT and EQ comparisons for integer, swapping operands and multiple
5089 // operations may be required for some comparisons.
5090 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5091 bool Swap = false, Invert = false, FlipSigns = false;
5092
5093 switch (VT.getSimpleVT()) {
5094 default: break;
5095 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5096 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5097 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5098 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5099 }
5100
5101 switch (SetCCOpcode) {
5102 default: break;
5103 case ISD::SETNE: Invert = true;
5104 case ISD::SETEQ: Opc = EQOpc; break;
5105 case ISD::SETLT: Swap = true;
5106 case ISD::SETGT: Opc = GTOpc; break;
5107 case ISD::SETGE: Swap = true;
5108 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5109 case ISD::SETULT: Swap = true;
5110 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5111 case ISD::SETUGE: Swap = true;
5112 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5113 }
5114 if (Swap)
5115 std::swap(Op0, Op1);
5116
5117 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5118 // bits of the inputs before performing those operations.
5119 if (FlipSigns) {
5120 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005121 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
5122 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
5123 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005124 SignBits.size());
5125 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
5126 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
5127 }
5128
Dan Gohman8181bd12008-07-27 21:46:04 +00005129 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005130
5131 // If the logical-not of the result is required, perform that now.
5132 if (Invert) {
5133 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005134 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
5135 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
5136 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005137 NegOnes.size());
5138 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
5139 }
5140 return Result;
5141}
Evan Cheng950aac02007-09-25 01:57:46 +00005142
Dan Gohman8181bd12008-07-27 21:46:04 +00005143SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005144 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005145 SDValue Cond = Op.getOperand(0);
5146 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005147
5148 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005149 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005150
Evan Cheng50d37ab2007-10-08 22:16:29 +00005151 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5152 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005153 if (Cond.getOpcode() == X86ISD::SETCC) {
5154 CC = Cond.getOperand(0);
5155
Dan Gohman8181bd12008-07-27 21:46:04 +00005156 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005157 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005158 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005159
Evan Cheng50d37ab2007-10-08 22:16:29 +00005160 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005161 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005162 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005163 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005164
Evan Cheng621216e2007-09-29 00:00:36 +00005165 if ((Opc == X86ISD::CMP ||
5166 Opc == X86ISD::COMI ||
5167 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005168 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005169 addTest = false;
5170 }
5171 }
5172
5173 if (addTest) {
5174 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00005175 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005176 }
5177
Duncan Sands92c43912008-06-06 12:08:01 +00005178 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005179 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005180 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005181 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5182 // condition is true.
5183 Ops.push_back(Op.getOperand(2));
5184 Ops.push_back(Op.getOperand(1));
5185 Ops.push_back(CC);
5186 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00005187 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005188}
5189
Dan Gohman8181bd12008-07-27 21:46:04 +00005190SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005191 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005192 SDValue Chain = Op.getOperand(0);
5193 SDValue Cond = Op.getOperand(1);
5194 SDValue Dest = Op.getOperand(2);
5195 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005196
5197 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005198 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005199
Evan Cheng50d37ab2007-10-08 22:16:29 +00005200 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5201 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005202 if (Cond.getOpcode() == X86ISD::SETCC) {
5203 CC = Cond.getOperand(0);
5204
Dan Gohman8181bd12008-07-27 21:46:04 +00005205 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005206 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00005207 if (Opc == X86ISD::CMP ||
5208 Opc == X86ISD::COMI ||
5209 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005210 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005211 addTest = false;
5212 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005213 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5214 // two branches instead of an explicit OR instruction with a
5215 // separate test.
5216 } else if (Cond.getOpcode() == ISD::OR &&
5217 Cond.hasOneUse() &&
5218 Cond.getOperand(0).getOpcode() == X86ISD::SETCC &&
5219 Cond.getOperand(0).hasOneUse() &&
5220 Cond.getOperand(1).getOpcode() == X86ISD::SETCC &&
5221 Cond.getOperand(1).hasOneUse()) {
5222 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5223 unsigned Opc = Cmp.getOpcode();
5224 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5225 (Opc == X86ISD::CMP ||
5226 Opc == X86ISD::COMI ||
5227 Opc == X86ISD::UCOMI)) {
5228 CC = Cond.getOperand(0).getOperand(0);
5229 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5230 Chain, Dest, CC, Cmp);
5231 CC = Cond.getOperand(1).getOperand(0);
5232 Cond = Cmp;
5233 addTest = false;
5234 }
5235 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5236 // two branches instead of an explicit AND instruction with a
5237 // separate test. However, we only do this if this block doesn't
5238 // have a fall-through edge, because this requires an explicit
5239 // jmp when the condition is false.
5240 } else if (Cond.getOpcode() == ISD::AND &&
5241 Cond.hasOneUse() &&
5242 Cond.getOperand(0).getOpcode() == X86ISD::SETCC &&
5243 Cond.getOperand(0).hasOneUse() &&
5244 Cond.getOperand(1).getOpcode() == X86ISD::SETCC &&
5245 Cond.getOperand(1).hasOneUse()) {
5246 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5247 unsigned Opc = Cmp.getOpcode();
5248 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5249 (Opc == X86ISD::CMP ||
5250 Opc == X86ISD::COMI ||
5251 Opc == X86ISD::UCOMI) &&
5252 Op.getNode()->hasOneUse()) {
5253 X86::CondCode CCode =
5254 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5255 CCode = X86::GetOppositeBranchCondition(CCode);
5256 CC = DAG.getConstant(CCode, MVT::i8);
5257 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5258 // Look for an unconditional branch following this conditional branch.
5259 // We need this because we need to reverse the successors in order
5260 // to implement FCMP_OEQ.
5261 if (User.getOpcode() == ISD::BR) {
5262 SDValue FalseBB = User.getOperand(1);
5263 SDValue NewBR =
5264 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5265 assert(NewBR == User);
5266 Dest = FalseBB;
5267
5268 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5269 Chain, Dest, CC, Cmp);
5270 X86::CondCode CCode =
5271 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5272 CCode = X86::GetOppositeBranchCondition(CCode);
5273 CC = DAG.getConstant(CCode, MVT::i8);
5274 Cond = Cmp;
5275 addTest = false;
5276 }
5277 }
Evan Cheng950aac02007-09-25 01:57:46 +00005278 }
5279
5280 if (addTest) {
5281 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005282 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005283 }
Evan Cheng621216e2007-09-29 00:00:36 +00005284 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005285 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005286}
5287
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005288
5289// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5290// Calls to _alloca is needed to probe the stack when allocating more than 4k
5291// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5292// that the guard pages used by the OS virtual memory manager are allocated in
5293// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005294SDValue
5295X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005296 SelectionDAG &DAG) {
5297 assert(Subtarget->isTargetCygMing() &&
5298 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005299
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005300 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005301 SDValue Chain = Op.getOperand(0);
5302 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005303 // FIXME: Ensure alignment here
5304
Dan Gohman8181bd12008-07-27 21:46:04 +00005305 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005306
Duncan Sands92c43912008-06-06 12:08:01 +00005307 MVT IntPtr = getPointerTy();
5308 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005309
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005310 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005311
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005312 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5313 Flag = Chain.getValue(1);
5314
5315 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005316 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005317 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005318 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005319 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005320 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005321 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005322 Flag = Chain.getValue(1);
5323
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005324 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005325 DAG.getIntPtrConstant(0, true),
5326 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005327 Flag);
5328
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005329 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005330
Dan Gohman8181bd12008-07-27 21:46:04 +00005331 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005332 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005333}
5334
Dan Gohman8181bd12008-07-27 21:46:04 +00005335SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005336X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005337 SDValue Chain,
5338 SDValue Dst, SDValue Src,
5339 SDValue Size, unsigned Align,
5340 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005341 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005342 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005343
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005344 // If not DWORD aligned or size is more than the threshold, call the library.
5345 // The libc version is likely to be faster for these cases. It can use the
5346 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005347 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005348 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005349 ConstantSize->getZExtValue() >
5350 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005351 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005352
5353 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005354 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005355
Bill Wendling4b2e3782008-10-01 00:59:58 +00005356 if (const char *bzeroEntry = V &&
5357 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5358 MVT IntPtr = getPointerTy();
5359 const Type *IntPtrTy = TD->getIntPtrType();
5360 TargetLowering::ArgListTy Args;
5361 TargetLowering::ArgListEntry Entry;
5362 Entry.Node = Dst;
5363 Entry.Ty = IntPtrTy;
5364 Args.push_back(Entry);
5365 Entry.Node = Size;
5366 Args.push_back(Entry);
5367 std::pair<SDValue,SDValue> CallResult =
5368 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5369 CallingConv::C, false,
5370 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG);
5371 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005372 }
5373
Dan Gohmane8b391e2008-04-12 04:36:06 +00005374 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005375 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005376 }
5377
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005378 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005379 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005380 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005381 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005382 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005383 unsigned BytesLeft = 0;
5384 bool TwoRepStos = false;
5385 if (ValC) {
5386 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005387 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005388
5389 // If the value is a constant, then we can potentially use larger sets.
5390 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005391 case 2: // WORD aligned
5392 AVT = MVT::i16;
5393 ValReg = X86::AX;
5394 Val = (Val << 8) | Val;
5395 break;
5396 case 0: // DWORD aligned
5397 AVT = MVT::i32;
5398 ValReg = X86::EAX;
5399 Val = (Val << 8) | Val;
5400 Val = (Val << 16) | Val;
5401 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5402 AVT = MVT::i64;
5403 ValReg = X86::RAX;
5404 Val = (Val << 32) | Val;
5405 }
5406 break;
5407 default: // Byte aligned
5408 AVT = MVT::i8;
5409 ValReg = X86::AL;
5410 Count = DAG.getIntPtrConstant(SizeVal);
5411 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005412 }
5413
Duncan Sandsec142ee2008-06-08 20:54:56 +00005414 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005415 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005416 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5417 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005418 }
5419
5420 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5421 InFlag);
5422 InFlag = Chain.getValue(1);
5423 } else {
5424 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005425 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005426 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005427 InFlag = Chain.getValue(1);
5428 }
5429
5430 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5431 Count, InFlag);
5432 InFlag = Chain.getValue(1);
5433 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005434 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005435 InFlag = Chain.getValue(1);
5436
5437 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005438 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005439 Ops.push_back(Chain);
5440 Ops.push_back(DAG.getValueType(AVT));
5441 Ops.push_back(InFlag);
5442 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5443
5444 if (TwoRepStos) {
5445 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005446 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005447 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005448 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005449 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5450 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5451 Left, InFlag);
5452 InFlag = Chain.getValue(1);
5453 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5454 Ops.clear();
5455 Ops.push_back(Chain);
5456 Ops.push_back(DAG.getValueType(MVT::i8));
5457 Ops.push_back(InFlag);
5458 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5459 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005460 // Handle the last 1 - 7 bytes.
5461 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005462 MVT AddrVT = Dst.getValueType();
5463 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005464
5465 Chain = DAG.getMemset(Chain,
5466 DAG.getNode(ISD::ADD, AddrVT, Dst,
5467 DAG.getConstant(Offset, AddrVT)),
5468 Src,
5469 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005470 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005471 }
5472
Dan Gohmane8b391e2008-04-12 04:36:06 +00005473 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005474 return Chain;
5475}
5476
Dan Gohman8181bd12008-07-27 21:46:04 +00005477SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005478X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005479 SDValue Chain, SDValue Dst, SDValue Src,
5480 SDValue Size, unsigned Align,
5481 bool AlwaysInline,
5482 const Value *DstSV, uint64_t DstSVOff,
5483 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005484 // This requires the copy size to be a constant, preferrably
5485 // within a subtarget-specific limit.
5486 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5487 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005488 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005489 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005490 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005491 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005492
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005493 /// If not DWORD aligned, call the library.
5494 if ((Align & 3) != 0)
5495 return SDValue();
5496
5497 // DWORD aligned
5498 MVT AVT = MVT::i32;
5499 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005500 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005501
Duncan Sands92c43912008-06-06 12:08:01 +00005502 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005503 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005504 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005505 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005506
Dan Gohman8181bd12008-07-27 21:46:04 +00005507 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005508 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5509 Count, InFlag);
5510 InFlag = Chain.getValue(1);
5511 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005512 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005513 InFlag = Chain.getValue(1);
5514 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005515 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005516 InFlag = Chain.getValue(1);
5517
5518 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005519 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005520 Ops.push_back(Chain);
5521 Ops.push_back(DAG.getValueType(AVT));
5522 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005523 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005524
Dan Gohman8181bd12008-07-27 21:46:04 +00005525 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005526 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005527 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005528 // Handle the last 1 - 7 bytes.
5529 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005530 MVT DstVT = Dst.getValueType();
5531 MVT SrcVT = Src.getValueType();
5532 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005533 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005534 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005535 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005536 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005537 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005538 DAG.getConstant(BytesLeft, SizeVT),
5539 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005540 DstSV, DstSVOff + Offset,
5541 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005542 }
5543
Dan Gohmane8b391e2008-04-12 04:36:06 +00005544 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005545}
5546
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005547/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5548SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005549 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005550 SDValue TheChain = N->getOperand(0);
5551 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005552 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005553 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5554 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005555 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005556 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005557 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005558 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005559 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005560 };
5561
Gabor Greif1c80d112008-08-28 21:40:38 +00005562 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005563 }
5564
Dan Gohman8181bd12008-07-27 21:46:04 +00005565 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5566 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005567 MVT::i32, eax.getValue(2));
5568 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005569 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005570 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5571
5572 // Use a MERGE_VALUES to return the value and chain.
5573 Ops[1] = edx.getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00005574 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005575}
5576
Dan Gohman8181bd12008-07-27 21:46:04 +00005577SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005578 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005579
5580 if (!Subtarget->is64Bit()) {
5581 // vastart just stores the address of the VarArgsFrameIndex slot into the
5582 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005583 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005584 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005585 }
5586
5587 // __va_list_tag:
5588 // gp_offset (0 - 6 * 8)
5589 // fp_offset (48 - 48 + 8 * 16)
5590 // overflow_arg_area (point to parameters coming in memory).
5591 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005592 SmallVector<SDValue, 8> MemOps;
5593 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005594 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005595 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005596 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005597 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005598 MemOps.push_back(Store);
5599
5600 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005601 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005602 Store = DAG.getStore(Op.getOperand(0),
5603 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005604 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005605 MemOps.push_back(Store);
5606
5607 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005608 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005609 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005610 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005611 MemOps.push_back(Store);
5612
5613 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005614 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005615 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005616 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005617 MemOps.push_back(Store);
5618 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5619}
5620
Dan Gohman8181bd12008-07-27 21:46:04 +00005621SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005622 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5623 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005624 SDValue Chain = Op.getOperand(0);
5625 SDValue SrcPtr = Op.getOperand(1);
5626 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005627
5628 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5629 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005630 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005631}
5632
Dan Gohman8181bd12008-07-27 21:46:04 +00005633SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005634 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005635 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005636 SDValue Chain = Op.getOperand(0);
5637 SDValue DstPtr = Op.getOperand(1);
5638 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005639 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5640 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005641
Dan Gohman840ff5c2008-04-18 20:55:41 +00005642 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5643 DAG.getIntPtrConstant(24), 8, false,
5644 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005645}
5646
Dan Gohman8181bd12008-07-27 21:46:04 +00005647SDValue
5648X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005649 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005650 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005651 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005652 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005653 case Intrinsic::x86_sse_comieq_ss:
5654 case Intrinsic::x86_sse_comilt_ss:
5655 case Intrinsic::x86_sse_comile_ss:
5656 case Intrinsic::x86_sse_comigt_ss:
5657 case Intrinsic::x86_sse_comige_ss:
5658 case Intrinsic::x86_sse_comineq_ss:
5659 case Intrinsic::x86_sse_ucomieq_ss:
5660 case Intrinsic::x86_sse_ucomilt_ss:
5661 case Intrinsic::x86_sse_ucomile_ss:
5662 case Intrinsic::x86_sse_ucomigt_ss:
5663 case Intrinsic::x86_sse_ucomige_ss:
5664 case Intrinsic::x86_sse_ucomineq_ss:
5665 case Intrinsic::x86_sse2_comieq_sd:
5666 case Intrinsic::x86_sse2_comilt_sd:
5667 case Intrinsic::x86_sse2_comile_sd:
5668 case Intrinsic::x86_sse2_comigt_sd:
5669 case Intrinsic::x86_sse2_comige_sd:
5670 case Intrinsic::x86_sse2_comineq_sd:
5671 case Intrinsic::x86_sse2_ucomieq_sd:
5672 case Intrinsic::x86_sse2_ucomilt_sd:
5673 case Intrinsic::x86_sse2_ucomile_sd:
5674 case Intrinsic::x86_sse2_ucomigt_sd:
5675 case Intrinsic::x86_sse2_ucomige_sd:
5676 case Intrinsic::x86_sse2_ucomineq_sd: {
5677 unsigned Opc = 0;
5678 ISD::CondCode CC = ISD::SETCC_INVALID;
5679 switch (IntNo) {
5680 default: break;
5681 case Intrinsic::x86_sse_comieq_ss:
5682 case Intrinsic::x86_sse2_comieq_sd:
5683 Opc = X86ISD::COMI;
5684 CC = ISD::SETEQ;
5685 break;
5686 case Intrinsic::x86_sse_comilt_ss:
5687 case Intrinsic::x86_sse2_comilt_sd:
5688 Opc = X86ISD::COMI;
5689 CC = ISD::SETLT;
5690 break;
5691 case Intrinsic::x86_sse_comile_ss:
5692 case Intrinsic::x86_sse2_comile_sd:
5693 Opc = X86ISD::COMI;
5694 CC = ISD::SETLE;
5695 break;
5696 case Intrinsic::x86_sse_comigt_ss:
5697 case Intrinsic::x86_sse2_comigt_sd:
5698 Opc = X86ISD::COMI;
5699 CC = ISD::SETGT;
5700 break;
5701 case Intrinsic::x86_sse_comige_ss:
5702 case Intrinsic::x86_sse2_comige_sd:
5703 Opc = X86ISD::COMI;
5704 CC = ISD::SETGE;
5705 break;
5706 case Intrinsic::x86_sse_comineq_ss:
5707 case Intrinsic::x86_sse2_comineq_sd:
5708 Opc = X86ISD::COMI;
5709 CC = ISD::SETNE;
5710 break;
5711 case Intrinsic::x86_sse_ucomieq_ss:
5712 case Intrinsic::x86_sse2_ucomieq_sd:
5713 Opc = X86ISD::UCOMI;
5714 CC = ISD::SETEQ;
5715 break;
5716 case Intrinsic::x86_sse_ucomilt_ss:
5717 case Intrinsic::x86_sse2_ucomilt_sd:
5718 Opc = X86ISD::UCOMI;
5719 CC = ISD::SETLT;
5720 break;
5721 case Intrinsic::x86_sse_ucomile_ss:
5722 case Intrinsic::x86_sse2_ucomile_sd:
5723 Opc = X86ISD::UCOMI;
5724 CC = ISD::SETLE;
5725 break;
5726 case Intrinsic::x86_sse_ucomigt_ss:
5727 case Intrinsic::x86_sse2_ucomigt_sd:
5728 Opc = X86ISD::UCOMI;
5729 CC = ISD::SETGT;
5730 break;
5731 case Intrinsic::x86_sse_ucomige_ss:
5732 case Intrinsic::x86_sse2_ucomige_sd:
5733 Opc = X86ISD::UCOMI;
5734 CC = ISD::SETGE;
5735 break;
5736 case Intrinsic::x86_sse_ucomineq_ss:
5737 case Intrinsic::x86_sse2_ucomineq_sd:
5738 Opc = X86ISD::UCOMI;
5739 CC = ISD::SETNE;
5740 break;
5741 }
5742
5743 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005744 SDValue LHS = Op.getOperand(1);
5745 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005746 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5747
Dan Gohman8181bd12008-07-27 21:46:04 +00005748 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5749 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005750 DAG.getConstant(X86CC, MVT::i8), Cond);
5751 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005752 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005753
5754 // Fix vector shift instructions where the last operand is a non-immediate
5755 // i32 value.
5756 case Intrinsic::x86_sse2_pslli_w:
5757 case Intrinsic::x86_sse2_pslli_d:
5758 case Intrinsic::x86_sse2_pslli_q:
5759 case Intrinsic::x86_sse2_psrli_w:
5760 case Intrinsic::x86_sse2_psrli_d:
5761 case Intrinsic::x86_sse2_psrli_q:
5762 case Intrinsic::x86_sse2_psrai_w:
5763 case Intrinsic::x86_sse2_psrai_d:
5764 case Intrinsic::x86_mmx_pslli_w:
5765 case Intrinsic::x86_mmx_pslli_d:
5766 case Intrinsic::x86_mmx_pslli_q:
5767 case Intrinsic::x86_mmx_psrli_w:
5768 case Intrinsic::x86_mmx_psrli_d:
5769 case Intrinsic::x86_mmx_psrli_q:
5770 case Intrinsic::x86_mmx_psrai_w:
5771 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005772 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005773 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005774 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005775
5776 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005777 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005778 switch (IntNo) {
5779 case Intrinsic::x86_sse2_pslli_w:
5780 NewIntNo = Intrinsic::x86_sse2_psll_w;
5781 break;
5782 case Intrinsic::x86_sse2_pslli_d:
5783 NewIntNo = Intrinsic::x86_sse2_psll_d;
5784 break;
5785 case Intrinsic::x86_sse2_pslli_q:
5786 NewIntNo = Intrinsic::x86_sse2_psll_q;
5787 break;
5788 case Intrinsic::x86_sse2_psrli_w:
5789 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5790 break;
5791 case Intrinsic::x86_sse2_psrli_d:
5792 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5793 break;
5794 case Intrinsic::x86_sse2_psrli_q:
5795 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5796 break;
5797 case Intrinsic::x86_sse2_psrai_w:
5798 NewIntNo = Intrinsic::x86_sse2_psra_w;
5799 break;
5800 case Intrinsic::x86_sse2_psrai_d:
5801 NewIntNo = Intrinsic::x86_sse2_psra_d;
5802 break;
5803 default: {
5804 ShAmtVT = MVT::v2i32;
5805 switch (IntNo) {
5806 case Intrinsic::x86_mmx_pslli_w:
5807 NewIntNo = Intrinsic::x86_mmx_psll_w;
5808 break;
5809 case Intrinsic::x86_mmx_pslli_d:
5810 NewIntNo = Intrinsic::x86_mmx_psll_d;
5811 break;
5812 case Intrinsic::x86_mmx_pslli_q:
5813 NewIntNo = Intrinsic::x86_mmx_psll_q;
5814 break;
5815 case Intrinsic::x86_mmx_psrli_w:
5816 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5817 break;
5818 case Intrinsic::x86_mmx_psrli_d:
5819 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5820 break;
5821 case Intrinsic::x86_mmx_psrli_q:
5822 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5823 break;
5824 case Intrinsic::x86_mmx_psrai_w:
5825 NewIntNo = Intrinsic::x86_mmx_psra_w;
5826 break;
5827 case Intrinsic::x86_mmx_psrai_d:
5828 NewIntNo = Intrinsic::x86_mmx_psra_d;
5829 break;
5830 default: abort(); // Can't reach here.
5831 }
5832 break;
5833 }
5834 }
Duncan Sands92c43912008-06-06 12:08:01 +00005835 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005836 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5837 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5838 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5839 DAG.getConstant(NewIntNo, MVT::i32),
5840 Op.getOperand(1), ShAmt);
5841 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005842 }
5843}
5844
Dan Gohman8181bd12008-07-27 21:46:04 +00005845SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005846 // Depths > 0 not supported yet!
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005847 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005848 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005849
5850 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005851 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005852 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5853}
5854
Dan Gohman8181bd12008-07-27 21:46:04 +00005855SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00005856 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
5857 MFI->setFrameAddressIsTaken(true);
5858 MVT VT = Op.getValueType();
5859 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5860 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
5861 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), FrameReg, VT);
5862 while (Depth--)
5863 FrameAddr = DAG.getLoad(VT, DAG.getEntryNode(), FrameAddr, NULL, 0);
5864 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005865}
5866
Dan Gohman8181bd12008-07-27 21:46:04 +00005867SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005868 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005869 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005870}
5871
Dan Gohman8181bd12008-07-27 21:46:04 +00005872SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005873{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005874 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005875 SDValue Chain = Op.getOperand(0);
5876 SDValue Offset = Op.getOperand(1);
5877 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005878
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005879 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5880 getPointerTy());
5881 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005882
Dan Gohman8181bd12008-07-27 21:46:04 +00005883 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005884 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005885 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5886 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005887 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5888 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005889
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005890 return DAG.getNode(X86ISD::EH_RETURN,
5891 MVT::Other,
5892 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005893}
5894
Dan Gohman8181bd12008-07-27 21:46:04 +00005895SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005896 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005897 SDValue Root = Op.getOperand(0);
5898 SDValue Trmp = Op.getOperand(1); // trampoline
5899 SDValue FPtr = Op.getOperand(2); // nested function
5900 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005901
Dan Gohman12a9c082008-02-06 22:27:42 +00005902 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005903
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005904 const X86InstrInfo *TII =
5905 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5906
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005907 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005908 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005909
5910 // Large code-model.
5911
5912 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5913 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5914
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005915 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5916 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005917
5918 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5919
5920 // Load the pointer to the nested function into R11.
5921 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005922 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005923 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005924 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005925
5926 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005927 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005928
5929 // Load the 'nest' parameter value into R10.
5930 // R10 is specified in X86CallingConv.td
5931 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5932 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5933 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005934 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005935
5936 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005937 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005938
5939 // Jump to the nested function.
5940 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5941 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5942 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005943 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005944
5945 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5946 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5947 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005948 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005949
Dan Gohman8181bd12008-07-27 21:46:04 +00005950 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005951 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005952 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005953 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005954 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005955 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5956 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005957 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005958
5959 switch (CC) {
5960 default:
5961 assert(0 && "Unsupported calling convention");
5962 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005963 case CallingConv::X86_StdCall: {
5964 // Pass 'nest' parameter in ECX.
5965 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005966 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005967
5968 // Check that ECX wasn't needed by an 'inreg' parameter.
5969 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00005970 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005971
Chris Lattner1c8733e2008-03-12 17:45:29 +00005972 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005973 unsigned InRegCount = 0;
5974 unsigned Idx = 1;
5975
5976 for (FunctionType::param_iterator I = FTy->param_begin(),
5977 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00005978 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005979 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005980 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005981
5982 if (InRegCount > 2) {
5983 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5984 abort();
5985 }
5986 }
5987 break;
5988 }
5989 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00005990 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005991 // Pass 'nest' parameter in EAX.
5992 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005993 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005994 break;
5995 }
5996
Dan Gohman8181bd12008-07-27 21:46:04 +00005997 SDValue OutChains[4];
5998 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005999
6000 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
6001 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
6002
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006003 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006004 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00006005 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00006006 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006007
6008 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006009 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006010
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006011 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006012 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
6013 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006014 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006015
6016 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006017 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006018
Dan Gohman8181bd12008-07-27 21:46:04 +00006019 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00006020 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00006021 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006022 }
6023}
6024
Dan Gohman8181bd12008-07-27 21:46:04 +00006025SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006026 /*
6027 The rounding mode is in bits 11:10 of FPSR, and has the following
6028 settings:
6029 00 Round to nearest
6030 01 Round to -inf
6031 10 Round to +inf
6032 11 Round to 0
6033
6034 FLT_ROUNDS, on the other hand, expects the following:
6035 -1 Undefined
6036 0 Round to 0
6037 1 Round to nearest
6038 2 Round to +inf
6039 3 Round to -inf
6040
6041 To perform the conversion, we do:
6042 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6043 */
6044
6045 MachineFunction &MF = DAG.getMachineFunction();
6046 const TargetMachine &TM = MF.getTarget();
6047 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6048 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00006049 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006050
6051 // Save FP Control Word to stack slot
6052 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00006053 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006054
Dan Gohman8181bd12008-07-27 21:46:04 +00006055 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00006056 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006057
6058 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00006059 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006060
6061 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00006062 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006063 DAG.getNode(ISD::SRL, MVT::i16,
6064 DAG.getNode(ISD::AND, MVT::i16,
6065 CWD, DAG.getConstant(0x800, MVT::i16)),
6066 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006067 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006068 DAG.getNode(ISD::SRL, MVT::i16,
6069 DAG.getNode(ISD::AND, MVT::i16,
6070 CWD, DAG.getConstant(0x400, MVT::i16)),
6071 DAG.getConstant(9, MVT::i8));
6072
Dan Gohman8181bd12008-07-27 21:46:04 +00006073 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006074 DAG.getNode(ISD::AND, MVT::i16,
6075 DAG.getNode(ISD::ADD, MVT::i16,
6076 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
6077 DAG.getConstant(1, MVT::i16)),
6078 DAG.getConstant(3, MVT::i16));
6079
6080
Duncan Sands92c43912008-06-06 12:08:01 +00006081 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006082 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6083}
6084
Dan Gohman8181bd12008-07-27 21:46:04 +00006085SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006086 MVT VT = Op.getValueType();
6087 MVT OpVT = VT;
6088 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006089
6090 Op = Op.getOperand(0);
6091 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006092 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00006093 OpVT = MVT::i32;
6094 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6095 }
Evan Cheng48679f42007-12-14 02:13:44 +00006096
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006097 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6098 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6099 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
6100
6101 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006102 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006103 Ops.push_back(Op);
6104 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6105 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6106 Ops.push_back(Op.getValue(1));
6107 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6108
6109 // Finally xor with NumBits-1.
6110 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
6111
Evan Cheng48679f42007-12-14 02:13:44 +00006112 if (VT == MVT::i8)
6113 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6114 return Op;
6115}
6116
Dan Gohman8181bd12008-07-27 21:46:04 +00006117SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006118 MVT VT = Op.getValueType();
6119 MVT OpVT = VT;
6120 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006121
6122 Op = Op.getOperand(0);
6123 if (VT == MVT::i8) {
6124 OpVT = MVT::i32;
6125 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6126 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006127
6128 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6129 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6130 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
6131
6132 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006133 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006134 Ops.push_back(Op);
6135 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6136 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6137 Ops.push_back(Op.getValue(1));
6138 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6139
Evan Cheng48679f42007-12-14 02:13:44 +00006140 if (VT == MVT::i8)
6141 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6142 return Op;
6143}
6144
Dan Gohman8181bd12008-07-27 21:46:04 +00006145SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006146 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00006147 unsigned Reg = 0;
6148 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006149 switch(T.getSimpleVT()) {
6150 default:
6151 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006152 case MVT::i8: Reg = X86::AL; size = 1; break;
6153 case MVT::i16: Reg = X86::AX; size = 2; break;
6154 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00006155 case MVT::i64:
6156 if (Subtarget->is64Bit()) {
6157 Reg = X86::RAX; size = 8;
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006158 } else //Should go away when LegalizeType stuff lands
Gabor Greif1c80d112008-08-28 21:40:38 +00006159 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00006160 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006161 };
Dan Gohman8181bd12008-07-27 21:46:04 +00006162 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00006163 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00006164 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006165 Op.getOperand(1),
6166 Op.getOperand(3),
6167 DAG.getTargetConstant(size, MVT::i8),
6168 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006169 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006170 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
6171 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006172 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
6173 return cpOut;
6174}
6175
Gabor Greif825aa892008-08-28 23:19:51 +00006176SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
6177 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006178 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00006179 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00006180 SDValue cpInL, cpInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00006181 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00006182 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00006183 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00006184 DAG.getConstant(1, MVT::i32));
6185 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00006186 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00006187 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
6188 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00006189 SDValue swapInL, swapInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00006190 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00006191 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00006192 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00006193 DAG.getConstant(1, MVT::i32));
6194 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
6195 swapInL, cpInH.getValue(1));
6196 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
6197 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00006198 SDValue Ops[] = { swapInH.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006199 Op->getOperand(1),
6200 swapInH.getValue(1) };
Andrew Lenharth81580822008-03-05 01:15:49 +00006201 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006202 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
6203 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00006204 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00006205 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00006206 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00006207 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
6208 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
6209 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greif1c80d112008-08-28 21:40:38 +00006210 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharth81580822008-03-05 01:15:49 +00006211}
6212
Dale Johannesenf160d802008-10-02 18:53:47 +00006213SDValue X86TargetLowering::LowerATOMIC_BINARY_64(SDValue Op,
6214 SelectionDAG &DAG,
6215 unsigned NewOp) {
6216 SDNode *Node = Op.getNode();
6217 MVT T = Node->getValueType(0);
6218 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6219
6220 SDValue Chain = Node->getOperand(0);
6221 SDValue In1 = Node->getOperand(1);
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006222 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6223 Node->getOperand(2), DAG.getIntPtrConstant(0));
6224 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6225 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dale Johannesen44eb5372008-10-03 19:41:08 +00006226 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6227 // have a MemOperand. Pass the info through as a normal operand.
6228 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6229 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
Dale Johannesenf160d802008-10-02 18:53:47 +00006230 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006231 SDValue Result = DAG.getNode(NewOp, Tys, Ops, 5);
Dale Johannesenf160d802008-10-02 18:53:47 +00006232 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
6233 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
6234 SDValue Vals[2] = { ResultVal, Result.getValue(2) };
6235 return SDValue(DAG.getMergeValues(Vals, 2).getNode(), 0);
6236}
6237
Dale Johannesen9011d872008-09-29 22:25:26 +00006238SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6239 SDNode *Node = Op.getNode();
6240 MVT T = Node->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006241 SDValue negOp = DAG.getNode(ISD::SUB, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006242 DAG.getConstant(0, T), Node->getOperand(2));
6243 return DAG.getAtomic((Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_8 ?
6244 ISD::ATOMIC_LOAD_ADD_8 :
6245 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_16 ?
6246 ISD::ATOMIC_LOAD_ADD_16 :
6247 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_32 ?
6248 ISD::ATOMIC_LOAD_ADD_32 :
6249 ISD::ATOMIC_LOAD_ADD_64),
6250 Node->getOperand(0),
6251 Node->getOperand(1), negOp,
6252 cast<AtomicSDNode>(Node)->getSrcValue(),
6253 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006254}
6255
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006256/// LowerOperation - Provide custom lowering hooks for some operations.
6257///
Dan Gohman8181bd12008-07-27 21:46:04 +00006258SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006259 switch (Op.getOpcode()) {
6260 default: assert(0 && "Should not custom lower this!");
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006261 case ISD::ATOMIC_CMP_SWAP_8:
6262 case ISD::ATOMIC_CMP_SWAP_16:
6263 case ISD::ATOMIC_CMP_SWAP_32:
Dale Johannesenbc187662008-08-28 02:44:49 +00006264 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006265 case ISD::ATOMIC_LOAD_SUB_8:
6266 case ISD::ATOMIC_LOAD_SUB_16:
Dale Johannesen9011d872008-09-29 22:25:26 +00006267 case ISD::ATOMIC_LOAD_SUB_32: return LowerLOAD_SUB(Op,DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006268 case ISD::ATOMIC_LOAD_SUB_64: return (Subtarget->is64Bit()) ?
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006269 LowerLOAD_SUB(Op,DAG) :
6270 LowerATOMIC_BINARY_64(Op,DAG,
Dale Johannesenf160d802008-10-02 18:53:47 +00006271 X86ISD::ATOMSUB64_DAG);
6272 case ISD::ATOMIC_LOAD_AND_64: return LowerATOMIC_BINARY_64(Op,DAG,
6273 X86ISD::ATOMAND64_DAG);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006274 case ISD::ATOMIC_LOAD_OR_64: return LowerATOMIC_BINARY_64(Op, DAG,
Dale Johannesenf160d802008-10-02 18:53:47 +00006275 X86ISD::ATOMOR64_DAG);
6276 case ISD::ATOMIC_LOAD_XOR_64: return LowerATOMIC_BINARY_64(Op,DAG,
6277 X86ISD::ATOMXOR64_DAG);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006278 case ISD::ATOMIC_LOAD_NAND_64:return LowerATOMIC_BINARY_64(Op,DAG,
Dale Johannesenf160d802008-10-02 18:53:47 +00006279 X86ISD::ATOMNAND64_DAG);
6280 case ISD::ATOMIC_LOAD_ADD_64: return LowerATOMIC_BINARY_64(Op,DAG,
6281 X86ISD::ATOMADD64_DAG);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006282 case ISD::ATOMIC_SWAP_64: return LowerATOMIC_BINARY_64(Op,DAG,
6283 X86ISD::ATOMSWAP64_DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006284 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6285 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6286 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6287 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6288 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6289 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6290 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6291 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006292 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006293 case ISD::SHL_PARTS:
6294 case ISD::SRA_PARTS:
6295 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6296 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00006297 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006298 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6299 case ISD::FABS: return LowerFABS(Op, DAG);
6300 case ISD::FNEG: return LowerFNEG(Op, DAG);
6301 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006302 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006303 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006304 case ISD::SELECT: return LowerSELECT(Op, DAG);
6305 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006306 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6307 case ISD::CALL: return LowerCALL(Op, DAG);
6308 case ISD::RET: return LowerRET(Op, DAG);
6309 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006310 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006311 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006312 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6313 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6314 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6315 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6316 case ISD::FRAME_TO_ARGS_OFFSET:
6317 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6318 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6319 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006320 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006321 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006322 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6323 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006324
6325 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6326 case ISD::READCYCLECOUNTER:
Gabor Greif1c80d112008-08-28 21:40:38 +00006327 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006328 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006329}
6330
Duncan Sandsac496a12008-07-04 11:47:58 +00006331/// ReplaceNodeResults - Replace a node with an illegal result type
6332/// with a new node built out of custom code.
6333SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006334 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006335 default:
6336 return X86TargetLowering::LowerOperation(SDValue (N, 0), DAG).getNode();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006337 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6338 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesenbc187662008-08-28 02:44:49 +00006339 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006340 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006341}
6342
6343const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6344 switch (Opcode) {
6345 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006346 case X86ISD::BSF: return "X86ISD::BSF";
6347 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006348 case X86ISD::SHLD: return "X86ISD::SHLD";
6349 case X86ISD::SHRD: return "X86ISD::SHRD";
6350 case X86ISD::FAND: return "X86ISD::FAND";
6351 case X86ISD::FOR: return "X86ISD::FOR";
6352 case X86ISD::FXOR: return "X86ISD::FXOR";
6353 case X86ISD::FSRL: return "X86ISD::FSRL";
6354 case X86ISD::FILD: return "X86ISD::FILD";
6355 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6356 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6357 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6358 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6359 case X86ISD::FLD: return "X86ISD::FLD";
6360 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006361 case X86ISD::CALL: return "X86ISD::CALL";
6362 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6363 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6364 case X86ISD::CMP: return "X86ISD::CMP";
6365 case X86ISD::COMI: return "X86ISD::COMI";
6366 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6367 case X86ISD::SETCC: return "X86ISD::SETCC";
6368 case X86ISD::CMOV: return "X86ISD::CMOV";
6369 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6370 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6371 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6372 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006373 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6374 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006375 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006376 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006377 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6378 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006379 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6380 case X86ISD::FMAX: return "X86ISD::FMAX";
6381 case X86ISD::FMIN: return "X86ISD::FMIN";
6382 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6383 case X86ISD::FRCP: return "X86ISD::FRCP";
6384 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6385 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6386 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006387 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006388 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006389 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6390 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006391 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6392 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6393 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6394 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6395 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6396 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006397 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6398 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006399 case X86ISD::VSHL: return "X86ISD::VSHL";
6400 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006401 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6402 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6403 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6404 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6405 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6406 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6407 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6408 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6409 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6410 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006411 }
6412}
6413
6414// isLegalAddressingMode - Return true if the addressing mode represented
6415// by AM is legal for this target, for a load/store of the specified type.
6416bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6417 const Type *Ty) const {
6418 // X86 supports extremely general addressing modes.
6419
6420 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6421 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6422 return false;
6423
6424 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006425 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006426 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6427 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006428
6429 // X86-64 only supports addr of globals in small code model.
6430 if (Subtarget->is64Bit()) {
6431 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6432 return false;
6433 // If lower 4G is not available, then we must use rip-relative addressing.
6434 if (AM.BaseOffs || AM.Scale > 1)
6435 return false;
6436 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006437 }
6438
6439 switch (AM.Scale) {
6440 case 0:
6441 case 1:
6442 case 2:
6443 case 4:
6444 case 8:
6445 // These scales always work.
6446 break;
6447 case 3:
6448 case 5:
6449 case 9:
6450 // These scales are formed with basereg+scalereg. Only accept if there is
6451 // no basereg yet.
6452 if (AM.HasBaseReg)
6453 return false;
6454 break;
6455 default: // Other stuff never works.
6456 return false;
6457 }
6458
6459 return true;
6460}
6461
6462
Evan Cheng27a820a2007-10-26 01:56:11 +00006463bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6464 if (!Ty1->isInteger() || !Ty2->isInteger())
6465 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006466 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6467 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006468 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006469 return false;
6470 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006471}
6472
Duncan Sands92c43912008-06-06 12:08:01 +00006473bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6474 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006475 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006476 unsigned NumBits1 = VT1.getSizeInBits();
6477 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006478 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006479 return false;
6480 return Subtarget->is64Bit() || NumBits1 < 64;
6481}
Evan Cheng27a820a2007-10-26 01:56:11 +00006482
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006483/// isShuffleMaskLegal - Targets can use this to indicate that they only
6484/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6485/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6486/// are assumed to be legal.
6487bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006488X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006489 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006490 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006491 return (Mask.getNode()->getNumOperands() <= 4 ||
6492 isIdentityMask(Mask.getNode()) ||
6493 isIdentityMask(Mask.getNode(), true) ||
6494 isSplatMask(Mask.getNode()) ||
6495 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6496 X86::isUNPCKLMask(Mask.getNode()) ||
6497 X86::isUNPCKHMask(Mask.getNode()) ||
6498 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6499 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006500}
6501
Dan Gohman48d5f062008-04-09 20:09:42 +00006502bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006503X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006504 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006505 unsigned NumElts = BVOps.size();
6506 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006507 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006508 if (NumElts == 2) return true;
6509 if (NumElts == 4) {
6510 return (isMOVLMask(&BVOps[0], 4) ||
6511 isCommutedMOVL(&BVOps[0], 4, true) ||
6512 isSHUFPMask(&BVOps[0], 4) ||
6513 isCommutedSHUFP(&BVOps[0], 4));
6514 }
6515 return false;
6516}
6517
6518//===----------------------------------------------------------------------===//
6519// X86 Scheduler Hooks
6520//===----------------------------------------------------------------------===//
6521
Mon P Wang078a62d2008-05-05 19:05:59 +00006522// private utility function
6523MachineBasicBlock *
6524X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6525 MachineBasicBlock *MBB,
6526 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006527 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006528 unsigned LoadOpc,
6529 unsigned CXchgOpc,
6530 unsigned copyOpc,
6531 unsigned notOpc,
6532 unsigned EAXreg,
6533 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006534 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006535 // For the atomic bitwise operator, we generate
6536 // thisMBB:
6537 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006538 // ld t1 = [bitinstr.addr]
6539 // op t2 = t1, [bitinstr.val]
6540 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006541 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6542 // bz newMBB
6543 // fallthrough -->nextMBB
6544 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6545 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006546 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006547 ++MBBIter;
6548
6549 /// First build the CFG
6550 MachineFunction *F = MBB->getParent();
6551 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006552 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6553 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6554 F->insert(MBBIter, newMBB);
6555 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006556
6557 // Move all successors to thisMBB to nextMBB
6558 nextMBB->transferSuccessors(thisMBB);
6559
6560 // Update thisMBB to fall through to newMBB
6561 thisMBB->addSuccessor(newMBB);
6562
6563 // newMBB jumps to itself and fall through to nextMBB
6564 newMBB->addSuccessor(nextMBB);
6565 newMBB->addSuccessor(newMBB);
6566
6567 // Insert instructions into newMBB based on incoming instruction
6568 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6569 MachineOperand& destOper = bInstr->getOperand(0);
6570 MachineOperand* argOpers[6];
6571 int numArgs = bInstr->getNumOperands() - 1;
6572 for (int i=0; i < numArgs; ++i)
6573 argOpers[i] = &bInstr->getOperand(i+1);
6574
6575 // x86 address has 4 operands: base, index, scale, and displacement
6576 int lastAddrIndx = 3; // [0,3]
6577 int valArgIndx = 4;
6578
Dale Johannesend20e4452008-08-19 18:47:28 +00006579 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6580 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006581 for (int i=0; i <= lastAddrIndx; ++i)
6582 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006583
Dale Johannesend20e4452008-08-19 18:47:28 +00006584 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006585 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006586 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006587 }
6588 else
6589 tt = t1;
6590
Dale Johannesend20e4452008-08-19 18:47:28 +00006591 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006592 assert((argOpers[valArgIndx]->isReg() ||
6593 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006594 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006595 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006596 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6597 else
6598 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006599 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006600 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006601
Dale Johannesend20e4452008-08-19 18:47:28 +00006602 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006603 MIB.addReg(t1);
6604
Dale Johannesend20e4452008-08-19 18:47:28 +00006605 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006606 for (int i=0; i <= lastAddrIndx; ++i)
6607 (*MIB).addOperand(*argOpers[i]);
6608 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006609 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6610 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6611
Dale Johannesend20e4452008-08-19 18:47:28 +00006612 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6613 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006614
6615 // insert branch
6616 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6617
Dan Gohman221a4372008-07-07 23:14:23 +00006618 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006619 return nextMBB;
6620}
6621
Dale Johannesen44eb5372008-10-03 19:41:08 +00006622// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00006623MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00006624X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
6625 MachineBasicBlock *MBB,
6626 unsigned regOpcL,
6627 unsigned regOpcH,
6628 unsigned immOpcL,
6629 unsigned immOpcH,
6630 bool invSrc) {
6631 // For the atomic bitwise operator, we generate
6632 // thisMBB (instructions are in pairs, except cmpxchg8b)
6633 // ld t1,t2 = [bitinstr.addr]
6634 // newMBB:
6635 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
6636 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006637 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00006638 // mov ECX, EBX <- t5, t6
6639 // mov EAX, EDX <- t1, t2
6640 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
6641 // mov t3, t4 <- EAX, EDX
6642 // bz newMBB
6643 // result in out1, out2
6644 // fallthrough -->nextMBB
6645
6646 const TargetRegisterClass *RC = X86::GR32RegisterClass;
6647 const unsigned LoadOpc = X86::MOV32rm;
6648 const unsigned copyOpc = X86::MOV32rr;
6649 const unsigned NotOpc = X86::NOT32r;
6650 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6651 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6652 MachineFunction::iterator MBBIter = MBB;
6653 ++MBBIter;
6654
6655 /// First build the CFG
6656 MachineFunction *F = MBB->getParent();
6657 MachineBasicBlock *thisMBB = MBB;
6658 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6659 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6660 F->insert(MBBIter, newMBB);
6661 F->insert(MBBIter, nextMBB);
6662
6663 // Move all successors to thisMBB to nextMBB
6664 nextMBB->transferSuccessors(thisMBB);
6665
6666 // Update thisMBB to fall through to newMBB
6667 thisMBB->addSuccessor(newMBB);
6668
6669 // newMBB jumps to itself and fall through to nextMBB
6670 newMBB->addSuccessor(nextMBB);
6671 newMBB->addSuccessor(newMBB);
6672
6673 // Insert instructions into newMBB based on incoming instruction
6674 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
6675 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
6676 MachineOperand& dest1Oper = bInstr->getOperand(0);
6677 MachineOperand& dest2Oper = bInstr->getOperand(1);
6678 MachineOperand* argOpers[6];
6679 for (int i=0; i < 6; ++i)
6680 argOpers[i] = &bInstr->getOperand(i+2);
6681
6682 // x86 address has 4 operands: base, index, scale, and displacement
6683 int lastAddrIndx = 3; // [0,3]
6684
6685 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6686 MachineInstrBuilder MIB = BuildMI(thisMBB, TII->get(LoadOpc), t1);
6687 for (int i=0; i <= lastAddrIndx; ++i)
6688 (*MIB).addOperand(*argOpers[i]);
6689 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
6690 MIB = BuildMI(thisMBB, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006691 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00006692 for (int i=0; i <= lastAddrIndx-1; ++i)
6693 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006694 MachineOperand newOp3 = *(argOpers[3]);
6695 if (newOp3.isImm())
6696 newOp3.setImm(newOp3.getImm()+4);
6697 else
6698 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00006699 (*MIB).addOperand(newOp3);
6700
6701 // t3/4 are defined later, at the bottom of the loop
6702 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
6703 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
6704 BuildMI(newMBB, TII->get(X86::PHI), dest1Oper.getReg())
6705 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
6706 BuildMI(newMBB, TII->get(X86::PHI), dest2Oper.getReg())
6707 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
6708
6709 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
6710 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
6711 if (invSrc) {
6712 MIB = BuildMI(newMBB, TII->get(NotOpc), tt1).addReg(t1);
6713 MIB = BuildMI(newMBB, TII->get(NotOpc), tt2).addReg(t2);
6714 } else {
6715 tt1 = t1;
6716 tt2 = t2;
6717 }
6718
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006719 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00006720 "invalid operand");
6721 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
6722 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006723 if (argOpers[4]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006724 MIB = BuildMI(newMBB, TII->get(regOpcL), t5);
6725 else
6726 MIB = BuildMI(newMBB, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006727 if (regOpcL != X86::MOV32rr)
6728 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00006729 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006730 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
6731 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
6732 if (argOpers[5]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006733 MIB = BuildMI(newMBB, TII->get(regOpcH), t6);
6734 else
6735 MIB = BuildMI(newMBB, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006736 if (regOpcH != X86::MOV32rr)
6737 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006738 (*MIB).addOperand(*argOpers[5]);
6739
6740 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EAX);
6741 MIB.addReg(t1);
6742 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EDX);
6743 MIB.addReg(t2);
6744
6745 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EBX);
6746 MIB.addReg(t5);
6747 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::ECX);
6748 MIB.addReg(t6);
6749
6750 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG8B));
6751 for (int i=0; i <= lastAddrIndx; ++i)
6752 (*MIB).addOperand(*argOpers[i]);
6753
6754 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6755 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6756
6757 MIB = BuildMI(newMBB, TII->get(copyOpc), t3);
6758 MIB.addReg(X86::EAX);
6759 MIB = BuildMI(newMBB, TII->get(copyOpc), t4);
6760 MIB.addReg(X86::EDX);
6761
6762 // insert branch
6763 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6764
6765 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
6766 return nextMBB;
6767}
6768
6769// private utility function
6770MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00006771X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6772 MachineBasicBlock *MBB,
6773 unsigned cmovOpc) {
6774 // For the atomic min/max operator, we generate
6775 // thisMBB:
6776 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006777 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006778 // mov t2 = [min/max.val]
6779 // cmp t1, t2
6780 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006781 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006782 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6783 // bz newMBB
6784 // fallthrough -->nextMBB
6785 //
6786 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6787 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006788 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006789 ++MBBIter;
6790
6791 /// First build the CFG
6792 MachineFunction *F = MBB->getParent();
6793 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006794 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6795 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6796 F->insert(MBBIter, newMBB);
6797 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006798
6799 // Move all successors to thisMBB to nextMBB
6800 nextMBB->transferSuccessors(thisMBB);
6801
6802 // Update thisMBB to fall through to newMBB
6803 thisMBB->addSuccessor(newMBB);
6804
6805 // newMBB jumps to newMBB and fall through to nextMBB
6806 newMBB->addSuccessor(nextMBB);
6807 newMBB->addSuccessor(newMBB);
6808
6809 // Insert instructions into newMBB based on incoming instruction
6810 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6811 MachineOperand& destOper = mInstr->getOperand(0);
6812 MachineOperand* argOpers[6];
6813 int numArgs = mInstr->getNumOperands() - 1;
6814 for (int i=0; i < numArgs; ++i)
6815 argOpers[i] = &mInstr->getOperand(i+1);
6816
6817 // x86 address has 4 operands: base, index, scale, and displacement
6818 int lastAddrIndx = 3; // [0,3]
6819 int valArgIndx = 4;
6820
Mon P Wang318b0372008-05-05 22:56:23 +00006821 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6822 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006823 for (int i=0; i <= lastAddrIndx; ++i)
6824 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006825
Mon P Wang078a62d2008-05-05 19:05:59 +00006826 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006827 assert((argOpers[valArgIndx]->isReg() ||
6828 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006829 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00006830
6831 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006832 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006833 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6834 else
6835 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6836 (*MIB).addOperand(*argOpers[valArgIndx]);
6837
Mon P Wang318b0372008-05-05 22:56:23 +00006838 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6839 MIB.addReg(t1);
6840
Mon P Wang078a62d2008-05-05 19:05:59 +00006841 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6842 MIB.addReg(t1);
6843 MIB.addReg(t2);
6844
6845 // Generate movc
6846 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6847 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6848 MIB.addReg(t2);
6849 MIB.addReg(t1);
6850
6851 // Cmp and exchange if none has modified the memory location
6852 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6853 for (int i=0; i <= lastAddrIndx; ++i)
6854 (*MIB).addOperand(*argOpers[i]);
6855 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006856 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6857 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006858
6859 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6860 MIB.addReg(X86::EAX);
6861
6862 // insert branch
6863 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6864
Dan Gohman221a4372008-07-07 23:14:23 +00006865 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006866 return nextMBB;
6867}
6868
6869
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006870MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006871X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6872 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006873 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6874 switch (MI->getOpcode()) {
6875 default: assert(false && "Unexpected instr type to insert");
6876 case X86::CMOV_FR32:
6877 case X86::CMOV_FR64:
6878 case X86::CMOV_V4F32:
6879 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006880 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006881 // To "insert" a SELECT_CC instruction, we actually have to insert the
6882 // diamond control-flow pattern. The incoming instruction knows the
6883 // destination vreg to set, the condition code register to branch on, the
6884 // true/false values to select between, and a branch opcode to use.
6885 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006886 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006887 ++It;
6888
6889 // thisMBB:
6890 // ...
6891 // TrueVal = ...
6892 // cmpTY ccX, r1, r2
6893 // bCC copy1MBB
6894 // fallthrough --> copy0MBB
6895 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006896 MachineFunction *F = BB->getParent();
6897 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6898 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006899 unsigned Opc =
6900 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6901 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006902 F->insert(It, copy0MBB);
6903 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006904 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006905 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006906 sinkMBB->transferSuccessors(BB);
6907
6908 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006909 BB->addSuccessor(copy0MBB);
6910 BB->addSuccessor(sinkMBB);
6911
6912 // copy0MBB:
6913 // %FalseValue = ...
6914 // # fallthrough to sinkMBB
6915 BB = copy0MBB;
6916
6917 // Update machine-CFG edges
6918 BB->addSuccessor(sinkMBB);
6919
6920 // sinkMBB:
6921 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6922 // ...
6923 BB = sinkMBB;
6924 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6925 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6926 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6927
Dan Gohman221a4372008-07-07 23:14:23 +00006928 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006929 return BB;
6930 }
6931
6932 case X86::FP32_TO_INT16_IN_MEM:
6933 case X86::FP32_TO_INT32_IN_MEM:
6934 case X86::FP32_TO_INT64_IN_MEM:
6935 case X86::FP64_TO_INT16_IN_MEM:
6936 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006937 case X86::FP64_TO_INT64_IN_MEM:
6938 case X86::FP80_TO_INT16_IN_MEM:
6939 case X86::FP80_TO_INT32_IN_MEM:
6940 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006941 // Change the floating point control register to use "round towards zero"
6942 // mode when truncating to an integer value.
6943 MachineFunction *F = BB->getParent();
6944 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6945 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6946
6947 // Load the old value of the high byte of the control word...
6948 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006949 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006950 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6951
6952 // Set the high part to be round to zero...
6953 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6954 .addImm(0xC7F);
6955
6956 // Reload the modified control word now...
6957 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6958
6959 // Restore the memory image of control word to original value
6960 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6961 .addReg(OldCW);
6962
6963 // Get the X86 opcode to use.
6964 unsigned Opc;
6965 switch (MI->getOpcode()) {
6966 default: assert(0 && "illegal opcode!");
6967 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6968 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6969 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6970 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6971 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6972 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006973 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6974 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6975 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006976 }
6977
6978 X86AddressMode AM;
6979 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006980 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006981 AM.BaseType = X86AddressMode::RegBase;
6982 AM.Base.Reg = Op.getReg();
6983 } else {
6984 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006985 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006986 }
6987 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006988 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006989 AM.Scale = Op.getImm();
6990 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006991 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006992 AM.IndexReg = Op.getImm();
6993 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006994 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006995 AM.GV = Op.getGlobal();
6996 } else {
6997 AM.Disp = Op.getImm();
6998 }
6999 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
7000 .addReg(MI->getOperand(4).getReg());
7001
7002 // Reload the original control word now.
7003 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7004
Dan Gohman221a4372008-07-07 23:14:23 +00007005 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007006 return BB;
7007 }
Mon P Wang078a62d2008-05-05 19:05:59 +00007008 case X86::ATOMAND32:
7009 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007010 X86::AND32ri, X86::MOV32rm,
7011 X86::LCMPXCHG32, X86::MOV32rr,
7012 X86::NOT32r, X86::EAX,
7013 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007014 case X86::ATOMOR32:
7015 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007016 X86::OR32ri, X86::MOV32rm,
7017 X86::LCMPXCHG32, X86::MOV32rr,
7018 X86::NOT32r, X86::EAX,
7019 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007020 case X86::ATOMXOR32:
7021 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007022 X86::XOR32ri, X86::MOV32rm,
7023 X86::LCMPXCHG32, X86::MOV32rr,
7024 X86::NOT32r, X86::EAX,
7025 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007026 case X86::ATOMNAND32:
7027 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007028 X86::AND32ri, X86::MOV32rm,
7029 X86::LCMPXCHG32, X86::MOV32rr,
7030 X86::NOT32r, X86::EAX,
7031 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00007032 case X86::ATOMMIN32:
7033 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7034 case X86::ATOMMAX32:
7035 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7036 case X86::ATOMUMIN32:
7037 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7038 case X86::ATOMUMAX32:
7039 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00007040
7041 case X86::ATOMAND16:
7042 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7043 X86::AND16ri, X86::MOV16rm,
7044 X86::LCMPXCHG16, X86::MOV16rr,
7045 X86::NOT16r, X86::AX,
7046 X86::GR16RegisterClass);
7047 case X86::ATOMOR16:
7048 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7049 X86::OR16ri, X86::MOV16rm,
7050 X86::LCMPXCHG16, X86::MOV16rr,
7051 X86::NOT16r, X86::AX,
7052 X86::GR16RegisterClass);
7053 case X86::ATOMXOR16:
7054 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7055 X86::XOR16ri, X86::MOV16rm,
7056 X86::LCMPXCHG16, X86::MOV16rr,
7057 X86::NOT16r, X86::AX,
7058 X86::GR16RegisterClass);
7059 case X86::ATOMNAND16:
7060 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7061 X86::AND16ri, X86::MOV16rm,
7062 X86::LCMPXCHG16, X86::MOV16rr,
7063 X86::NOT16r, X86::AX,
7064 X86::GR16RegisterClass, true);
7065 case X86::ATOMMIN16:
7066 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7067 case X86::ATOMMAX16:
7068 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7069 case X86::ATOMUMIN16:
7070 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7071 case X86::ATOMUMAX16:
7072 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7073
7074 case X86::ATOMAND8:
7075 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7076 X86::AND8ri, X86::MOV8rm,
7077 X86::LCMPXCHG8, X86::MOV8rr,
7078 X86::NOT8r, X86::AL,
7079 X86::GR8RegisterClass);
7080 case X86::ATOMOR8:
7081 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7082 X86::OR8ri, X86::MOV8rm,
7083 X86::LCMPXCHG8, X86::MOV8rr,
7084 X86::NOT8r, X86::AL,
7085 X86::GR8RegisterClass);
7086 case X86::ATOMXOR8:
7087 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7088 X86::XOR8ri, X86::MOV8rm,
7089 X86::LCMPXCHG8, X86::MOV8rr,
7090 X86::NOT8r, X86::AL,
7091 X86::GR8RegisterClass);
7092 case X86::ATOMNAND8:
7093 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7094 X86::AND8ri, X86::MOV8rm,
7095 X86::LCMPXCHG8, X86::MOV8rr,
7096 X86::NOT8r, X86::AL,
7097 X86::GR8RegisterClass, true);
7098 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00007099 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007100 case X86::ATOMAND64:
7101 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7102 X86::AND64ri32, X86::MOV64rm,
7103 X86::LCMPXCHG64, X86::MOV64rr,
7104 X86::NOT64r, X86::RAX,
7105 X86::GR64RegisterClass);
7106 case X86::ATOMOR64:
7107 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7108 X86::OR64ri32, X86::MOV64rm,
7109 X86::LCMPXCHG64, X86::MOV64rr,
7110 X86::NOT64r, X86::RAX,
7111 X86::GR64RegisterClass);
7112 case X86::ATOMXOR64:
7113 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7114 X86::XOR64ri32, X86::MOV64rm,
7115 X86::LCMPXCHG64, X86::MOV64rr,
7116 X86::NOT64r, X86::RAX,
7117 X86::GR64RegisterClass);
7118 case X86::ATOMNAND64:
7119 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7120 X86::AND64ri32, X86::MOV64rm,
7121 X86::LCMPXCHG64, X86::MOV64rr,
7122 X86::NOT64r, X86::RAX,
7123 X86::GR64RegisterClass, true);
7124 case X86::ATOMMIN64:
7125 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7126 case X86::ATOMMAX64:
7127 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7128 case X86::ATOMUMIN64:
7129 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7130 case X86::ATOMUMAX64:
7131 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00007132
7133 // This group does 64-bit operations on a 32-bit host.
7134 case X86::ATOMAND6432:
7135 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7136 X86::AND32rr, X86::AND32rr,
7137 X86::AND32ri, X86::AND32ri,
7138 false);
7139 case X86::ATOMOR6432:
7140 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7141 X86::OR32rr, X86::OR32rr,
7142 X86::OR32ri, X86::OR32ri,
7143 false);
7144 case X86::ATOMXOR6432:
7145 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7146 X86::XOR32rr, X86::XOR32rr,
7147 X86::XOR32ri, X86::XOR32ri,
7148 false);
7149 case X86::ATOMNAND6432:
7150 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7151 X86::AND32rr, X86::AND32rr,
7152 X86::AND32ri, X86::AND32ri,
7153 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00007154 case X86::ATOMADD6432:
7155 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7156 X86::ADD32rr, X86::ADC32rr,
7157 X86::ADD32ri, X86::ADC32ri,
7158 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00007159 case X86::ATOMSUB6432:
7160 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7161 X86::SUB32rr, X86::SBB32rr,
7162 X86::SUB32ri, X86::SBB32ri,
7163 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007164 case X86::ATOMSWAP6432:
7165 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7166 X86::MOV32rr, X86::MOV32rr,
7167 X86::MOV32ri, X86::MOV32ri,
7168 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007169 }
7170}
7171
7172//===----------------------------------------------------------------------===//
7173// X86 Optimization Hooks
7174//===----------------------------------------------------------------------===//
7175
Dan Gohman8181bd12008-07-27 21:46:04 +00007176void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00007177 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00007178 APInt &KnownZero,
7179 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007180 const SelectionDAG &DAG,
7181 unsigned Depth) const {
7182 unsigned Opc = Op.getOpcode();
7183 assert((Opc >= ISD::BUILTIN_OP_END ||
7184 Opc == ISD::INTRINSIC_WO_CHAIN ||
7185 Opc == ISD::INTRINSIC_W_CHAIN ||
7186 Opc == ISD::INTRINSIC_VOID) &&
7187 "Should use MaskedValueIsZero if you don't know whether Op"
7188 " is a target node!");
7189
Dan Gohman1d79e432008-02-13 23:07:24 +00007190 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007191 switch (Opc) {
7192 default: break;
7193 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007194 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7195 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007196 break;
7197 }
7198}
7199
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007200/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007201/// node is a GlobalAddress + offset.
7202bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7203 GlobalValue* &GA, int64_t &Offset) const{
7204 if (N->getOpcode() == X86ISD::Wrapper) {
7205 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007206 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00007207 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007208 return true;
7209 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007210 }
Evan Chengef7be082008-05-12 19:56:52 +00007211 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007212}
7213
Evan Chengef7be082008-05-12 19:56:52 +00007214static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7215 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007216 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007217 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007218 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007219 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007220 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007221 return false;
7222}
7223
Dan Gohman8181bd12008-07-27 21:46:04 +00007224static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007225 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007226 SDNode *&Base,
7227 SelectionDAG &DAG, MachineFrameInfo *MFI,
7228 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007229 Base = NULL;
7230 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007231 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007232 if (Idx.getOpcode() == ISD::UNDEF) {
7233 if (!Base)
7234 return false;
7235 continue;
7236 }
7237
Dan Gohman8181bd12008-07-27 21:46:04 +00007238 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007239 if (!Elt.getNode() ||
7240 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007241 return false;
7242 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007243 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007244 if (Base->getOpcode() == ISD::UNDEF)
7245 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007246 continue;
7247 }
7248 if (Elt.getOpcode() == ISD::UNDEF)
7249 continue;
7250
Gabor Greif1c80d112008-08-28 21:40:38 +00007251 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007252 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007253 return false;
7254 }
7255 return true;
7256}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007257
7258/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7259/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7260/// if the load addresses are consecutive, non-overlapping, and in the right
7261/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007262static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007263 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007264 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00007265 MVT VT = N->getValueType(0);
7266 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007267 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007268 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007269 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007270 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7271 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007272 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007273
Dan Gohman11821702007-07-27 17:16:43 +00007274 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007275 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007276 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00007277 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00007278 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
7279 LD->getSrcValueOffset(), LD->isVolatile(),
7280 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007281}
7282
Evan Chengb6290462008-05-12 23:04:07 +00007283/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007284static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng6617eed2008-09-24 23:26:36 +00007285 const X86Subtarget *Subtarget,
7286 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007287 unsigned NumOps = N->getNumOperands();
7288
Evan Chenge9b9c672008-05-09 21:53:03 +00007289 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007290 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007291 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007292
Duncan Sands92c43912008-06-06 12:08:01 +00007293 MVT VT = N->getValueType(0);
7294 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007295 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7296 // We are looking for load i64 and zero extend. We want to transform
7297 // it before legalizer has a chance to expand it. Also look for i64
7298 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007299 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007300 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007301 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007302 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007303 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007304
7305 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007306 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007307 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007308 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007309 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007310 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007311 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007312 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007313 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007314
7315 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007316 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007317
7318 // Load must not be an extload.
7319 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007320 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00007321
Evan Cheng6617eed2008-09-24 23:26:36 +00007322 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7323 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7324 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
7325 DAG.ReplaceAllUsesOfValueWith(SDValue(Base, 1), ResNode.getValue(1));
7326 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007327}
7328
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007329/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007330static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007331 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007332 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007333
7334 // If we have SSE[12] support, try to form min/max nodes.
7335 if (Subtarget->hasSSE2() &&
7336 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7337 if (Cond.getOpcode() == ISD::SETCC) {
7338 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007339 SDValue LHS = N->getOperand(1);
7340 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007341 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7342
7343 unsigned Opcode = 0;
7344 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7345 switch (CC) {
7346 default: break;
7347 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7348 case ISD::SETULE:
7349 case ISD::SETLE:
7350 if (!UnsafeFPMath) break;
7351 // FALL THROUGH.
7352 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7353 case ISD::SETLT:
7354 Opcode = X86ISD::FMIN;
7355 break;
7356
7357 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7358 case ISD::SETUGT:
7359 case ISD::SETGT:
7360 if (!UnsafeFPMath) break;
7361 // FALL THROUGH.
7362 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7363 case ISD::SETGE:
7364 Opcode = X86ISD::FMAX;
7365 break;
7366 }
7367 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7368 switch (CC) {
7369 default: break;
7370 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7371 case ISD::SETUGT:
7372 case ISD::SETGT:
7373 if (!UnsafeFPMath) break;
7374 // FALL THROUGH.
7375 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7376 case ISD::SETGE:
7377 Opcode = X86ISD::FMIN;
7378 break;
7379
7380 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7381 case ISD::SETULE:
7382 case ISD::SETLE:
7383 if (!UnsafeFPMath) break;
7384 // FALL THROUGH.
7385 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7386 case ISD::SETLT:
7387 Opcode = X86ISD::FMAX;
7388 break;
7389 }
7390 }
7391
7392 if (Opcode)
7393 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
7394 }
7395
7396 }
7397
Dan Gohman8181bd12008-07-27 21:46:04 +00007398 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007399}
7400
Chris Lattnerce84ae42008-02-22 02:09:43 +00007401/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007402static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007403 const X86Subtarget *Subtarget) {
7404 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7405 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007406 // A preferable solution to the general problem is to figure out the right
7407 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007408 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007409 if (St->getValue().getValueType().isVector() &&
7410 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007411 isa<LoadSDNode>(St->getValue()) &&
7412 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7413 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007414 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007415 LoadSDNode *Ld = 0;
7416 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007417 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007418 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007419 // Must be a store of a load. We currently handle two cases: the load
7420 // is a direct child, and it's under an intervening TokenFactor. It is
7421 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007422 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007423 Ld = cast<LoadSDNode>(St->getChain());
7424 else if (St->getValue().hasOneUse() &&
7425 ChainVal->getOpcode() == ISD::TokenFactor) {
7426 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007427 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007428 TokenFactorIndex = i;
7429 Ld = cast<LoadSDNode>(St->getValue());
7430 } else
7431 Ops.push_back(ChainVal->getOperand(i));
7432 }
7433 }
7434 if (Ld) {
7435 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7436 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007437 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007438 Ld->getBasePtr(), Ld->getSrcValue(),
7439 Ld->getSrcValueOffset(), Ld->isVolatile(),
7440 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007441 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007442 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00007443 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00007444 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7445 Ops.size());
7446 }
7447 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
7448 St->getSrcValue(), St->getSrcValueOffset(),
7449 St->isVolatile(), St->getAlignment());
7450 }
7451
7452 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00007453 SDValue LoAddr = Ld->getBasePtr();
7454 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007455 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007456
Dan Gohman8181bd12008-07-27 21:46:04 +00007457 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007458 Ld->getSrcValue(), Ld->getSrcValueOffset(),
7459 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007460 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007461 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
7462 Ld->isVolatile(),
7463 MinAlign(Ld->getAlignment(), 4));
7464
Dan Gohman8181bd12008-07-27 21:46:04 +00007465 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007466 if (TokenFactorIndex != -1) {
7467 Ops.push_back(LoLd);
7468 Ops.push_back(HiLd);
7469 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7470 Ops.size());
7471 }
7472
7473 LoAddr = St->getBasePtr();
7474 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007475 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007476
Dan Gohman8181bd12008-07-27 21:46:04 +00007477 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007478 St->getSrcValue(), St->getSrcValueOffset(),
7479 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007480 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00007481 St->getSrcValue(),
7482 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00007483 St->isVolatile(),
7484 MinAlign(St->getAlignment(), 4));
7485 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00007486 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00007487 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007488 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00007489}
7490
Chris Lattner470d5dc2008-01-25 06:14:17 +00007491/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7492/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007493static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00007494 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7495 // F[X]OR(0.0, x) -> x
7496 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00007497 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7498 if (C->getValueAPF().isPosZero())
7499 return N->getOperand(1);
7500 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7501 if (C->getValueAPF().isPosZero())
7502 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007503 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007504}
7505
7506/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007507static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007508 // FAND(0.0, x) -> 0.0
7509 // FAND(x, 0.0) -> 0.0
7510 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7511 if (C->getValueAPF().isPosZero())
7512 return N->getOperand(0);
7513 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7514 if (C->getValueAPF().isPosZero())
7515 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007516 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007517}
7518
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007519
Dan Gohman8181bd12008-07-27 21:46:04 +00007520SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00007521 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007522 SelectionDAG &DAG = DCI.DAG;
7523 switch (N->getOpcode()) {
7524 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007525 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7526 case ISD::BUILD_VECTOR:
7527 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007528 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007529 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007530 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007531 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7532 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007533 }
7534
Dan Gohman8181bd12008-07-27 21:46:04 +00007535 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007536}
7537
7538//===----------------------------------------------------------------------===//
7539// X86 Inline Assembly Support
7540//===----------------------------------------------------------------------===//
7541
7542/// getConstraintType - Given a constraint letter, return the type of
7543/// constraint it is for this target.
7544X86TargetLowering::ConstraintType
7545X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7546 if (Constraint.size() == 1) {
7547 switch (Constraint[0]) {
7548 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +00007549 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +00007550 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007551 case 'r':
7552 case 'R':
7553 case 'l':
7554 case 'q':
7555 case 'Q':
7556 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007557 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007558 case 'Y':
7559 return C_RegisterClass;
7560 default:
7561 break;
7562 }
7563 }
7564 return TargetLowering::getConstraintType(Constraint);
7565}
7566
Dale Johannesene99fc902008-01-29 02:21:21 +00007567/// LowerXConstraint - try to replace an X constraint, which matches anything,
7568/// with another that has more specific requirements based on the type of the
7569/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007570const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007571LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007572 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7573 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007574 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007575 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007576 return "Y";
7577 if (Subtarget->hasSSE1())
7578 return "x";
7579 }
7580
7581 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007582}
7583
Chris Lattnera531abc2007-08-25 00:47:38 +00007584/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7585/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007586void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007587 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00007588 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00007589 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007590 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007591 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007592
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007593 switch (Constraint) {
7594 default: break;
7595 case 'I':
7596 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007597 if (C->getZExtValue() <= 31) {
7598 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007599 break;
7600 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007601 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007602 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00007603 case 'J':
7604 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
7605 if (C->getZExtValue() <= 63) {
7606 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
7607 break;
7608 }
7609 }
7610 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007611 case 'N':
7612 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007613 if (C->getZExtValue() <= 255) {
7614 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007615 break;
7616 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007617 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007618 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007619 case 'i': {
7620 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007621 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007622 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007623 break;
7624 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007625
7626 // If we are in non-pic codegen mode, we allow the address of a global (with
7627 // an optional displacement) to be used with 'i'.
7628 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7629 int64_t Offset = 0;
7630
7631 // Match either (GA) or (GA+C)
7632 if (GA) {
7633 Offset = GA->getOffset();
7634 } else if (Op.getOpcode() == ISD::ADD) {
7635 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7636 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7637 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007638 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007639 } else {
7640 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7641 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7642 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007643 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007644 else
7645 C = 0, GA = 0;
7646 }
7647 }
7648
7649 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00007650 if (hasMemory)
Dan Gohman36322c72008-10-18 02:06:02 +00007651 Op = LowerGlobalAddress(GA->getGlobal(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00007652 else
7653 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7654 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007655 Result = Op;
7656 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007657 }
7658
7659 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007660 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007661 }
7662 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007663
Gabor Greif1c80d112008-08-28 21:40:38 +00007664 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007665 Ops.push_back(Result);
7666 return;
7667 }
Evan Cheng7f250d62008-09-24 00:05:32 +00007668 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
7669 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007670}
7671
7672std::vector<unsigned> X86TargetLowering::
7673getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007674 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007675 if (Constraint.size() == 1) {
7676 // FIXME: not handling fp-stack yet!
7677 switch (Constraint[0]) { // GCC X86 Constraint Letters
7678 default: break; // Unknown constraint letter
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007679 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7680 case 'Q': // Q_REGS
7681 if (VT == MVT::i32)
7682 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7683 else if (VT == MVT::i16)
7684 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7685 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007686 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007687 else if (VT == MVT::i64)
7688 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7689 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007690 }
7691 }
7692
7693 return std::vector<unsigned>();
7694}
7695
7696std::pair<unsigned, const TargetRegisterClass*>
7697X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007698 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007699 // First, see if this is a constraint that directly corresponds to an LLVM
7700 // register class.
7701 if (Constraint.size() == 1) {
7702 // GCC Constraint Letters
7703 switch (Constraint[0]) {
7704 default: break;
7705 case 'r': // GENERAL_REGS
7706 case 'R': // LEGACY_REGS
7707 case 'l': // INDEX_REGS
Chris Lattnerbbfea052008-10-17 18:15:05 +00007708 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007709 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00007710 if (VT == MVT::i16)
7711 return std::make_pair(0U, X86::GR16RegisterClass);
7712 if (VT == MVT::i32 || !Subtarget->is64Bit())
7713 return std::make_pair(0U, X86::GR32RegisterClass);
7714 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00007715 case 'f': // FP Stack registers.
7716 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7717 // value to the correct fpstack register class.
7718 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7719 return std::make_pair(0U, X86::RFP32RegisterClass);
7720 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7721 return std::make_pair(0U, X86::RFP64RegisterClass);
7722 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007723 case 'y': // MMX_REGS if MMX allowed.
7724 if (!Subtarget->hasMMX()) break;
7725 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007726 case 'Y': // SSE_REGS if SSE2 allowed
7727 if (!Subtarget->hasSSE2()) break;
7728 // FALL THROUGH.
7729 case 'x': // SSE_REGS if SSE1 allowed
7730 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007731
7732 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007733 default: break;
7734 // Scalar SSE types.
7735 case MVT::f32:
7736 case MVT::i32:
7737 return std::make_pair(0U, X86::FR32RegisterClass);
7738 case MVT::f64:
7739 case MVT::i64:
7740 return std::make_pair(0U, X86::FR64RegisterClass);
7741 // Vector types.
7742 case MVT::v16i8:
7743 case MVT::v8i16:
7744 case MVT::v4i32:
7745 case MVT::v2i64:
7746 case MVT::v4f32:
7747 case MVT::v2f64:
7748 return std::make_pair(0U, X86::VR128RegisterClass);
7749 }
7750 break;
7751 }
7752 }
7753
7754 // Use the default implementation in TargetLowering to convert the register
7755 // constraint into a member of a register class.
7756 std::pair<unsigned, const TargetRegisterClass*> Res;
7757 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7758
7759 // Not found as a standard register?
7760 if (Res.second == 0) {
7761 // GCC calls "st(0)" just plain "st".
7762 if (StringsEqualNoCase("{st}", Constraint)) {
7763 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007764 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007765 }
Dale Johannesen73920c02008-11-13 21:52:36 +00007766 // 'A' means EAX + EDX.
7767 if (Constraint == "A") {
7768 Res.first = X86::EAX;
7769 Res.second = X86::GRADRegisterClass;
7770 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007771 return Res;
7772 }
7773
7774 // Otherwise, check to see if this is a register class of the wrong value
7775 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7776 // turn into {ax},{dx}.
7777 if (Res.second->hasType(VT))
7778 return Res; // Correct type already, nothing to do.
7779
7780 // All of the single-register GCC register classes map their values onto
7781 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7782 // really want an 8-bit or 32-bit register, map to the appropriate register
7783 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007784 if (Res.second == X86::GR16RegisterClass) {
7785 if (VT == MVT::i8) {
7786 unsigned DestReg = 0;
7787 switch (Res.first) {
7788 default: break;
7789 case X86::AX: DestReg = X86::AL; break;
7790 case X86::DX: DestReg = X86::DL; break;
7791 case X86::CX: DestReg = X86::CL; break;
7792 case X86::BX: DestReg = X86::BL; break;
7793 }
7794 if (DestReg) {
7795 Res.first = DestReg;
7796 Res.second = Res.second = X86::GR8RegisterClass;
7797 }
7798 } else if (VT == MVT::i32) {
7799 unsigned DestReg = 0;
7800 switch (Res.first) {
7801 default: break;
7802 case X86::AX: DestReg = X86::EAX; break;
7803 case X86::DX: DestReg = X86::EDX; break;
7804 case X86::CX: DestReg = X86::ECX; break;
7805 case X86::BX: DestReg = X86::EBX; break;
7806 case X86::SI: DestReg = X86::ESI; break;
7807 case X86::DI: DestReg = X86::EDI; break;
7808 case X86::BP: DestReg = X86::EBP; break;
7809 case X86::SP: DestReg = X86::ESP; break;
7810 }
7811 if (DestReg) {
7812 Res.first = DestReg;
7813 Res.second = Res.second = X86::GR32RegisterClass;
7814 }
7815 } else if (VT == MVT::i64) {
7816 unsigned DestReg = 0;
7817 switch (Res.first) {
7818 default: break;
7819 case X86::AX: DestReg = X86::RAX; break;
7820 case X86::DX: DestReg = X86::RDX; break;
7821 case X86::CX: DestReg = X86::RCX; break;
7822 case X86::BX: DestReg = X86::RBX; break;
7823 case X86::SI: DestReg = X86::RSI; break;
7824 case X86::DI: DestReg = X86::RDI; break;
7825 case X86::BP: DestReg = X86::RBP; break;
7826 case X86::SP: DestReg = X86::RSP; break;
7827 }
7828 if (DestReg) {
7829 Res.first = DestReg;
7830 Res.second = Res.second = X86::GR64RegisterClass;
7831 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007832 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007833 } else if (Res.second == X86::FR32RegisterClass ||
7834 Res.second == X86::FR64RegisterClass ||
7835 Res.second == X86::VR128RegisterClass) {
7836 // Handle references to XMM physical registers that got mapped into the
7837 // wrong class. This can happen with constraints like {xmm0} where the
7838 // target independent register mapper will just pick the first match it can
7839 // find, ignoring the required type.
7840 if (VT == MVT::f32)
7841 Res.second = X86::FR32RegisterClass;
7842 else if (VT == MVT::f64)
7843 Res.second = X86::FR64RegisterClass;
7844 else if (X86::VR128RegisterClass->hasType(VT))
7845 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007846 }
7847
7848 return Res;
7849}
Mon P Wang1448aad2008-10-30 08:01:45 +00007850
7851//===----------------------------------------------------------------------===//
7852// X86 Widen vector type
7853//===----------------------------------------------------------------------===//
7854
7855/// getWidenVectorType: given a vector type, returns the type to widen
7856/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
7857/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +00007858/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +00007859/// scalarizing vs using the wider vector type.
7860
7861MVT X86TargetLowering::getWidenVectorType(MVT VT) {
7862 assert(VT.isVector());
7863 if (isTypeLegal(VT))
7864 return VT;
7865
7866 // TODO: In computeRegisterProperty, we can compute the list of legal vector
7867 // type based on element type. This would speed up our search (though
7868 // it may not be worth it since the size of the list is relatively
7869 // small).
7870 MVT EltVT = VT.getVectorElementType();
7871 unsigned NElts = VT.getVectorNumElements();
7872
7873 // On X86, it make sense to widen any vector wider than 1
7874 if (NElts <= 1)
7875 return MVT::Other;
7876
7877 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
7878 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
7879 MVT SVT = (MVT::SimpleValueType)nVT;
7880
7881 if (isTypeLegal(SVT) &&
7882 SVT.getVectorElementType() == EltVT &&
7883 SVT.getVectorNumElements() > NElts)
7884 return SVT;
7885 }
7886 return MVT::Other;
7887}