blob: a9a2def0a67d68b0134f6d04d828df31bc6cb0e0 [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);
Mon P Wang83edba52008-12-12 01:25:51 +0000653
654 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
655 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
656 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
657 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
658 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
659 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 }
661
662 if (Subtarget->hasSSE1()) {
663 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
664
665 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
666 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
667 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
668 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
669 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
670 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
672 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
673 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
674 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
675 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000676 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 }
678
679 if (Subtarget->hasSSE2()) {
680 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
681 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
682 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
683 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
684 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
685
686 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
687 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
688 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
689 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
690 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
691 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
692 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
693 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
694 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
695 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
696 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
697 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
698 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
699 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
700 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701
Nate Begeman03605a02008-07-17 16:51:19 +0000702 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
703 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
704 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
705 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000706
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
708 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
709 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
710 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
712
713 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000714 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
715 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000716 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000717 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000718 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000719 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
720 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
721 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000722 }
723 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
724 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
725 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
726 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000727 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000728 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000729 if (Subtarget->is64Bit()) {
730 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000731 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000732 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000733
734 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
735 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000736 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
737 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
738 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
739 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
740 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
741 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
742 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
743 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
744 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
745 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000746 }
747
Chris Lattner3bc08502008-01-17 19:59:44 +0000748 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000749
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750 // Custom lower v2i64 and v2f64 selects.
751 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
752 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
753 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
754 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000755
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000757
758 if (Subtarget->hasSSE41()) {
759 // FIXME: Do we need to handle scalar-to-vector here?
760 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000761 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000762
763 // i8 and i16 vectors are custom , because the source register and source
764 // source memory operand types are not the same width. f32 vectors are
765 // custom since the immediate controlling the insert encodes additional
766 // information.
767 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
768 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
769 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
770 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
771
772 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
773 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
774 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000775 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000776
777 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000778 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
779 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000780 }
781 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782
Nate Begeman03605a02008-07-17 16:51:19 +0000783 if (Subtarget->hasSSE42()) {
784 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
785 }
786
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787 // We want to custom lower some of our intrinsics.
788 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
789
Bill Wendling7e04be62008-12-09 22:08:41 +0000790 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling4c134df2008-11-24 19:21:46 +0000791 setOperationAction(ISD::SADDO, MVT::i32, Custom);
792 setOperationAction(ISD::SADDO, MVT::i64, Custom);
793 setOperationAction(ISD::UADDO, MVT::i32, Custom);
794 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling7e04be62008-12-09 22:08:41 +0000795 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
796 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
797 setOperationAction(ISD::USUBO, MVT::i32, Custom);
798 setOperationAction(ISD::USUBO, MVT::i64, Custom);
799 setOperationAction(ISD::SMULO, MVT::i32, Custom);
800 setOperationAction(ISD::SMULO, MVT::i64, Custom);
801 setOperationAction(ISD::UMULO, MVT::i32, Custom);
802 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Bill Wendling4c134df2008-11-24 19:21:46 +0000803
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 // We have target-specific dag combine patterns for the following nodes:
805 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000806 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000808 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809
810 computeRegisterProperties();
811
812 // FIXME: These should be based on subtarget info. Plus, the values should
813 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000814 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
815 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
816 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000818 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819}
820
Scott Michel502151f2008-03-10 15:42:14 +0000821
Dan Gohman8181bd12008-07-27 21:46:04 +0000822MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000823 return MVT::i8;
824}
825
826
Evan Cheng5a67b812008-01-23 23:17:41 +0000827/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
828/// the desired ByVal argument alignment.
829static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
830 if (MaxAlign == 16)
831 return;
832 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
833 if (VTy->getBitWidth() == 128)
834 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000835 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
836 unsigned EltAlign = 0;
837 getMaxByValAlign(ATy->getElementType(), EltAlign);
838 if (EltAlign > MaxAlign)
839 MaxAlign = EltAlign;
840 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
841 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
842 unsigned EltAlign = 0;
843 getMaxByValAlign(STy->getElementType(i), EltAlign);
844 if (EltAlign > MaxAlign)
845 MaxAlign = EltAlign;
846 if (MaxAlign == 16)
847 break;
848 }
849 }
850 return;
851}
852
853/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
854/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000855/// that contain SSE vectors are placed at 16-byte boundaries while the rest
856/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000857unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000858 if (Subtarget->is64Bit()) {
859 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000860 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000861 if (TyAlign > 8)
862 return TyAlign;
863 return 8;
864 }
865
Evan Cheng5a67b812008-01-23 23:17:41 +0000866 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000867 if (Subtarget->hasSSE1())
868 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000869 return Align;
870}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000871
Evan Cheng8c590372008-05-15 08:39:06 +0000872/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000873/// and store operations as a result of memset, memcpy, and memmove
874/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000875/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000876MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000877X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
878 bool isSrcConst, bool isSrcStr) const {
Chris Lattnerf0bf1062008-10-28 05:49:35 +0000879 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
880 // linux. This is because the stack realignment code can't handle certain
881 // cases like PR2962. This should be removed when PR2962 is fixed.
882 if (Subtarget->getStackAlignment() >= 16) {
883 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
884 return MVT::v4i32;
885 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
886 return MVT::v4f32;
887 }
Evan Cheng8c590372008-05-15 08:39:06 +0000888 if (Subtarget->is64Bit() && Size >= 8)
889 return MVT::i64;
890 return MVT::i32;
891}
892
893
Evan Cheng6fb06762007-11-09 01:32:10 +0000894/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
895/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000896SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000897 SelectionDAG &DAG) const {
898 if (usesGlobalOffsetTable())
899 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
900 if (!Subtarget->isPICStyleRIPRel())
901 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
902 return Table;
903}
904
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000905//===----------------------------------------------------------------------===//
906// Return Value Calling Convention Implementation
907//===----------------------------------------------------------------------===//
908
909#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000910
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000912SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
914
915 SmallVector<CCValAssign, 16> RVLocs;
916 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
917 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
918 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000919 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000920
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 // If this is the first return lowered for this function, add the regs to the
922 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000923 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000924 for (unsigned i = 0; i != RVLocs.size(); ++i)
925 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000926 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000927 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000928 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000930 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000931 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000932 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000933 SDValue TailCall = Chain;
934 SDValue TargetAddress = TailCall.getOperand(1);
935 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000936 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000937 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000938 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000939 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000940 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
941 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000942 assert(StackAdjustment.getOpcode() == ISD::Constant &&
943 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000944
Dan Gohman8181bd12008-07-27 21:46:04 +0000945 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000946 Operands.push_back(Chain.getOperand(0));
947 Operands.push_back(TargetAddress);
948 Operands.push_back(StackAdjustment);
949 // Copy registers used by the call. Last operand is a flag so it is not
950 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000951 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000952 Operands.push_back(Chain.getOperand(i));
953 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000954 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
955 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000956 }
957
958 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000959 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000960
Dan Gohman8181bd12008-07-27 21:46:04 +0000961 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000962 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
963 // Operand #1 = Bytes To Pop
964 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
965
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000967 for (unsigned i = 0; i != RVLocs.size(); ++i) {
968 CCValAssign &VA = RVLocs[i];
969 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000970 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971
Chris Lattnerb56cc342008-03-11 03:23:40 +0000972 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
973 // the RET instruction and handled by the FP Stackifier.
974 if (RVLocs[i].getLocReg() == X86::ST0 ||
975 RVLocs[i].getLocReg() == X86::ST1) {
976 // If this is a copy from an xmm register to ST(0), use an FPExtend to
977 // change the value to the FP stack register class.
978 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
979 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
980 RetOps.push_back(ValToCopy);
981 // Don't emit a copytoreg.
982 continue;
983 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000984
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000985 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986 Flag = Chain.getValue(1);
987 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000988
989 // The x86-64 ABI for returning structs by value requires that we copy
990 // the sret argument into %rax for the return. We saved the argument into
991 // a virtual register in the entry block, so now we copy the value out
992 // and into %rax.
993 if (Subtarget->is64Bit() &&
994 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
995 MachineFunction &MF = DAG.getMachineFunction();
996 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
997 unsigned Reg = FuncInfo->getSRetReturnReg();
998 if (!Reg) {
999 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1000 FuncInfo->setSRetReturnReg(Reg);
1001 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001002 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001003
1004 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
1005 Flag = Chain.getValue(1);
1006 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001007
Chris Lattnerb56cc342008-03-11 03:23:40 +00001008 RetOps[0] = Chain; // Update chain.
1009
1010 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +00001011 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +00001012 RetOps.push_back(Flag);
1013
1014 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015}
1016
1017
1018/// LowerCallResult - Lower the result values of an ISD::CALL into the
1019/// appropriate copies out of appropriate physical registers. This assumes that
1020/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1021/// being lowered. The returns a SDNode with the same number of values as the
1022/// ISD::CALL.
1023SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +00001024LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 unsigned CallingConv, SelectionDAG &DAG) {
1026
1027 // Assign locations to each value returned by this call.
1028 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +00001029 bool isVarArg = TheCall->isVarArg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
1031 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1032
Dan Gohman8181bd12008-07-27 21:46:04 +00001033 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001034
1035 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001036 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00001037 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001038
1039 // If this is a call to a function that returns an fp value on the floating
1040 // point stack, but where we prefer to use the value in xmm registers, copy
1041 // 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 +00001042 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1043 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001044 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1045 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001046 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001048 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1049 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001050 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001051 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001052
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001053 if (CopyVT != RVLocs[i].getValVT()) {
1054 // Round the F80 the right size, which also moves to the appropriate xmm
1055 // register.
1056 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1057 // This truncation won't change the value.
1058 DAG.getIntPtrConstant(1));
1059 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001060
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001061 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062 }
Duncan Sands698842f2008-07-02 17:40:58 +00001063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 // Merge everything together with a MERGE_VALUES node.
1065 ResultVals.push_back(Chain);
Duncan Sands42d7bb82008-12-01 11:41:29 +00001066 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(), &ResultVals[0],
1067 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068}
1069
1070
1071//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001072// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073//===----------------------------------------------------------------------===//
1074// StdCall calling convention seems to be standard for many Windows' API
1075// routines and around. It differs from C calling convention just a little:
1076// callee should clean up the stack, not caller. Symbols should be also
1077// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001078// For info on fast calling convention see Fast Calling Convention (tail call)
1079// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001080
1081/// AddLiveIn - This helper function adds the specified physical register to the
1082/// MachineFunction as a live in value. It also creates a corresponding virtual
1083/// register for it.
1084static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1085 const TargetRegisterClass *RC) {
1086 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001087 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1088 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089 return VReg;
1090}
1091
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001092/// CallIsStructReturn - Determines whether a CALL node uses struct return
1093/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001094static bool CallIsStructReturn(CallSDNode *TheCall) {
1095 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001096 if (!NumOps)
1097 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001098
Dan Gohman705e3f72008-09-13 01:54:27 +00001099 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001100}
1101
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001102/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1103/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001104static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001105 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001106 if (!NumArgs)
1107 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001108
1109 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001110}
1111
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001112/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1113/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001114/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001115bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001116 if (IsVarArg)
1117 return false;
1118
Dan Gohman705e3f72008-09-13 01:54:27 +00001119 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001120 default:
1121 return false;
1122 case CallingConv::X86_StdCall:
1123 return !Subtarget->is64Bit();
1124 case CallingConv::X86_FastCall:
1125 return !Subtarget->is64Bit();
1126 case CallingConv::Fast:
1127 return PerformTailCallOpt;
1128 }
1129}
1130
Dan Gohman705e3f72008-09-13 01:54:27 +00001131/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1132/// given CallingConvention value.
1133CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001134 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001135 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001136 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001137 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1138 return CC_X86_64_TailCall;
1139 else
1140 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001141 }
1142
Gordon Henriksen18ace102008-01-05 16:56:59 +00001143 if (CC == CallingConv::X86_FastCall)
1144 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001145 else if (CC == CallingConv::Fast)
1146 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001147 else
1148 return CC_X86_32_C;
1149}
1150
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001151/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1152/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001153NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001154X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001155 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001156 if (CC == CallingConv::X86_FastCall)
1157 return FastCall;
1158 else if (CC == CallingConv::X86_StdCall)
1159 return StdCall;
1160 return None;
1161}
1162
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001163
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001164/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1165/// in a register before calling.
1166bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1167 return !IsTailCall && !Is64Bit &&
1168 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1169 Subtarget->isPICStyleGOT();
1170}
1171
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001172/// CallRequiresFnAddressInReg - Check whether the call requires the function
1173/// address to be loaded in a register.
1174bool
1175X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1176 return !Is64Bit && IsTailCall &&
1177 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1178 Subtarget->isPICStyleGOT();
1179}
1180
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001181/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1182/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001183/// the specific parameter attribute. The copy will be passed as a byval
1184/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001185static SDValue
1186CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001187 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001188 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001189 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001190 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001191}
1192
Dan Gohman8181bd12008-07-27 21:46:04 +00001193SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001194 const CCValAssign &VA,
1195 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001196 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001197 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001198 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001199 ISD::ArgFlagsTy Flags =
1200 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001201 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001202 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001203
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001204 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1205 // changed with more analysis.
1206 // In case of tail call optimization mark all arguments mutable. Since they
1207 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001208 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001209 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001210 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001211 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001212 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001213 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001214 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001215}
1216
Dan Gohman8181bd12008-07-27 21:46:04 +00001217SDValue
1218X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001219 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001220 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1221
1222 const Function* Fn = MF.getFunction();
1223 if (Fn->hasExternalLinkage() &&
1224 Subtarget->isTargetCygMing() &&
1225 Fn->getName() == "main")
1226 FuncInfo->setForceFramePointer(true);
1227
1228 // Decorate the function name.
1229 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1230
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001231 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001232 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001233 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001234 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001235 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001236 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001237
1238 assert(!(isVarArg && CC == CallingConv::Fast) &&
1239 "Var args not supported with calling convention fastcc");
1240
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001241 // Assign locations to all of the incoming arguments.
1242 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001243 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001244 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001245
Dan Gohman8181bd12008-07-27 21:46:04 +00001246 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 unsigned LastVal = ~0U;
1248 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1249 CCValAssign &VA = ArgLocs[i];
1250 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1251 // places.
1252 assert(VA.getValNo() != LastVal &&
1253 "Don't support value assigned to multiple locs yet");
1254 LastVal = VA.getValNo();
1255
1256 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001257 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001258 TargetRegisterClass *RC;
1259 if (RegVT == MVT::i32)
1260 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001261 else if (Is64Bit && RegVT == MVT::i64)
1262 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001263 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001264 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001265 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001266 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001267 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001268 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001269 else if (RegVT.isVector()) {
1270 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001271 if (!Is64Bit)
1272 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1273 else {
1274 // Darwin calling convention passes MMX values in either GPRs or
1275 // XMMs in x86-64. Other targets pass them in memory.
1276 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1277 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1278 RegVT = MVT::v2i64;
1279 } else {
1280 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1281 RegVT = MVT::i64;
1282 }
1283 }
1284 } else {
1285 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001286 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001287
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001288 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001289 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290
1291 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1292 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1293 // right size.
1294 if (VA.getLocInfo() == CCValAssign::SExt)
1295 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1296 DAG.getValueType(VA.getValVT()));
1297 else if (VA.getLocInfo() == CCValAssign::ZExt)
1298 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1299 DAG.getValueType(VA.getValVT()));
1300
1301 if (VA.getLocInfo() != CCValAssign::Full)
1302 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1303
Gordon Henriksen18ace102008-01-05 16:56:59 +00001304 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001305 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001306 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001307 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1308 else if (RC == X86::VR128RegisterClass) {
1309 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1310 DAG.getConstant(0, MVT::i64));
1311 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1312 }
1313 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001314
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001315 ArgValues.push_back(ArgValue);
1316 } else {
1317 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001318 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001319 }
1320 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001321
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001322 // The x86-64 ABI for returning structs by value requires that we copy
1323 // the sret argument into %rax for the return. Save the argument into
1324 // a virtual register so that we can access it from the return points.
1325 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1326 MachineFunction &MF = DAG.getMachineFunction();
1327 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1328 unsigned Reg = FuncInfo->getSRetReturnReg();
1329 if (!Reg) {
1330 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1331 FuncInfo->setSRetReturnReg(Reg);
1332 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001333 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001334 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1335 }
1336
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001337 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001338 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001339 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001340 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001341
1342 // If the function takes variable number of arguments, make a frame index for
1343 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001344 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001345 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1346 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1347 }
1348 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001349 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1350
1351 // FIXME: We should really autogenerate these arrays
1352 static const unsigned GPR64ArgRegsWin64[] = {
1353 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001354 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001355 static const unsigned XMMArgRegsWin64[] = {
1356 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1357 };
1358 static const unsigned GPR64ArgRegs64Bit[] = {
1359 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1360 };
1361 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001362 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1363 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1364 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001365 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1366
1367 if (IsWin64) {
1368 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1369 GPR64ArgRegs = GPR64ArgRegsWin64;
1370 XMMArgRegs = XMMArgRegsWin64;
1371 } else {
1372 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1373 GPR64ArgRegs = GPR64ArgRegs64Bit;
1374 XMMArgRegs = XMMArgRegs64Bit;
1375 }
1376 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1377 TotalNumIntRegs);
1378 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1379 TotalNumXMMRegs);
1380
Gordon Henriksen18ace102008-01-05 16:56:59 +00001381 // For X86-64, if there are vararg parameters that are passed via
1382 // registers, then we must store them to their spots on the stack so they
1383 // may be loaded by deferencing the result of va_next.
1384 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001385 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1386 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1387 TotalNumXMMRegs * 16, 16);
1388
Gordon Henriksen18ace102008-01-05 16:56:59 +00001389 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001390 SmallVector<SDValue, 8> MemOps;
1391 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1392 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001393 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001394 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001395 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1396 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001397 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1398 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001399 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001400 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001401 MemOps.push_back(Store);
1402 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001403 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001404 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001405
Gordon Henriksen18ace102008-01-05 16:56:59 +00001406 // Now store the XMM (fp + vector) parameter registers.
1407 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001408 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001409 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001410 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1411 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001412 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1413 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001414 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001415 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001416 MemOps.push_back(Store);
1417 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001418 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001419 }
1420 if (!MemOps.empty())
1421 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1422 &MemOps[0], MemOps.size());
1423 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001424 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001425
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001426 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001427
Gordon Henriksen18ace102008-01-05 16:56:59 +00001428 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001429 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001430 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001431 BytesCallerReserves = 0;
1432 } else {
1433 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001434 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001435 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001436 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001437 BytesCallerReserves = StackSize;
1438 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001439
Gordon Henriksen18ace102008-01-05 16:56:59 +00001440 if (!Is64Bit) {
1441 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1442 if (CC == CallingConv::X86_FastCall)
1443 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1444 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001445
Anton Korobeynikove844e472007-08-15 17:12:32 +00001446 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447
1448 // Return the new list of results.
Duncan Sands42d7bb82008-12-01 11:41:29 +00001449 return DAG.getNode(ISD::MERGE_VALUES, Op.getNode()->getVTList(),
1450 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001451}
1452
Dan Gohman8181bd12008-07-27 21:46:04 +00001453SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001454X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001455 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001456 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001457 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001458 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001459 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001460 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001461 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001462 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001463 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001464 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001465 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001466 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001467}
1468
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001469/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1470/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001471SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001472X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001473 SDValue &OutRetAddr,
1474 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001475 bool IsTailCall,
1476 bool Is64Bit,
1477 int FPDiff) {
1478 if (!IsTailCall || FPDiff==0) return Chain;
1479
1480 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001481 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001482 OutRetAddr = getReturnAddressFrameIndex(DAG);
1483 // Load the "old" Return address.
1484 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001485 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001486}
1487
1488/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1489/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001490static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001491EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001492 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001493 bool Is64Bit, int FPDiff) {
1494 // Store the return address to the appropriate stack slot.
1495 if (!FPDiff) return Chain;
1496 // Calculate the new stack slot for the return address.
1497 int SlotSize = Is64Bit ? 8 : 4;
1498 int NewReturnAddrFI =
1499 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001500 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001501 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001502 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001503 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001504 return Chain;
1505}
1506
Dan Gohman8181bd12008-07-27 21:46:04 +00001507SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001508 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001509 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1510 SDValue Chain = TheCall->getChain();
1511 unsigned CC = TheCall->getCallingConv();
1512 bool isVarArg = TheCall->isVarArg();
1513 bool IsTailCall = TheCall->isTailCall() &&
1514 CC == CallingConv::Fast && PerformTailCallOpt;
1515 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001516 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001517 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001518
1519 assert(!(isVarArg && CC == CallingConv::Fast) &&
1520 "Var args not supported with calling convention fastcc");
1521
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001522 // Analyze operands of the call, assigning locations to each operand.
1523 SmallVector<CCValAssign, 16> ArgLocs;
1524 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001525 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001526
1527 // Get a count of how many bytes are to be pushed on the stack.
1528 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001529 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001530 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001531
Gordon Henriksen18ace102008-01-05 16:56:59 +00001532 int FPDiff = 0;
1533 if (IsTailCall) {
1534 // Lower arguments at fp - stackoffset + fpdiff.
1535 unsigned NumBytesCallerPushed =
1536 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1537 FPDiff = NumBytesCallerPushed - NumBytes;
1538
1539 // Set the delta of movement of the returnaddr stackslot.
1540 // But only set if delta is greater than previous delta.
1541 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1542 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1543 }
1544
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001545 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001546
Dan Gohman8181bd12008-07-27 21:46:04 +00001547 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001548 // Load return adress for tail calls.
1549 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1550 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001551
Dan Gohman8181bd12008-07-27 21:46:04 +00001552 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1553 SmallVector<SDValue, 8> MemOpChains;
1554 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001555
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001556 // Walk the register/memloc assignments, inserting copies/loads. In the case
1557 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001558 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1559 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001560 SDValue Arg = TheCall->getArg(i);
1561 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1562 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001563
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001564 // Promote the value if needed.
1565 switch (VA.getLocInfo()) {
1566 default: assert(0 && "Unknown loc info!");
1567 case CCValAssign::Full: break;
1568 case CCValAssign::SExt:
1569 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1570 break;
1571 case CCValAssign::ZExt:
1572 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1573 break;
1574 case CCValAssign::AExt:
1575 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1576 break;
1577 }
1578
1579 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001580 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001581 MVT RegVT = VA.getLocVT();
1582 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001583 switch (VA.getLocReg()) {
1584 default:
1585 break;
1586 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1587 case X86::R8: {
1588 // Special case: passing MMX values in GPR registers.
1589 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1590 break;
1591 }
1592 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1593 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1594 // Special case: passing MMX values in XMM registers.
1595 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1596 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1597 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1598 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1599 getMOVLMask(2, DAG));
1600 break;
1601 }
1602 }
1603 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001604 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1605 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001606 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001607 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001608 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001609 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1610
Dan Gohman705e3f72008-09-13 01:54:27 +00001611 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1612 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001613 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001614 }
1615 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001616
1617 if (!MemOpChains.empty())
1618 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1619 &MemOpChains[0], MemOpChains.size());
1620
1621 // Build a sequence of copy-to-reg nodes chained together with token chain
1622 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001623 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001624 // Tail call byval lowering might overwrite argument registers so in case of
1625 // tail call optimization the copies to registers are lowered later.
1626 if (!IsTailCall)
1627 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1628 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1629 InFlag);
1630 InFlag = Chain.getValue(1);
1631 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001632
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001633 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001634 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001635 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1636 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1637 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1638 InFlag);
1639 InFlag = Chain.getValue(1);
1640 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001641 // If we are tail calling and generating PIC/GOT style code load the address
1642 // of the callee into ecx. The value in ecx is used as target of the tail
1643 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1644 // calls on PIC/GOT architectures. Normally we would just put the address of
1645 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1646 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001647 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001648 // Note: The actual moving to ecx is done further down.
1649 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001650 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001651 !G->getGlobal()->hasProtectedVisibility())
1652 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001653 else if (isa<ExternalSymbolSDNode>(Callee))
1654 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001655 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001656
Gordon Henriksen18ace102008-01-05 16:56:59 +00001657 if (Is64Bit && isVarArg) {
1658 // From AMD64 ABI document:
1659 // For calls that may call functions that use varargs or stdargs
1660 // (prototype-less calls or calls to functions containing ellipsis (...) in
1661 // the declaration) %al is used as hidden argument to specify the number
1662 // of SSE registers used. The contents of %al do not need to match exactly
1663 // the number of registers, but must be an ubound on the number of SSE
1664 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001665
1666 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001667 // Count the number of XMM registers allocated.
1668 static const unsigned XMMArgRegs[] = {
1669 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1670 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1671 };
1672 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1673
1674 Chain = DAG.getCopyToReg(Chain, X86::AL,
1675 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1676 InFlag = Chain.getValue(1);
1677 }
1678
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001679
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001680 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001681 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001682 SmallVector<SDValue, 8> MemOpChains2;
1683 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001684 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001685 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001686 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001687 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1688 CCValAssign &VA = ArgLocs[i];
1689 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001690 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001691 SDValue Arg = TheCall->getArg(i);
1692 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001693 // Create frame index.
1694 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001695 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001696 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001697 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001698
Duncan Sandsc93fae32008-03-21 09:14:45 +00001699 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001700 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001701 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001702 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001703 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1704 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1705
1706 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001707 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001708 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001709 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001710 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001711 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001712 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001713 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001714 }
1715 }
1716
1717 if (!MemOpChains2.empty())
1718 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001719 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001720
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001721 // Copy arguments to their registers.
1722 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1723 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1724 InFlag);
1725 InFlag = Chain.getValue(1);
1726 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001727 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001728
Gordon Henriksen18ace102008-01-05 16:56:59 +00001729 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001730 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1731 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001732 }
1733
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001734 // If the callee is a GlobalAddress node (quite common, every direct call is)
1735 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1736 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1737 // We should use extra load for direct calls to dllimported functions in
1738 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001739 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1740 getTargetMachine(), true))
Dan Gohman36322c72008-10-18 02:06:02 +00001741 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1742 G->getOffset());
Bill Wendlingfef06052008-09-16 21:48:12 +00001743 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1744 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001745 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001746 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001747
1748 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001749 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001750 Callee,InFlag);
1751 Callee = DAG.getRegister(Opc, getPointerTy());
1752 // Add register as live out.
1753 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001754 }
1755
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001756 // Returns a chain & a flag for retval copy to use.
1757 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001758 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001759
1760 if (IsTailCall) {
1761 Ops.push_back(Chain);
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001762 Ops.push_back(DAG.getIntPtrConstant(NumBytes, true));
1763 Ops.push_back(DAG.getIntPtrConstant(0, true));
Gabor Greif1c80d112008-08-28 21:40:38 +00001764 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001765 Ops.push_back(InFlag);
1766 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1767 InFlag = Chain.getValue(1);
1768
1769 // Returns a chain & a flag for retval copy to use.
1770 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1771 Ops.clear();
1772 }
1773
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001774 Ops.push_back(Chain);
1775 Ops.push_back(Callee);
1776
Gordon Henriksen18ace102008-01-05 16:56:59 +00001777 if (IsTailCall)
1778 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001779
Gordon Henriksen18ace102008-01-05 16:56:59 +00001780 // Add argument registers to the end of the list so that they are known live
1781 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001782 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1783 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1784 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001785
Evan Cheng8ba45e62008-03-18 23:36:35 +00001786 // Add an implicit use GOT pointer in EBX.
1787 if (!IsTailCall && !Is64Bit &&
1788 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1789 Subtarget->isPICStyleGOT())
1790 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1791
1792 // Add an implicit use of AL for x86 vararg functions.
1793 if (Is64Bit && isVarArg)
1794 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1795
Gabor Greif1c80d112008-08-28 21:40:38 +00001796 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001797 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001798
Gordon Henriksen18ace102008-01-05 16:56:59 +00001799 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001800 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001801 "Flag must be set. Depend on flag being set in LowerRET");
1802 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman705e3f72008-09-13 01:54:27 +00001803 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001804
Gabor Greif1c80d112008-08-28 21:40:38 +00001805 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001806 }
1807
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001808 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001809 InFlag = Chain.getValue(1);
1810
1811 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001812 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001813 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001814 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001815 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001816 // If this is is a call to a struct-return function, the callee
1817 // pops the hidden struct pointer, so we have to push it back.
1818 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001819 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001820 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001821 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001822
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001823 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001824 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001825 DAG.getIntPtrConstant(NumBytes, true),
1826 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1827 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001828 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001829 InFlag = Chain.getValue(1);
1830
1831 // Handle result values, copying them out of physregs into vregs that we
1832 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001833 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001834 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001835}
1836
1837
1838//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001839// Fast Calling Convention (tail call) implementation
1840//===----------------------------------------------------------------------===//
1841
1842// Like std call, callee cleans arguments, convention except that ECX is
1843// reserved for storing the tail called function address. Only 2 registers are
1844// free for argument passing (inreg). Tail call optimization is performed
1845// provided:
1846// * tailcallopt is enabled
1847// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001848// On X86_64 architecture with GOT-style position independent code only local
1849// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001850// To keep the stack aligned according to platform abi the function
1851// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1852// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001853// If a tail called function callee has more arguments than the caller the
1854// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001855// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001856// original REtADDR, but before the saved framepointer or the spilled registers
1857// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1858// stack layout:
1859// arg1
1860// arg2
1861// RETADDR
1862// [ new RETADDR
1863// move area ]
1864// (possible EBP)
1865// ESI
1866// EDI
1867// local1 ..
1868
1869/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1870/// for a 16 byte align requirement.
1871unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1872 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001873 MachineFunction &MF = DAG.getMachineFunction();
1874 const TargetMachine &TM = MF.getTarget();
1875 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1876 unsigned StackAlignment = TFI.getStackAlignment();
1877 uint64_t AlignMask = StackAlignment - 1;
1878 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001879 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001880 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1881 // Number smaller than 12 so just add the difference.
1882 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1883 } else {
1884 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1885 Offset = ((~AlignMask) & Offset) + StackAlignment +
1886 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001887 }
Evan Chengded8f902008-09-07 09:07:23 +00001888 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001889}
1890
1891/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001892/// following the call is a return. A function is eligible if caller/callee
1893/// calling conventions match, currently only fastcc supports tail calls, and
1894/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001895bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001896 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001897 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001898 if (!PerformTailCallOpt)
1899 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001900
Dan Gohman705e3f72008-09-13 01:54:27 +00001901 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001902 MachineFunction &MF = DAG.getMachineFunction();
1903 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001904 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001905 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001906 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001907 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001908 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001909 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001910 return true;
1911
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001912 // Can only do local tail calls (in same module, hidden or protected) on
1913 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001914 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1915 return G->getGlobal()->hasHiddenVisibility()
1916 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001917 }
1918 }
Evan Chenge7a87392007-11-02 01:26:22 +00001919
1920 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001921}
1922
Dan Gohmanca4857a2008-09-03 23:12:08 +00001923FastISel *
1924X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001925 MachineModuleInfo *mmo,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001926 DenseMap<const Value *, unsigned> &vm,
1927 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001928 MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00001929 DenseMap<const AllocaInst *, int> &am
1930#ifndef NDEBUG
1931 , SmallSet<Instruction*, 8> &cil
1932#endif
1933 ) {
1934 return X86::createFastISel(mf, mmo, vm, bm, am
1935#ifndef NDEBUG
1936 , cil
1937#endif
1938 );
Dan Gohman97805ee2008-08-19 21:32:53 +00001939}
1940
1941
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001942//===----------------------------------------------------------------------===//
1943// Other Lowering Hooks
1944//===----------------------------------------------------------------------===//
1945
1946
Dan Gohman8181bd12008-07-27 21:46:04 +00001947SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001948 MachineFunction &MF = DAG.getMachineFunction();
1949 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1950 int ReturnAddrIndex = FuncInfo->getRAIndex();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001951 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikove844e472007-08-15 17:12:32 +00001952
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001953 if (ReturnAddrIndex == 0) {
1954 // Set up a frame object for the return address.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001955 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001956 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001957 }
1958
1959 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1960}
1961
1962
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001963/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1964/// specific condition code. It returns a false if it cannot do a direct
1965/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1966/// needed.
1967static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001968 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001969 SelectionDAG &DAG) {
1970 X86CC = X86::COND_INVALID;
1971 if (!isFP) {
1972 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1973 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1974 // X > -1 -> X == 0, jump !sign.
1975 RHS = DAG.getConstant(0, RHS.getValueType());
1976 X86CC = X86::COND_NS;
1977 return true;
1978 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1979 // X < 0 -> X == 0, jump on sign.
1980 X86CC = X86::COND_S;
1981 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001982 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00001983 // X < 1 -> X <= 0
1984 RHS = DAG.getConstant(0, RHS.getValueType());
1985 X86CC = X86::COND_LE;
1986 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001987 }
1988 }
1989
1990 switch (SetCCOpcode) {
1991 default: break;
1992 case ISD::SETEQ: X86CC = X86::COND_E; break;
1993 case ISD::SETGT: X86CC = X86::COND_G; break;
1994 case ISD::SETGE: X86CC = X86::COND_GE; break;
1995 case ISD::SETLT: X86CC = X86::COND_L; break;
1996 case ISD::SETLE: X86CC = X86::COND_LE; break;
1997 case ISD::SETNE: X86CC = X86::COND_NE; break;
1998 case ISD::SETULT: X86CC = X86::COND_B; break;
1999 case ISD::SETUGT: X86CC = X86::COND_A; break;
2000 case ISD::SETULE: X86CC = X86::COND_BE; break;
2001 case ISD::SETUGE: X86CC = X86::COND_AE; break;
2002 }
2003 } else {
Duncan Sandsc2a04622008-10-24 13:03:10 +00002004 // First determine if it is required or is profitable to flip the operands.
2005
2006 // If LHS is a foldable load, but RHS is not, flip the condition.
2007 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2008 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2009 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2010 std::swap(LHS, RHS);
2011 }
2012
Evan Chengb488ca32008-08-29 23:22:12 +00002013 switch (SetCCOpcode) {
2014 default: break;
2015 case ISD::SETOLT:
2016 case ISD::SETOLE:
2017 case ISD::SETUGT:
2018 case ISD::SETUGE:
Duncan Sandsc2a04622008-10-24 13:03:10 +00002019 std::swap(LHS, RHS);
Evan Chengb488ca32008-08-29 23:22:12 +00002020 break;
2021 }
2022
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002023 // On a floating point condition, the flags are set as follows:
2024 // ZF PF CF op
2025 // 0 | 0 | 0 | X > Y
2026 // 0 | 0 | 1 | X < Y
2027 // 1 | 0 | 0 | X == Y
2028 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002029 switch (SetCCOpcode) {
2030 default: break;
2031 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00002032 case ISD::SETEQ:
2033 X86CC = X86::COND_E;
2034 break;
2035 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002036 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00002037 case ISD::SETGT:
2038 X86CC = X86::COND_A;
2039 break;
2040 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002041 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00002042 case ISD::SETGE:
2043 X86CC = X86::COND_AE;
2044 break;
2045 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002046 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00002047 case ISD::SETLT:
2048 X86CC = X86::COND_B;
2049 break;
2050 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002051 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00002052 case ISD::SETLE:
2053 X86CC = X86::COND_BE;
2054 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002055 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00002056 case ISD::SETNE:
2057 X86CC = X86::COND_NE;
2058 break;
2059 case ISD::SETUO:
2060 X86CC = X86::COND_P;
2061 break;
2062 case ISD::SETO:
2063 X86CC = X86::COND_NP;
2064 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002065 }
Evan Chengfc937c92008-08-28 23:48:31 +00002066 }
2067
Evan Chengc6162692008-08-29 22:13:21 +00002068 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002069}
2070
2071/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2072/// code. Current x86 isa includes the following FP cmov instructions:
2073/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2074static bool hasFPCMov(unsigned X86CC) {
2075 switch (X86CC) {
2076 default:
2077 return false;
2078 case X86::COND_B:
2079 case X86::COND_BE:
2080 case X86::COND_E:
2081 case X86::COND_P:
2082 case X86::COND_A:
2083 case X86::COND_AE:
2084 case X86::COND_NE:
2085 case X86::COND_NP:
2086 return true;
2087 }
2088}
2089
2090/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2091/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002092static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002093 if (Op.getOpcode() == ISD::UNDEF)
2094 return true;
2095
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002096 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002097 return (Val >= Low && Val < Hi);
2098}
2099
2100/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2101/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002102static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002103 if (Op.getOpcode() == ISD::UNDEF)
2104 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002105 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002106}
2107
2108/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2109/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2110bool X86::isPSHUFDMask(SDNode *N) {
2111 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2112
Dan Gohman7dc19012007-08-02 21:17:01 +00002113 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002114 return false;
2115
2116 // Check if the value doesn't reference the second vector.
2117 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002118 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002119 if (Arg.getOpcode() == ISD::UNDEF) continue;
2120 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002121 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002122 return false;
2123 }
2124
2125 return true;
2126}
2127
2128/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2129/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2130bool X86::isPSHUFHWMask(SDNode *N) {
2131 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2132
2133 if (N->getNumOperands() != 8)
2134 return false;
2135
2136 // Lower quadword copied in order.
2137 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002138 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002139 if (Arg.getOpcode() == ISD::UNDEF) continue;
2140 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002141 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002142 return false;
2143 }
2144
2145 // Upper quadword shuffled.
2146 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002147 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002148 if (Arg.getOpcode() == ISD::UNDEF) continue;
2149 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002150 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002151 if (Val < 4 || Val > 7)
2152 return false;
2153 }
2154
2155 return true;
2156}
2157
2158/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2159/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2160bool X86::isPSHUFLWMask(SDNode *N) {
2161 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2162
2163 if (N->getNumOperands() != 8)
2164 return false;
2165
2166 // Upper quadword copied in order.
2167 for (unsigned i = 4; i != 8; ++i)
2168 if (!isUndefOrEqual(N->getOperand(i), i))
2169 return false;
2170
2171 // Lower quadword shuffled.
2172 for (unsigned i = 0; i != 4; ++i)
2173 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2174 return false;
2175
2176 return true;
2177}
2178
2179/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2180/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002181static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002182 if (NumElems != 2 && NumElems != 4) return false;
2183
2184 unsigned Half = NumElems / 2;
2185 for (unsigned i = 0; i < Half; ++i)
2186 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2187 return false;
2188 for (unsigned i = Half; i < NumElems; ++i)
2189 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2190 return false;
2191
2192 return true;
2193}
2194
2195bool X86::isSHUFPMask(SDNode *N) {
2196 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2197 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2198}
2199
2200/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2201/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2202/// half elements to come from vector 1 (which would equal the dest.) and
2203/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002204static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002205 if (NumOps != 2 && NumOps != 4) return false;
2206
2207 unsigned Half = NumOps / 2;
2208 for (unsigned i = 0; i < Half; ++i)
2209 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2210 return false;
2211 for (unsigned i = Half; i < NumOps; ++i)
2212 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2213 return false;
2214 return true;
2215}
2216
2217static bool isCommutedSHUFP(SDNode *N) {
2218 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2219 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2220}
2221
2222/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2223/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2224bool X86::isMOVHLPSMask(SDNode *N) {
2225 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2226
2227 if (N->getNumOperands() != 4)
2228 return false;
2229
2230 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2231 return isUndefOrEqual(N->getOperand(0), 6) &&
2232 isUndefOrEqual(N->getOperand(1), 7) &&
2233 isUndefOrEqual(N->getOperand(2), 2) &&
2234 isUndefOrEqual(N->getOperand(3), 3);
2235}
2236
2237/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2238/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2239/// <2, 3, 2, 3>
2240bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2241 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2242
2243 if (N->getNumOperands() != 4)
2244 return false;
2245
2246 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2247 return isUndefOrEqual(N->getOperand(0), 2) &&
2248 isUndefOrEqual(N->getOperand(1), 3) &&
2249 isUndefOrEqual(N->getOperand(2), 2) &&
2250 isUndefOrEqual(N->getOperand(3), 3);
2251}
2252
2253/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2254/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2255bool X86::isMOVLPMask(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 + NumElems))
2264 return false;
2265
2266 for (unsigned i = NumElems/2; i < NumElems; ++i)
2267 if (!isUndefOrEqual(N->getOperand(i), i))
2268 return false;
2269
2270 return true;
2271}
2272
2273/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2274/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2275/// and MOVLHPS.
2276bool X86::isMOVHPMask(SDNode *N) {
2277 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2278
2279 unsigned NumElems = N->getNumOperands();
2280 if (NumElems != 2 && NumElems != 4)
2281 return false;
2282
2283 for (unsigned i = 0; i < NumElems/2; ++i)
2284 if (!isUndefOrEqual(N->getOperand(i), i))
2285 return false;
2286
2287 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002288 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002289 if (!isUndefOrEqual(Arg, i + NumElems))
2290 return false;
2291 }
2292
2293 return true;
2294}
2295
2296/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2297/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002298bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002299 bool V2IsSplat = false) {
2300 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2301 return false;
2302
2303 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002304 SDValue BitI = Elts[i];
2305 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002306 if (!isUndefOrEqual(BitI, j))
2307 return false;
2308 if (V2IsSplat) {
2309 if (isUndefOrEqual(BitI1, NumElts))
2310 return false;
2311 } else {
2312 if (!isUndefOrEqual(BitI1, j + NumElts))
2313 return false;
2314 }
2315 }
2316
2317 return true;
2318}
2319
2320bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2321 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2322 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2323}
2324
2325/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2326/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002327bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002328 bool V2IsSplat = false) {
2329 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2330 return false;
2331
2332 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002333 SDValue BitI = Elts[i];
2334 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002335 if (!isUndefOrEqual(BitI, j + NumElts/2))
2336 return false;
2337 if (V2IsSplat) {
2338 if (isUndefOrEqual(BitI1, NumElts))
2339 return false;
2340 } else {
2341 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2342 return false;
2343 }
2344 }
2345
2346 return true;
2347}
2348
2349bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2350 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2351 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2352}
2353
2354/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2355/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2356/// <0, 0, 1, 1>
2357bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2358 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2359
2360 unsigned NumElems = N->getNumOperands();
2361 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2362 return false;
2363
2364 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002365 SDValue BitI = N->getOperand(i);
2366 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002367
2368 if (!isUndefOrEqual(BitI, j))
2369 return false;
2370 if (!isUndefOrEqual(BitI1, j))
2371 return false;
2372 }
2373
2374 return true;
2375}
2376
2377/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2378/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2379/// <2, 2, 3, 3>
2380bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2381 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2382
2383 unsigned NumElems = N->getNumOperands();
2384 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2385 return false;
2386
2387 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002388 SDValue BitI = N->getOperand(i);
2389 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002390
2391 if (!isUndefOrEqual(BitI, j))
2392 return false;
2393 if (!isUndefOrEqual(BitI1, j))
2394 return false;
2395 }
2396
2397 return true;
2398}
2399
2400/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2401/// specifies a shuffle of elements that is suitable for input to MOVSS,
2402/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002403static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002404 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002405 return false;
2406
2407 if (!isUndefOrEqual(Elts[0], NumElts))
2408 return false;
2409
2410 for (unsigned i = 1; i < NumElts; ++i) {
2411 if (!isUndefOrEqual(Elts[i], i))
2412 return false;
2413 }
2414
2415 return true;
2416}
2417
2418bool X86::isMOVLMask(SDNode *N) {
2419 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2420 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2421}
2422
2423/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2424/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2425/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002426static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002427 bool V2IsSplat = false,
2428 bool V2IsUndef = false) {
2429 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2430 return false;
2431
2432 if (!isUndefOrEqual(Ops[0], 0))
2433 return false;
2434
2435 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002436 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002437 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2438 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2439 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2440 return false;
2441 }
2442
2443 return true;
2444}
2445
2446static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2447 bool V2IsUndef = false) {
2448 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2449 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2450 V2IsSplat, V2IsUndef);
2451}
2452
2453/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2454/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2455bool X86::isMOVSHDUPMask(SDNode *N) {
2456 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2457
2458 if (N->getNumOperands() != 4)
2459 return false;
2460
2461 // Expect 1, 1, 3, 3
2462 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002463 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002464 if (Arg.getOpcode() == ISD::UNDEF) continue;
2465 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002466 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002467 if (Val != 1) return false;
2468 }
2469
2470 bool HasHi = false;
2471 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002472 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002473 if (Arg.getOpcode() == ISD::UNDEF) continue;
2474 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002475 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002476 if (Val != 3) return false;
2477 HasHi = true;
2478 }
2479
2480 // Don't use movshdup if it can be done with a shufps.
2481 return HasHi;
2482}
2483
2484/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2485/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2486bool X86::isMOVSLDUPMask(SDNode *N) {
2487 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2488
2489 if (N->getNumOperands() != 4)
2490 return false;
2491
2492 // Expect 0, 0, 2, 2
2493 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002494 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002495 if (Arg.getOpcode() == ISD::UNDEF) continue;
2496 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002497 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002498 if (Val != 0) return false;
2499 }
2500
2501 bool HasHi = false;
2502 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002503 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002504 if (Arg.getOpcode() == ISD::UNDEF) continue;
2505 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002506 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002507 if (Val != 2) return false;
2508 HasHi = true;
2509 }
2510
2511 // Don't use movshdup if it can be done with a shufps.
2512 return HasHi;
2513}
2514
2515/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2516/// specifies a identity operation on the LHS or RHS.
2517static bool isIdentityMask(SDNode *N, bool RHS = false) {
2518 unsigned NumElems = N->getNumOperands();
2519 for (unsigned i = 0; i < NumElems; ++i)
2520 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2521 return false;
2522 return true;
2523}
2524
2525/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2526/// a splat of a single element.
2527static bool isSplatMask(SDNode *N) {
2528 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2529
2530 // This is a splat operation if each element of the permute is the same, and
2531 // if the value doesn't reference the second vector.
2532 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002533 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002534 unsigned i = 0;
2535 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002536 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002537 if (isa<ConstantSDNode>(Elt)) {
2538 ElementBase = Elt;
2539 break;
2540 }
2541 }
2542
Gabor Greif1c80d112008-08-28 21:40:38 +00002543 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002544 return false;
2545
2546 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002547 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002548 if (Arg.getOpcode() == ISD::UNDEF) continue;
2549 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2550 if (Arg != ElementBase) return false;
2551 }
2552
2553 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002554 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002555}
2556
2557/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2558/// a splat of a single element and it's a 2 or 4 element mask.
2559bool X86::isSplatMask(SDNode *N) {
2560 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2561
2562 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2563 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2564 return false;
2565 return ::isSplatMask(N);
2566}
2567
2568/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2569/// specifies a splat of zero element.
2570bool X86::isSplatLoMask(SDNode *N) {
2571 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2572
2573 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2574 if (!isUndefOrEqual(N->getOperand(i), 0))
2575 return false;
2576 return true;
2577}
2578
Evan Chenga2497eb2008-09-25 20:50:48 +00002579/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2580/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2581bool X86::isMOVDDUPMask(SDNode *N) {
2582 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2583
2584 unsigned e = N->getNumOperands() / 2;
2585 for (unsigned i = 0; i < e; ++i)
2586 if (!isUndefOrEqual(N->getOperand(i), i))
2587 return false;
2588 for (unsigned i = 0; i < e; ++i)
2589 if (!isUndefOrEqual(N->getOperand(e+i), i))
2590 return false;
2591 return true;
2592}
2593
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002594/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2595/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2596/// instructions.
2597unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2598 unsigned NumOperands = N->getNumOperands();
2599 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2600 unsigned Mask = 0;
2601 for (unsigned i = 0; i < NumOperands; ++i) {
2602 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002603 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002604 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002605 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002606 if (Val >= NumOperands) Val -= NumOperands;
2607 Mask |= Val;
2608 if (i != NumOperands - 1)
2609 Mask <<= Shift;
2610 }
2611
2612 return Mask;
2613}
2614
2615/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2616/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2617/// instructions.
2618unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2619 unsigned Mask = 0;
2620 // 8 nodes, but we only care about the last 4.
2621 for (unsigned i = 7; i >= 4; --i) {
2622 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002623 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002624 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002625 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002626 Mask |= (Val - 4);
2627 if (i != 4)
2628 Mask <<= 2;
2629 }
2630
2631 return Mask;
2632}
2633
2634/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2635/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2636/// instructions.
2637unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2638 unsigned Mask = 0;
2639 // 8 nodes, but we only care about the first 4.
2640 for (int i = 3; i >= 0; --i) {
2641 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002642 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002643 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002644 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002645 Mask |= Val;
2646 if (i != 0)
2647 Mask <<= 2;
2648 }
2649
2650 return Mask;
2651}
2652
2653/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2654/// specifies a 8 element shuffle that can be broken into a pair of
2655/// PSHUFHW and PSHUFLW.
2656static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2657 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2658
2659 if (N->getNumOperands() != 8)
2660 return false;
2661
2662 // Lower quadword shuffled.
2663 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002664 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665 if (Arg.getOpcode() == ISD::UNDEF) continue;
2666 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002667 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002668 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002669 return false;
2670 }
2671
2672 // Upper quadword shuffled.
2673 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002674 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002675 if (Arg.getOpcode() == ISD::UNDEF) continue;
2676 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002677 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002678 if (Val < 4 || Val > 7)
2679 return false;
2680 }
2681
2682 return true;
2683}
2684
Chris Lattnere6aa3862007-11-25 00:24:49 +00002685/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002686/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002687static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2688 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002689 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002690 MVT VT = Op.getValueType();
2691 MVT MaskVT = Mask.getValueType();
2692 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002693 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002694 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002695
2696 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002697 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002698 if (Arg.getOpcode() == ISD::UNDEF) {
2699 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2700 continue;
2701 }
2702 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002703 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002704 if (Val < NumElems)
2705 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2706 else
2707 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2708 }
2709
2710 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002711 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002712 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2713}
2714
Evan Chenga6769df2007-12-07 21:30:01 +00002715/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2716/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002717static
Dan Gohman8181bd12008-07-27 21:46:04 +00002718SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002719 MVT MaskVT = Mask.getValueType();
2720 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002721 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002722 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002723 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002724 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002725 if (Arg.getOpcode() == ISD::UNDEF) {
2726 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2727 continue;
2728 }
2729 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002730 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002731 if (Val < NumElems)
2732 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2733 else
2734 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2735 }
2736 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2737}
2738
2739
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002740/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2741/// match movhlps. The lower half elements should come from upper half of
2742/// V1 (and in order), and the upper half elements should come from the upper
2743/// half of V2 (and in order).
2744static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2745 unsigned NumElems = Mask->getNumOperands();
2746 if (NumElems != 4)
2747 return false;
2748 for (unsigned i = 0, e = 2; i != e; ++i)
2749 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2750 return false;
2751 for (unsigned i = 2; i != 4; ++i)
2752 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2753 return false;
2754 return true;
2755}
2756
2757/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002758/// is promoted to a vector. It also returns the LoadSDNode by reference if
2759/// required.
2760static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002761 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2762 return false;
2763 N = N->getOperand(0).getNode();
2764 if (!ISD::isNON_EXTLoad(N))
2765 return false;
2766 if (LD)
2767 *LD = cast<LoadSDNode>(N);
2768 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002769}
2770
2771/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2772/// match movlp{s|d}. The lower half elements should come from lower half of
2773/// V1 (and in order), and the upper half elements should come from the upper
2774/// half of V2 (and in order). And since V1 will become the source of the
2775/// MOVLP, it must be either a vector load or a scalar load to vector.
2776static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2777 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2778 return false;
2779 // Is V2 is a vector load, don't do this transformation. We will try to use
2780 // load folding shufps op.
2781 if (ISD::isNON_EXTLoad(V2))
2782 return false;
2783
2784 unsigned NumElems = Mask->getNumOperands();
2785 if (NumElems != 2 && NumElems != 4)
2786 return false;
2787 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2788 if (!isUndefOrEqual(Mask->getOperand(i), i))
2789 return false;
2790 for (unsigned i = NumElems/2; i != NumElems; ++i)
2791 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2792 return false;
2793 return true;
2794}
2795
2796/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2797/// all the same.
2798static bool isSplatVector(SDNode *N) {
2799 if (N->getOpcode() != ISD::BUILD_VECTOR)
2800 return false;
2801
Dan Gohman8181bd12008-07-27 21:46:04 +00002802 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002803 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2804 if (N->getOperand(i) != SplatValue)
2805 return false;
2806 return true;
2807}
2808
2809/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2810/// to an undef.
2811static bool isUndefShuffle(SDNode *N) {
2812 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2813 return false;
2814
Dan Gohman8181bd12008-07-27 21:46:04 +00002815 SDValue V1 = N->getOperand(0);
2816 SDValue V2 = N->getOperand(1);
2817 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002818 unsigned NumElems = Mask.getNumOperands();
2819 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002820 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002821 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002822 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002823 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2824 return false;
2825 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2826 return false;
2827 }
2828 }
2829 return true;
2830}
2831
2832/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2833/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002834static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002836 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002837 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002838 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002839}
2840
2841/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2842/// to an zero vector.
2843static bool isZeroShuffle(SDNode *N) {
2844 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2845 return false;
2846
Dan Gohman8181bd12008-07-27 21:46:04 +00002847 SDValue V1 = N->getOperand(0);
2848 SDValue V2 = N->getOperand(1);
2849 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002850 unsigned NumElems = Mask.getNumOperands();
2851 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002852 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002853 if (Arg.getOpcode() == ISD::UNDEF)
2854 continue;
2855
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002856 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002857 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002858 unsigned Opc = V1.getNode()->getOpcode();
2859 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002860 continue;
2861 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002862 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002863 return false;
2864 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002865 unsigned Opc = V2.getNode()->getOpcode();
2866 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002867 continue;
2868 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002869 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002870 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002871 }
2872 }
2873 return true;
2874}
2875
2876/// getZeroVector - Returns a vector of specified type with all zero elements.
2877///
Dan Gohman8181bd12008-07-27 21:46:04 +00002878static SDValue getZeroVector(MVT VT, bool HasSSE2, 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 zero 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 Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002884 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002885 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002886 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002887 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002888 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002889 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002890 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002891 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002892 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2893 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002894 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002895}
2896
Chris Lattnere6aa3862007-11-25 00:24:49 +00002897/// getOnesVector - Returns a vector of specified type with all bits set.
2898///
Dan Gohman8181bd12008-07-27 21:46:04 +00002899static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002900 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002901
2902 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2903 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002904 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2905 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002906 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002907 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2908 else // SSE
2909 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2910 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2911}
2912
2913
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002914/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2915/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002916static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002917 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2918
2919 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002920 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002921 unsigned NumElems = Mask.getNumOperands();
2922 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002923 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002924 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002925 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002926 if (Val > NumElems) {
2927 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2928 Changed = true;
2929 }
2930 }
2931 MaskVec.push_back(Arg);
2932 }
2933
2934 if (Changed)
2935 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2936 &MaskVec[0], MaskVec.size());
2937 return Mask;
2938}
2939
2940/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2941/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002942static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002943 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2944 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002945
Dan Gohman8181bd12008-07-27 21:46:04 +00002946 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002947 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2948 for (unsigned i = 1; i != NumElems; ++i)
2949 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2950 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2951}
2952
2953/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2954/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002955static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002956 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2957 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002958 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002959 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2960 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2961 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2962 }
2963 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2964}
2965
2966/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2967/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002968static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002969 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2970 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002971 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002972 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002973 for (unsigned i = 0; i != Half; ++i) {
2974 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2975 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2976 }
2977 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2978}
2979
Chris Lattner2d91b962008-03-09 01:05:04 +00002980/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2981/// element #0 of a vector with the specified index, leaving the rest of the
2982/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002983static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002984 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002985 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2986 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002987 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002988 // Element #0 of the result gets the elt we are replacing.
2989 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2990 for (unsigned i = 1; i != NumElems; ++i)
2991 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2992 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2993}
2994
Evan Chengbf8b2c52008-04-05 00:30:36 +00002995/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002996static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002997 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2998 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002999 if (PVT == VT)
3000 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00003001 SDValue V1 = Op.getOperand(0);
3002 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003003 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003004 // Special handling of v4f32 -> v4i32.
3005 if (VT != MVT::v4f32) {
3006 Mask = getUnpacklMask(NumElems, DAG);
3007 while (NumElems > 4) {
3008 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
3009 NumElems >>= 1;
3010 }
Evan Cheng8c590372008-05-15 08:39:06 +00003011 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003012 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003013
Evan Chengbf8b2c52008-04-05 00:30:36 +00003014 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00003015 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00003016 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003017 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3018}
3019
Evan Chenga2497eb2008-09-25 20:50:48 +00003020/// isVectorLoad - Returns true if the node is a vector load, a scalar
3021/// load that's promoted to vector, or a load bitcasted.
3022static bool isVectorLoad(SDValue Op) {
3023 assert(Op.getValueType().isVector() && "Expected a vector type");
3024 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
3025 Op.getOpcode() == ISD::BIT_CONVERT) {
3026 return isa<LoadSDNode>(Op.getOperand(0));
3027 }
3028 return isa<LoadSDNode>(Op);
3029}
3030
3031
3032/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3033///
3034static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3035 SelectionDAG &DAG, bool HasSSE3) {
3036 // If we have sse3 and shuffle has more than one use or input is a load, then
3037 // use movddup. Otherwise, use movlhps.
3038 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3039 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3040 MVT VT = Op.getValueType();
3041 if (VT == PVT)
3042 return Op;
3043 unsigned NumElems = PVT.getVectorNumElements();
3044 if (NumElems == 2) {
3045 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3046 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
3047 } else {
3048 assert(NumElems == 4);
3049 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3050 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
3051 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst0, Cst1, Cst0, Cst1);
3052 }
3053
3054 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
3055 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
3056 DAG.getNode(ISD::UNDEF, PVT), Mask);
3057 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3058}
3059
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003060/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003061/// vector of zero or undef vector. This produces a shuffle where the low
3062/// element of V2 is swizzled into the zero/undef vector, landing at element
3063/// 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 +00003064static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003065 bool isZero, bool HasSSE2,
3066 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003067 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003068 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00003069 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003070 unsigned NumElems = V2.getValueType().getVectorNumElements();
3071 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3072 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003073 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003074 for (unsigned i = 0; i != NumElems; ++i)
3075 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3076 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3077 else
3078 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003079 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003080 &MaskVec[0], MaskVec.size());
3081 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3082}
3083
Evan Chengdea99362008-05-29 08:22:04 +00003084/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3085/// a shuffle that is zero.
3086static
Dan Gohman8181bd12008-07-27 21:46:04 +00003087unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003088 unsigned NumElems, bool Low,
3089 SelectionDAG &DAG) {
3090 unsigned NumZeros = 0;
3091 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003092 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003093 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003094 if (Idx.getOpcode() == ISD::UNDEF) {
3095 ++NumZeros;
3096 continue;
3097 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003098 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3099 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003100 ++NumZeros;
3101 else
3102 break;
3103 }
3104 return NumZeros;
3105}
3106
3107/// isVectorShift - Returns true if the shuffle can be implemented as a
3108/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003109static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3110 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003111 unsigned NumElems = Mask.getNumOperands();
3112
3113 isLeft = true;
3114 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3115 if (!NumZeros) {
3116 isLeft = false;
3117 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3118 if (!NumZeros)
3119 return false;
3120 }
3121
3122 bool SeenV1 = false;
3123 bool SeenV2 = false;
3124 for (unsigned i = NumZeros; i < NumElems; ++i) {
3125 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003126 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003127 if (Idx.getOpcode() == ISD::UNDEF)
3128 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003129 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003130 if (Index < NumElems)
3131 SeenV1 = true;
3132 else {
3133 Index -= NumElems;
3134 SeenV2 = true;
3135 }
3136 if (Index != Val)
3137 return false;
3138 }
3139 if (SeenV1 && SeenV2)
3140 return false;
3141
3142 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3143 ShAmt = NumZeros;
3144 return true;
3145}
3146
3147
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003148/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3149///
Dan Gohman8181bd12008-07-27 21:46:04 +00003150static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003151 unsigned NumNonZero, unsigned NumZero,
3152 SelectionDAG &DAG, TargetLowering &TLI) {
3153 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003154 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003155
Dan Gohman8181bd12008-07-27 21:46:04 +00003156 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003157 bool First = true;
3158 for (unsigned i = 0; i < 16; ++i) {
3159 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3160 if (ThisIsNonZero && First) {
3161 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003162 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003163 else
3164 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3165 First = false;
3166 }
3167
3168 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003169 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003170 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3171 if (LastIsNonZero) {
3172 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3173 }
3174 if (ThisIsNonZero) {
3175 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3176 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3177 ThisElt, DAG.getConstant(8, MVT::i8));
3178 if (LastIsNonZero)
3179 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3180 } else
3181 ThisElt = LastElt;
3182
Gabor Greif1c80d112008-08-28 21:40:38 +00003183 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003184 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003185 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003186 }
3187 }
3188
3189 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3190}
3191
3192/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3193///
Dan Gohman8181bd12008-07-27 21:46:04 +00003194static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003195 unsigned NumNonZero, unsigned NumZero,
3196 SelectionDAG &DAG, TargetLowering &TLI) {
3197 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003198 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003199
Dan Gohman8181bd12008-07-27 21:46:04 +00003200 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003201 bool First = true;
3202 for (unsigned i = 0; i < 8; ++i) {
3203 bool isNonZero = (NonZeros & (1 << i)) != 0;
3204 if (isNonZero) {
3205 if (First) {
3206 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003207 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003208 else
3209 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3210 First = false;
3211 }
3212 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003213 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003214 }
3215 }
3216
3217 return V;
3218}
3219
Evan Chengdea99362008-05-29 08:22:04 +00003220/// getVShift - Return a vector logical shift node.
3221///
Dan Gohman8181bd12008-07-27 21:46:04 +00003222static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003223 unsigned NumBits, SelectionDAG &DAG,
3224 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003225 bool isMMX = VT.getSizeInBits() == 64;
3226 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003227 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3228 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3229 return DAG.getNode(ISD::BIT_CONVERT, VT,
3230 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003231 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003232}
3233
Dan Gohman8181bd12008-07-27 21:46:04 +00003234SDValue
3235X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003236 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003237 if (ISD::isBuildVectorAllZeros(Op.getNode())
3238 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003239 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3240 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3241 // eliminated on x86-32 hosts.
3242 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3243 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003244
Gabor Greif1c80d112008-08-28 21:40:38 +00003245 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003246 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003247 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003248 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003249
Duncan Sands92c43912008-06-06 12:08:01 +00003250 MVT VT = Op.getValueType();
3251 MVT EVT = VT.getVectorElementType();
3252 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003253
3254 unsigned NumElems = Op.getNumOperands();
3255 unsigned NumZero = 0;
3256 unsigned NumNonZero = 0;
3257 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003258 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003259 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003260 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003261 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003262 if (Elt.getOpcode() == ISD::UNDEF)
3263 continue;
3264 Values.insert(Elt);
3265 if (Elt.getOpcode() != ISD::Constant &&
3266 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003267 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003268 if (isZeroNode(Elt))
3269 NumZero++;
3270 else {
3271 NonZeros |= (1 << i);
3272 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003273 }
3274 }
3275
3276 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003277 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3278 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003279 }
3280
Chris Lattner66a4dda2008-03-09 05:42:06 +00003281 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003282 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003283 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003284 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003285
Chris Lattner2d91b962008-03-09 01:05:04 +00003286 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3287 // the value are obviously zero, truncate the value to i32 and do the
3288 // insertion that way. Only do this if the value is non-constant or if the
3289 // value is a constant being inserted into element 0. It is cheaper to do
3290 // a constant pool load than it is to do a movd + shuffle.
3291 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3292 (!IsAllConstants || Idx == 0)) {
3293 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3294 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003295 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3296 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003297
3298 // Truncate the value (which may itself be a constant) to i32, and
3299 // convert it to a vector with movd (S2V+shuffle to zero extend).
3300 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3301 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003302 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3303 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003304
3305 // Now we have our 32-bit value zero extended in the low element of
3306 // a vector. If Idx != 0, swizzle it into place.
3307 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003308 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003309 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3310 getSwapEltZeroMask(VecElts, Idx, DAG)
3311 };
3312 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3313 }
3314 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3315 }
3316 }
3317
Chris Lattnerac914892008-03-08 22:59:52 +00003318 // If we have a constant or non-constant insertion into the low element of
3319 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3320 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3321 // depending on what the source datatype is. Because we can only get here
3322 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3323 if (Idx == 0 &&
3324 // Don't do this for i64 values on x86-32.
3325 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003326 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003327 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003328 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3329 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003330 }
Evan Chengdea99362008-05-29 08:22:04 +00003331
3332 // Is it a vector logical left shift?
3333 if (NumElems == 2 && Idx == 1 &&
3334 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003335 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003336 return getVShift(true, VT,
3337 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3338 NumBits/2, DAG, *this);
3339 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003340
3341 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003342 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003343
Chris Lattnerac914892008-03-08 22:59:52 +00003344 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3345 // is a non-constant being inserted into an element other than the low one,
3346 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3347 // movd/movss) to move this into the low element, then shuffle it into
3348 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003349 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003350 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3351
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003352 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003353 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3354 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003355 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3356 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003357 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003358 for (unsigned i = 0; i < NumElems; i++)
3359 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003360 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003361 &MaskVec[0], MaskVec.size());
3362 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3363 DAG.getNode(ISD::UNDEF, VT), Mask);
3364 }
3365 }
3366
Chris Lattner66a4dda2008-03-09 05:42:06 +00003367 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3368 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003369 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003370
Dan Gohman21463242007-07-24 22:55:08 +00003371 // A vector full of immediates; various special cases are already
3372 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003373 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003374 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003375
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003376 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003377 if (EVTBits == 64) {
3378 if (NumNonZero == 1) {
3379 // One half is zero or undef.
3380 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003381 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003382 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003383 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3384 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003385 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003386 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003387 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003388
3389 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3390 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003391 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003392 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003393 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003394 }
3395
3396 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003397 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003398 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003399 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003400 }
3401
3402 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003403 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003404 V.resize(NumElems);
3405 if (NumElems == 4 && NumZero > 0) {
3406 for (unsigned i = 0; i < 4; ++i) {
3407 bool isZero = !(NonZeros & (1 << i));
3408 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003409 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003410 else
3411 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3412 }
3413
3414 for (unsigned i = 0; i < 2; ++i) {
3415 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3416 default: break;
3417 case 0:
3418 V[i] = V[i*2]; // Must be a zero vector.
3419 break;
3420 case 1:
3421 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3422 getMOVLMask(NumElems, DAG));
3423 break;
3424 case 2:
3425 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3426 getMOVLMask(NumElems, DAG));
3427 break;
3428 case 3:
3429 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3430 getUnpacklMask(NumElems, DAG));
3431 break;
3432 }
3433 }
3434
Duncan Sands92c43912008-06-06 12:08:01 +00003435 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3436 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003437 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003438 bool Reverse = (NonZeros & 0x3) == 2;
3439 for (unsigned i = 0; i < 2; ++i)
3440 if (Reverse)
3441 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3442 else
3443 MaskVec.push_back(DAG.getConstant(i, EVT));
3444 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3445 for (unsigned i = 0; i < 2; ++i)
3446 if (Reverse)
3447 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3448 else
3449 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003450 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003451 &MaskVec[0], MaskVec.size());
3452 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3453 }
3454
3455 if (Values.size() > 2) {
3456 // Expand into a number of unpckl*.
3457 // e.g. for v4f32
3458 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3459 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3460 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003461 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003462 for (unsigned i = 0; i < NumElems; ++i)
3463 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3464 NumElems >>= 1;
3465 while (NumElems != 0) {
3466 for (unsigned i = 0; i < NumElems; ++i)
3467 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3468 UnpckMask);
3469 NumElems >>= 1;
3470 }
3471 return V[0];
3472 }
3473
Dan Gohman8181bd12008-07-27 21:46:04 +00003474 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003475}
3476
Evan Chengfca29242007-12-07 08:07:39 +00003477static
Dan Gohman8181bd12008-07-27 21:46:04 +00003478SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003479 SDValue PermMask, SelectionDAG &DAG,
3480 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003481 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003482 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3483 MVT MaskEVT = MaskVT.getVectorElementType();
3484 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003485 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3486 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003487
3488 // First record which half of which vector the low elements come from.
3489 SmallVector<unsigned, 4> LowQuad(4);
3490 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003491 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003492 if (Elt.getOpcode() == ISD::UNDEF)
3493 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003494 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003495 int QuadIdx = EltIdx / 4;
3496 ++LowQuad[QuadIdx];
3497 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003498
Evan Cheng75184a92007-12-11 01:46:18 +00003499 int BestLowQuad = -1;
3500 unsigned MaxQuad = 1;
3501 for (unsigned i = 0; i < 4; ++i) {
3502 if (LowQuad[i] > MaxQuad) {
3503 BestLowQuad = i;
3504 MaxQuad = LowQuad[i];
3505 }
Evan Chengfca29242007-12-07 08:07:39 +00003506 }
3507
Evan Cheng75184a92007-12-11 01:46:18 +00003508 // Record which half of which vector the high elements come from.
3509 SmallVector<unsigned, 4> HighQuad(4);
3510 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003511 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003512 if (Elt.getOpcode() == ISD::UNDEF)
3513 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003514 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003515 int QuadIdx = EltIdx / 4;
3516 ++HighQuad[QuadIdx];
3517 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003518
Evan Cheng75184a92007-12-11 01:46:18 +00003519 int BestHighQuad = -1;
3520 MaxQuad = 1;
3521 for (unsigned i = 0; i < 4; ++i) {
3522 if (HighQuad[i] > MaxQuad) {
3523 BestHighQuad = i;
3524 MaxQuad = HighQuad[i];
3525 }
3526 }
3527
3528 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3529 if (BestLowQuad != -1 || BestHighQuad != -1) {
3530 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003531 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003532
Evan Cheng75184a92007-12-11 01:46:18 +00003533 if (BestLowQuad != -1)
3534 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3535 else
3536 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003537
Evan Cheng75184a92007-12-11 01:46:18 +00003538 if (BestHighQuad != -1)
3539 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3540 else
3541 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003542
Dan Gohman8181bd12008-07-27 21:46:04 +00003543 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003544 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3545 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3546 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3547 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3548
3549 // Now sort high and low parts separately.
3550 BitVector InOrder(8);
3551 if (BestLowQuad != -1) {
3552 // Sort lower half in order using PSHUFLW.
3553 MaskVec.clear();
3554 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003555
Evan Cheng75184a92007-12-11 01:46:18 +00003556 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003557 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003558 if (Elt.getOpcode() == ISD::UNDEF) {
3559 MaskVec.push_back(Elt);
3560 InOrder.set(i);
3561 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003562 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003563 if (EltIdx != i)
3564 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003565
Evan Cheng75184a92007-12-11 01:46:18 +00003566 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003567
Evan Cheng75184a92007-12-11 01:46:18 +00003568 // If this element is in the right place after this shuffle, then
3569 // remember it.
3570 if ((int)(EltIdx / 4) == BestLowQuad)
3571 InOrder.set(i);
3572 }
3573 }
3574 if (AnyOutOrder) {
3575 for (unsigned i = 4; i != 8; ++i)
3576 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003577 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003578 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3579 }
3580 }
3581
3582 if (BestHighQuad != -1) {
3583 // Sort high half in order using PSHUFHW if possible.
3584 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003585
Evan Cheng75184a92007-12-11 01:46:18 +00003586 for (unsigned i = 0; i != 4; ++i)
3587 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003588
Evan Cheng75184a92007-12-11 01:46:18 +00003589 bool AnyOutOrder = false;
3590 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003591 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003592 if (Elt.getOpcode() == ISD::UNDEF) {
3593 MaskVec.push_back(Elt);
3594 InOrder.set(i);
3595 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003596 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003597 if (EltIdx != i)
3598 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003599
Evan Cheng75184a92007-12-11 01:46:18 +00003600 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003601
Evan Cheng75184a92007-12-11 01:46:18 +00003602 // If this element is in the right place after this shuffle, then
3603 // remember it.
3604 if ((int)(EltIdx / 4) == BestHighQuad)
3605 InOrder.set(i);
3606 }
3607 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003608
Evan Cheng75184a92007-12-11 01:46:18 +00003609 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003610 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003611 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3612 }
3613 }
3614
3615 // The other elements are put in the right place using pextrw and pinsrw.
3616 for (unsigned i = 0; i != 8; ++i) {
3617 if (InOrder[i])
3618 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003619 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003620 if (Elt.getOpcode() == ISD::UNDEF)
3621 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003622 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003623 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003624 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3625 DAG.getConstant(EltIdx, PtrVT))
3626 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3627 DAG.getConstant(EltIdx - 8, PtrVT));
3628 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3629 DAG.getConstant(i, PtrVT));
3630 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003631
Evan Cheng75184a92007-12-11 01:46:18 +00003632 return NewV;
3633 }
3634
Bill Wendling2c7cd592008-08-21 22:35:37 +00003635 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3636 // few as possible. First, let's find out how many elements are already in the
3637 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003638 unsigned V1InOrder = 0;
3639 unsigned V1FromV1 = 0;
3640 unsigned V2InOrder = 0;
3641 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003642 SmallVector<SDValue, 8> V1Elts;
3643 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003644 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003645 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003646 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003647 V1Elts.push_back(Elt);
3648 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003649 ++V1InOrder;
3650 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003651 continue;
3652 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003653 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003654 if (EltIdx == i) {
3655 V1Elts.push_back(Elt);
3656 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3657 ++V1InOrder;
3658 } else if (EltIdx == i+8) {
3659 V1Elts.push_back(Elt);
3660 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3661 ++V2InOrder;
3662 } else if (EltIdx < 8) {
3663 V1Elts.push_back(Elt);
3664 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003665 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003666 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3667 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003668 }
3669 }
3670
3671 if (V2InOrder > V1InOrder) {
3672 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3673 std::swap(V1, V2);
3674 std::swap(V1Elts, V2Elts);
3675 std::swap(V1FromV1, V2FromV2);
3676 }
3677
Evan Cheng75184a92007-12-11 01:46:18 +00003678 if ((V1FromV1 + V1InOrder) != 8) {
3679 // Some elements are from V2.
3680 if (V1FromV1) {
3681 // If there are elements that are from V1 but out of place,
3682 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003683 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003684 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003685 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003686 if (Elt.getOpcode() == ISD::UNDEF) {
3687 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3688 continue;
3689 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003690 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003691 if (EltIdx >= 8)
3692 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3693 else
3694 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3695 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003696 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003697 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003698 }
Evan Cheng75184a92007-12-11 01:46:18 +00003699
3700 NewV = V1;
3701 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003702 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003703 if (Elt.getOpcode() == ISD::UNDEF)
3704 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003705 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003706 if (EltIdx < 8)
3707 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003708 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003709 DAG.getConstant(EltIdx - 8, PtrVT));
3710 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3711 DAG.getConstant(i, PtrVT));
3712 }
3713 return NewV;
3714 } else {
3715 // All elements are from V1.
3716 NewV = V1;
3717 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003718 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003719 if (Elt.getOpcode() == ISD::UNDEF)
3720 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003721 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003722 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003723 DAG.getConstant(EltIdx, PtrVT));
3724 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3725 DAG.getConstant(i, PtrVT));
3726 }
3727 return NewV;
3728 }
3729}
3730
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003731/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3732/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3733/// done when every pair / quad of shuffle mask elements point to elements in
3734/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003735/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3736static
Dan Gohman8181bd12008-07-27 21:46:04 +00003737SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003738 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003739 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003740 TargetLowering &TLI) {
3741 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003742 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003743 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003744 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003745 MVT NewVT = MaskVT;
3746 switch (VT.getSimpleVT()) {
3747 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003748 case MVT::v4f32: NewVT = MVT::v2f64; break;
3749 case MVT::v4i32: NewVT = MVT::v2i64; break;
3750 case MVT::v8i16: NewVT = MVT::v4i32; break;
3751 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003752 }
3753
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003754 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003755 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003756 NewVT = MVT::v2i64;
3757 else
3758 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003759 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003760 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003761 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003762 for (unsigned i = 0; i < NumElems; i += Scale) {
3763 unsigned StartIdx = ~0U;
3764 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003765 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003766 if (Elt.getOpcode() == ISD::UNDEF)
3767 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003768 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003769 if (StartIdx == ~0U)
3770 StartIdx = EltIdx - (EltIdx % Scale);
3771 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003772 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003773 }
3774 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003775 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003776 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003777 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003778 }
3779
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003780 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3781 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3782 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3783 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3784 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003785}
3786
Evan Chenge9b9c672008-05-09 21:53:03 +00003787/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003788///
Dan Gohman8181bd12008-07-27 21:46:04 +00003789static SDValue getVZextMovL(MVT VT, MVT OpVT,
3790 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003791 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003792 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3793 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003794 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003795 LD = dyn_cast<LoadSDNode>(SrcOp);
3796 if (!LD) {
3797 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3798 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003799 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003800 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3801 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3802 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3803 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3804 // PR2108
3805 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3806 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003807 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003808 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003809 SrcOp.getOperand(0)
3810 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003811 }
3812 }
3813 }
3814
3815 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003816 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003817 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3818}
3819
Evan Chengf50554e2008-07-22 21:13:36 +00003820/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3821/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003822static SDValue
3823LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3824 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003825 MVT MaskVT = PermMask.getValueType();
3826 MVT MaskEVT = MaskVT.getVectorElementType();
3827 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003828 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003829 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003830 unsigned NumHi = 0;
3831 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003832 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003833 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003834 if (Elt.getOpcode() == ISD::UNDEF) {
3835 Locs[i] = std::make_pair(-1, -1);
3836 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003837 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003838 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003839 if (Val < 4) {
3840 Locs[i] = std::make_pair(0, NumLo);
3841 Mask1[NumLo] = Elt;
3842 NumLo++;
3843 } else {
3844 Locs[i] = std::make_pair(1, NumHi);
3845 if (2+NumHi < 4)
3846 Mask1[2+NumHi] = Elt;
3847 NumHi++;
3848 }
3849 }
3850 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003851
Evan Chengf50554e2008-07-22 21:13:36 +00003852 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003853 // If no more than two elements come from either vector. This can be
3854 // implemented with two shuffles. First shuffle gather the elements.
3855 // The second shuffle, which takes the first shuffle as both of its
3856 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003857 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3858 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3859 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003860
Dan Gohman8181bd12008-07-27 21:46:04 +00003861 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003862 for (unsigned i = 0; i != 4; ++i) {
3863 if (Locs[i].first == -1)
3864 continue;
3865 else {
3866 unsigned Idx = (i < 2) ? 0 : 4;
3867 Idx += Locs[i].first * 2 + Locs[i].second;
3868 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3869 }
3870 }
3871
3872 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3873 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3874 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003875 } else if (NumLo == 3 || NumHi == 3) {
3876 // Otherwise, we must have three elements from one vector, call it X, and
3877 // one element from the other, call it Y. First, use a shufps to build an
3878 // intermediate vector with the one element from Y and the element from X
3879 // that will be in the same half in the final destination (the indexes don't
3880 // matter). Then, use a shufps to build the final vector, taking the half
3881 // containing the element from Y from the intermediate, and the other half
3882 // from X.
3883 if (NumHi == 3) {
3884 // Normalize it so the 3 elements come from V1.
3885 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3886 std::swap(V1, V2);
3887 }
3888
3889 // Find the element from V2.
3890 unsigned HiIndex;
3891 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003892 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003893 if (Elt.getOpcode() == ISD::UNDEF)
3894 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003895 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003896 if (Val >= 4)
3897 break;
3898 }
3899
3900 Mask1[0] = PermMask.getOperand(HiIndex);
3901 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3902 Mask1[2] = PermMask.getOperand(HiIndex^1);
3903 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3904 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3905 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3906
3907 if (HiIndex >= 2) {
3908 Mask1[0] = PermMask.getOperand(0);
3909 Mask1[1] = PermMask.getOperand(1);
3910 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3911 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3912 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3913 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3914 } else {
3915 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3916 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3917 Mask1[2] = PermMask.getOperand(2);
3918 Mask1[3] = PermMask.getOperand(3);
3919 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003920 Mask1[2] =
3921 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3922 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003923 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003924 Mask1[3] =
3925 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3926 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003927 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3928 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3929 }
Evan Chengf50554e2008-07-22 21:13:36 +00003930 }
3931
3932 // Break it into (shuffle shuffle_hi, shuffle_lo).
3933 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003934 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3935 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3936 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003937 unsigned MaskIdx = 0;
3938 unsigned LoIdx = 0;
3939 unsigned HiIdx = 2;
3940 for (unsigned i = 0; i != 4; ++i) {
3941 if (i == 2) {
3942 MaskPtr = &HiMask;
3943 MaskIdx = 1;
3944 LoIdx = 0;
3945 HiIdx = 2;
3946 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003947 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003948 if (Elt.getOpcode() == ISD::UNDEF) {
3949 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003950 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003951 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3952 (*MaskPtr)[LoIdx] = Elt;
3953 LoIdx++;
3954 } else {
3955 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3956 (*MaskPtr)[HiIdx] = Elt;
3957 HiIdx++;
3958 }
3959 }
3960
Dan Gohman8181bd12008-07-27 21:46:04 +00003961 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003962 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3963 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003964 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003965 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3966 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003967 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003968 for (unsigned i = 0; i != 4; ++i) {
3969 if (Locs[i].first == -1) {
3970 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3971 } else {
3972 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3973 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3974 }
3975 }
3976 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3977 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3978 &MaskOps[0], MaskOps.size()));
3979}
3980
Dan Gohman8181bd12008-07-27 21:46:04 +00003981SDValue
3982X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3983 SDValue V1 = Op.getOperand(0);
3984 SDValue V2 = Op.getOperand(1);
3985 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003986 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003987 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003988 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003989 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3990 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3991 bool V1IsSplat = false;
3992 bool V2IsSplat = false;
3993
Gabor Greif1c80d112008-08-28 21:40:38 +00003994 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003995 return DAG.getNode(ISD::UNDEF, VT);
3996
Gabor Greif1c80d112008-08-28 21:40:38 +00003997 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003998 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003999
Gabor Greif1c80d112008-08-28 21:40:38 +00004000 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004001 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00004002 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004003 return V2;
4004
Evan Chengae6c9212008-09-25 23:35:16 +00004005 // Canonicalize movddup shuffles.
4006 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Chengbdd9d9f2008-10-06 21:13:08 +00004007 VT.getSizeInBits() == 128 &&
Evan Chengae6c9212008-09-25 23:35:16 +00004008 X86::isMOVDDUPMask(PermMask.getNode()))
4009 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
4010
Gabor Greif1c80d112008-08-28 21:40:38 +00004011 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004012 if (isMMX || NumElems < 4) return Op;
4013 // Promote it to a v4{if}32 splat.
4014 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004015 }
4016
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004017 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4018 // do it!
4019 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004020 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004021 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004022 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
4023 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4024 // FIXME: Figure out a cleaner way to do this.
4025 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004026 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004027 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004028 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004029 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004030 SDValue NewV1 = NewOp.getOperand(0);
4031 SDValue NewV2 = NewOp.getOperand(1);
4032 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00004033 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004034 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00004035 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004036 }
4037 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004038 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004039 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004040 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004041 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004042 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00004043 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004044 }
4045 }
4046
Evan Chengdea99362008-05-29 08:22:04 +00004047 // Check if this can be converted into a logical shift.
4048 bool isLeft = false;
4049 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004050 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00004051 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4052 if (isShift && ShVal.hasOneUse()) {
4053 // If the shifted value has multiple uses, it may be cheaper to use
4054 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004055 MVT EVT = VT.getVectorElementType();
4056 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004057 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4058 }
4059
Gabor Greif1c80d112008-08-28 21:40:38 +00004060 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004061 if (V1IsUndef)
4062 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004063 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004064 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004065 if (!isMMX)
4066 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004067 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004068
Gabor Greif1c80d112008-08-28 21:40:38 +00004069 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4070 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4071 X86::isMOVHLPSMask(PermMask.getNode()) ||
4072 X86::isMOVHPMask(PermMask.getNode()) ||
4073 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004074 return Op;
4075
Gabor Greif1c80d112008-08-28 21:40:38 +00004076 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4077 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004078 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4079
Evan Chengdea99362008-05-29 08:22:04 +00004080 if (isShift) {
4081 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004082 MVT EVT = VT.getVectorElementType();
4083 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004084 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4085 }
4086
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004087 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004088 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4089 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004090 V1IsSplat = isSplatVector(V1.getNode());
4091 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004092
4093 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004094 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4095 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4096 std::swap(V1IsSplat, V2IsSplat);
4097 std::swap(V1IsUndef, V2IsUndef);
4098 Commuted = true;
4099 }
4100
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004101 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004102 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004103 if (V2IsUndef) return V1;
4104 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4105 if (V2IsSplat) {
4106 // V2 is a splat, so the mask may be malformed. That is, it may point
4107 // to any V2 element. The instruction selectior won't like this. Get
4108 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004109 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004110 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004111 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4112 }
4113 return Op;
4114 }
4115
Gabor Greif1c80d112008-08-28 21:40:38 +00004116 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4117 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4118 X86::isUNPCKLMask(PermMask.getNode()) ||
4119 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004120 return Op;
4121
4122 if (V2IsSplat) {
4123 // Normalize mask so all entries that point to V2 points to its first
4124 // element then try to match unpck{h|l} again. If match, return a
4125 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004126 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004127 if (NewMask.getNode() != PermMask.getNode()) {
4128 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004129 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004130 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004131 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004132 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004133 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4134 }
4135 }
4136 }
4137
4138 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004139 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004140 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4141
4142 if (Commuted) {
4143 // Commute is back and try unpck* again.
4144 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004145 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4146 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4147 X86::isUNPCKLMask(PermMask.getNode()) ||
4148 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004149 return Op;
4150 }
4151
Evan Chengbf8b2c52008-04-05 00:30:36 +00004152 // Try PSHUF* first, then SHUFP*.
4153 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4154 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004155 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004156 if (V2.getOpcode() != ISD::UNDEF)
4157 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4158 DAG.getNode(ISD::UNDEF, VT), PermMask);
4159 return Op;
4160 }
4161
4162 if (!isMMX) {
4163 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004164 (X86::isPSHUFDMask(PermMask.getNode()) ||
4165 X86::isPSHUFHWMask(PermMask.getNode()) ||
4166 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004167 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004168 if (VT == MVT::v4f32) {
4169 RVT = MVT::v4i32;
4170 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4171 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4172 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4173 } else if (V2.getOpcode() != ISD::UNDEF)
4174 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4175 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4176 if (RVT != VT)
4177 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004178 return Op;
4179 }
4180
Evan Chengbf8b2c52008-04-05 00:30:36 +00004181 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004182 if (X86::isSHUFPMask(PermMask.getNode()) ||
4183 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004184 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004185 }
4186
Evan Cheng75184a92007-12-11 01:46:18 +00004187 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4188 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004189 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004190 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004191 return NewOp;
4192 }
4193
Evan Chengf50554e2008-07-22 21:13:36 +00004194 // Handle all 4 wide cases with a number of shuffles except for MMX.
4195 if (NumElems == 4 && !isMMX)
4196 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004197
Dan Gohman8181bd12008-07-27 21:46:04 +00004198 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004199}
4200
Dan Gohman8181bd12008-07-27 21:46:04 +00004201SDValue
4202X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004203 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004204 MVT VT = Op.getValueType();
4205 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004206 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004207 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004208 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004209 DAG.getValueType(VT));
4210 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004211 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004212 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004213 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004214 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004215 DAG.getValueType(VT));
4216 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004217 } else if (VT == MVT::f32) {
4218 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4219 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004220 // result has a single use which is a store or a bitcast to i32. And in
4221 // the case of a store, it's not worth it if the index is a constant 0,
4222 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004223 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004224 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004225 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004226 if ((User->getOpcode() != ISD::STORE ||
4227 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4228 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004229 (User->getOpcode() != ISD::BIT_CONVERT ||
4230 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004231 return SDValue();
4232 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004233 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4234 Op.getOperand(1));
4235 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004236 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004237 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004238}
4239
4240
Dan Gohman8181bd12008-07-27 21:46:04 +00004241SDValue
4242X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004243 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004244 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004245
Evan Cheng6c249332008-03-24 21:52:23 +00004246 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004247 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004248 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004249 return Res;
4250 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004251
Duncan Sands92c43912008-06-06 12:08:01 +00004252 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004253 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004254 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004255 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004256 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004257 if (Idx == 0)
4258 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4259 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4260 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4261 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004262 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004263 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004264 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004265 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004266 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004267 DAG.getValueType(VT));
4268 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004269 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004270 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004271 if (Idx == 0)
4272 return Op;
4273 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004274 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004275 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004276 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004277 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004278 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004279 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004280 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004281 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004282 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004283 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004284 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004285 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004286 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004287 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4288 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4289 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004290 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004291 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004292 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4293 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4294 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004295 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004296 if (Idx == 0)
4297 return Op;
4298
4299 // UNPCKHPD the element to the lowest double word, then movsd.
4300 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4301 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004302 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004303 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004304 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004305 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004306 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004307 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004308 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004309 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004310 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4311 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4312 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004313 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004314 }
4315
Dan Gohman8181bd12008-07-27 21:46:04 +00004316 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004317}
4318
Dan Gohman8181bd12008-07-27 21:46:04 +00004319SDValue
4320X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004321 MVT VT = Op.getValueType();
4322 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004323
Dan Gohman8181bd12008-07-27 21:46:04 +00004324 SDValue N0 = Op.getOperand(0);
4325 SDValue N1 = Op.getOperand(1);
4326 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004327
Dan Gohman5a7af042008-08-14 22:53:18 +00004328 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4329 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004330 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004331 : X86ISD::PINSRW;
4332 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4333 // argument.
4334 if (N1.getValueType() != MVT::i32)
4335 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4336 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004337 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begemand77e59e2008-02-11 04:19:36 +00004338 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004339 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004340 // Bits [7:6] of the constant are the source select. This will always be
4341 // zero here. The DAG Combiner may combine an extract_elt index into these
4342 // bits. For example (insert (extract, 3), 2) could be matched by putting
4343 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4344 // Bits [5:4] of the constant are the destination select. This is the
4345 // value of the incoming immediate.
4346 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4347 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004348 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begemand77e59e2008-02-11 04:19:36 +00004349 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4350 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004351 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004352}
4353
Dan Gohman8181bd12008-07-27 21:46:04 +00004354SDValue
4355X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004356 MVT VT = Op.getValueType();
4357 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004358
4359 if (Subtarget->hasSSE41())
4360 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4361
Evan Chenge12a7eb2007-12-12 07:55:34 +00004362 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004363 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004364
Dan Gohman8181bd12008-07-27 21:46:04 +00004365 SDValue N0 = Op.getOperand(0);
4366 SDValue N1 = Op.getOperand(1);
4367 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004368
Duncan Sands92c43912008-06-06 12:08:01 +00004369 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004370 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4371 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004372 if (N1.getValueType() != MVT::i32)
4373 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4374 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004375 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004376 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004377 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004378 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004379}
4380
Dan Gohman8181bd12008-07-27 21:46:04 +00004381SDValue
4382X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004383 if (Op.getValueType() == MVT::v2f32)
4384 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4385 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4386 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4387 Op.getOperand(0))));
4388
Dan Gohman8181bd12008-07-27 21:46:04 +00004389 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004390 MVT VT = MVT::v2i32;
4391 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004392 default: break;
4393 case MVT::v16i8:
4394 case MVT::v8i16:
4395 VT = MVT::v4i32;
4396 break;
4397 }
4398 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4399 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004400}
4401
Bill Wendlingfef06052008-09-16 21:48:12 +00004402// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4403// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4404// one of the above mentioned nodes. It has to be wrapped because otherwise
4405// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4406// be used to form addressing mode. These wrapped nodes will be selected
4407// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004408SDValue
4409X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004410 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004411 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004412 getPointerTy(),
4413 CP->getAlignment());
4414 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4415 // With PIC, the address is actually $g + Offset.
4416 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4417 !Subtarget->isPICStyleRIPRel()) {
4418 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4419 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4420 Result);
4421 }
4422
4423 return Result;
4424}
4425
Dan Gohman8181bd12008-07-27 21:46:04 +00004426SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004427X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
Dan Gohman36322c72008-10-18 02:06:02 +00004428 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00004429 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00004430 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4431 bool ExtraLoadRequired =
4432 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4433
4434 // Create the TargetGlobalAddress node, folding in the constant
4435 // offset if it is legal.
4436 SDValue Result;
Dan Gohman3d5257c2008-10-21 03:38:42 +00004437 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +00004438 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4439 Offset = 0;
4440 } else
4441 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004442 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00004443
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004444 // With PIC, the address is actually $g + Offset.
Dan Gohman36322c72008-10-18 02:06:02 +00004445 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004446 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4447 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4448 Result);
4449 }
4450
4451 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4452 // load the value at address GV, not the value of GV itself. This means that
4453 // the GlobalAddress must be in the base or index register of the address, not
4454 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4455 // The same applies for external symbols during PIC codegen
Dan Gohman36322c72008-10-18 02:06:02 +00004456 if (ExtraLoadRequired)
Dan Gohman12a9c082008-02-06 22:27:42 +00004457 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004458 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004459
Dan Gohman36322c72008-10-18 02:06:02 +00004460 // If there was a non-zero offset that we didn't fold, create an explicit
4461 // addition for it.
4462 if (Offset != 0)
4463 Result = DAG.getNode(ISD::ADD, getPointerTy(), Result,
4464 DAG.getConstant(Offset, getPointerTy()));
4465
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004466 return Result;
4467}
4468
Evan Cheng7f250d62008-09-24 00:05:32 +00004469SDValue
4470X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4471 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00004472 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
4473 return LowerGlobalAddress(GV, Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00004474}
4475
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004476// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004477static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004478LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004479 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004480 SDValue InFlag;
4481 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004482 DAG.getNode(X86ISD::GlobalBaseReg,
4483 PtrVT), InFlag);
4484 InFlag = Chain.getValue(1);
4485
4486 // emit leal symbol@TLSGD(,%ebx,1), %eax
4487 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004488 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004489 GA->getValueType(0),
4490 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004491 SDValue Ops[] = { Chain, TGA, InFlag };
4492 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004493 InFlag = Result.getValue(2);
4494 Chain = Result.getValue(1);
4495
4496 // call ___tls_get_addr. This function receives its argument in
4497 // the register EAX.
4498 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4499 InFlag = Chain.getValue(1);
4500
4501 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004502 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004503 DAG.getTargetExternalSymbol("___tls_get_addr",
4504 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004505 DAG.getRegister(X86::EAX, PtrVT),
4506 DAG.getRegister(X86::EBX, PtrVT),
4507 InFlag };
4508 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4509 InFlag = Chain.getValue(1);
4510
4511 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4512}
4513
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004514// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004515static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004516LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004517 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004518 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004519
4520 // emit leaq symbol@TLSGD(%rip), %rdi
4521 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004522 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004523 GA->getValueType(0),
4524 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004525 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4526 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004527 Chain = Result.getValue(1);
4528 InFlag = Result.getValue(2);
4529
aslb204cd52008-08-16 12:58:29 +00004530 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004531 // the register RDI.
4532 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4533 InFlag = Chain.getValue(1);
4534
4535 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004536 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004537 DAG.getTargetExternalSymbol("__tls_get_addr",
4538 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004539 DAG.getRegister(X86::RDI, PtrVT),
4540 InFlag };
4541 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4542 InFlag = Chain.getValue(1);
4543
4544 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4545}
4546
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004547// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4548// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004549static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004550 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004551 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004552 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004553 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4554 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004555 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004556 GA->getValueType(0),
4557 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004558 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004559
4560 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004561 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004562 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004563
4564 // The address of the thread local variable is the add of the thread
4565 // pointer with the offset of the variable.
4566 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4567}
4568
Dan Gohman8181bd12008-07-27 21:46:04 +00004569SDValue
4570X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004571 // TODO: implement the "local dynamic" model
4572 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004573 assert(Subtarget->isTargetELF() &&
4574 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004575 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4576 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4577 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004578 if (Subtarget->is64Bit()) {
4579 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4580 } else {
4581 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4582 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4583 else
4584 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4585 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004586}
4587
Dan Gohman8181bd12008-07-27 21:46:04 +00004588SDValue
4589X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004590 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4591 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004592 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4593 // With PIC, the address is actually $g + Offset.
4594 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4595 !Subtarget->isPICStyleRIPRel()) {
4596 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4597 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4598 Result);
4599 }
4600
4601 return Result;
4602}
4603
Dan Gohman8181bd12008-07-27 21:46:04 +00004604SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004605 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004606 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004607 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4608 // With PIC, the address is actually $g + Offset.
4609 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4610 !Subtarget->isPICStyleRIPRel()) {
4611 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4612 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4613 Result);
4614 }
4615
4616 return Result;
4617}
4618
Chris Lattner62814a32007-10-17 06:02:13 +00004619/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4620/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004621SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004622 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004623 MVT VT = Op.getValueType();
4624 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004625 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004626 SDValue ShOpLo = Op.getOperand(0);
4627 SDValue ShOpHi = Op.getOperand(1);
4628 SDValue ShAmt = Op.getOperand(2);
4629 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004630 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4631 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004632
Dan Gohman8181bd12008-07-27 21:46:04 +00004633 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004634 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004635 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4636 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004637 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004638 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4639 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004640 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004641
Dan Gohman8181bd12008-07-27 21:46:04 +00004642 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004643 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004644 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004645 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004646
Dan Gohman8181bd12008-07-27 21:46:04 +00004647 SDValue Hi, Lo;
4648 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4649 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4650 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004651
Chris Lattner62814a32007-10-17 06:02:13 +00004652 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004653 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4654 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004655 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004656 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4657 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004658 }
4659
Dan Gohman8181bd12008-07-27 21:46:04 +00004660 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004661 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004662}
4663
Dan Gohman8181bd12008-07-27 21:46:04 +00004664SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004665 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004666 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004667 "Unknown SINT_TO_FP to lower!");
4668
4669 // These are really Legal; caller falls through into that case.
4670 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004671 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004672 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4673 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004674 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004675
Duncan Sands92c43912008-06-06 12:08:01 +00004676 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004677 MachineFunction &MF = DAG.getMachineFunction();
4678 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004679 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4680 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004681 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004682 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004683
4684 // Build the FILD
4685 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004686 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004687 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004688 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4689 else
4690 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004691 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004692 Ops.push_back(Chain);
4693 Ops.push_back(StackSlot);
4694 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004695 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004696 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004697
Dale Johannesen2fc20782007-09-14 22:26:36 +00004698 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004699 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004700 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004701
4702 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4703 // shouldn't be necessary except that RFP cannot be live across
4704 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4705 MachineFunction &MF = DAG.getMachineFunction();
4706 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004707 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004708 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004709 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004710 Ops.push_back(Chain);
4711 Ops.push_back(Result);
4712 Ops.push_back(StackSlot);
4713 Ops.push_back(DAG.getValueType(Op.getValueType()));
4714 Ops.push_back(InFlag);
4715 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004716 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004717 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004718 }
4719
4720 return Result;
4721}
4722
Dale Johannesena359b8b2008-10-21 20:50:01 +00004723SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4724 MVT SrcVT = Op.getOperand(0).getValueType();
4725 assert(SrcVT.getSimpleVT() == MVT::i64 && "Unknown UINT_TO_FP to lower!");
4726
4727 // We only handle SSE2 f64 target here; caller can handle the rest.
4728 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4729 return SDValue();
4730
Dale Johannesenfb019af2008-10-21 23:07:49 +00004731 // This algorithm is not obvious. Here it is in C code, more or less:
4732/*
4733 double uint64_to_double( uint32_t hi, uint32_t lo )
4734 {
4735 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4736 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
4737
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004738 // copy ints to xmm registers
Dale Johannesenfb019af2008-10-21 23:07:49 +00004739 __m128i xh = _mm_cvtsi32_si128( hi );
4740 __m128i xl = _mm_cvtsi32_si128( lo );
4741
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004742 // combine into low half of a single xmm register
Dale Johannesenfb019af2008-10-21 23:07:49 +00004743 __m128i x = _mm_unpacklo_epi32( xh, xl );
4744 __m128d d;
4745 double sd;
4746
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004747 // merge in appropriate exponents to give the integer bits the
Dale Johannesenfb019af2008-10-21 23:07:49 +00004748 // right magnitude
4749 x = _mm_unpacklo_epi32( x, exp );
4750
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004751 // subtract away the biases to deal with the IEEE-754 double precision
4752 // implicit 1
Dale Johannesenfb019af2008-10-21 23:07:49 +00004753 d = _mm_sub_pd( (__m128d) x, bias );
4754
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004755 // All conversions up to here are exact. The correctly rounded result is
Dale Johannesenfb019af2008-10-21 23:07:49 +00004756 // calculated using the
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004757 // current rounding mode using the following horizontal add.
Dale Johannesenfb019af2008-10-21 23:07:49 +00004758 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4759 _mm_store_sd( &sd, d ); //since we are returning doubles in XMM, this
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004760 // store doesn't really need to be here (except maybe to zero the other
4761 // double)
Dale Johannesenfb019af2008-10-21 23:07:49 +00004762 return sd;
4763 }
4764*/
4765
Dale Johannesena359b8b2008-10-21 20:50:01 +00004766 // Build some magic constants.
4767 std::vector<Constant*>CV0;
4768 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4769 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4770 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4771 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4772 Constant *C0 = ConstantVector::get(CV0);
4773 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 4);
4774
4775 std::vector<Constant*>CV1;
4776 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4777 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4778 Constant *C1 = ConstantVector::get(CV1);
4779 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 4);
4780
4781 SmallVector<SDValue, 4> MaskVec;
4782 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
4783 MaskVec.push_back(DAG.getConstant(4, MVT::i32));
4784 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
4785 MaskVec.push_back(DAG.getConstant(5, MVT::i32));
4786 SDValue UnpcklMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, &MaskVec[0],
4787 MaskVec.size());
4788 SmallVector<SDValue, 4> MaskVec2;
Duncan Sandsca872ca2008-10-22 11:24:12 +00004789 MaskVec2.push_back(DAG.getConstant(1, MVT::i32));
4790 MaskVec2.push_back(DAG.getConstant(0, MVT::i32));
4791 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec2[0],
Dale Johannesena359b8b2008-10-21 20:50:01 +00004792 MaskVec2.size());
4793
4794 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004795 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4796 Op.getOperand(0),
4797 DAG.getIntPtrConstant(1)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004798 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004799 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4800 Op.getOperand(0),
4801 DAG.getIntPtrConstant(0)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004802 SDValue Unpck1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4803 XR1, XR2, UnpcklMask);
4804 SDValue CLod0 = DAG.getLoad(MVT::v4i32, DAG.getEntryNode(), CPIdx0,
4805 PseudoSourceValue::getConstantPool(), 0, false, 16);
4806 SDValue Unpck2 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4807 Unpck1, CLod0, UnpcklMask);
4808 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Unpck2);
4809 SDValue CLod1 = DAG.getLoad(MVT::v2f64, CLod0.getValue(1), CPIdx1,
4810 PseudoSourceValue::getConstantPool(), 0, false, 16);
4811 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::v2f64, XR2F, CLod1);
4812 // Add the halves; easiest way is to swap them into another reg first.
4813 SDValue Shuf = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2f64,
4814 Sub, Sub, ShufMask);
4815 SDValue Add = DAG.getNode(ISD::FADD, MVT::v2f64, Shuf, Sub);
4816 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64, Add,
4817 DAG.getIntPtrConstant(0));
4818}
4819
Dan Gohman8181bd12008-07-27 21:46:04 +00004820std::pair<SDValue,SDValue> X86TargetLowering::
4821FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004822 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4823 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004824 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004825
Dale Johannesen2fc20782007-09-14 22:26:36 +00004826 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004827 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004828 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004829 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004830 if (Subtarget->is64Bit() &&
4831 Op.getValueType() == MVT::i64 &&
4832 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004833 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004834
Evan Cheng05441e62007-10-15 20:11:21 +00004835 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4836 // stack slot.
4837 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004838 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004839 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004840 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004841 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004842 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004843 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4844 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4845 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4846 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004847 }
4848
Dan Gohman8181bd12008-07-27 21:46:04 +00004849 SDValue Chain = DAG.getEntryNode();
4850 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004851 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004852 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004853 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004854 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004855 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004856 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004857 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4858 };
4859 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4860 Chain = Value.getValue(1);
4861 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4862 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4863 }
4864
4865 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004866 SDValue Ops[] = { Chain, Value, StackSlot };
4867 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004868
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004869 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004870}
4871
Dan Gohman8181bd12008-07-27 21:46:04 +00004872SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4873 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4874 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004875 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004876
4877 // Load the result.
4878 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4879}
4880
Dan Gohman8181bd12008-07-27 21:46:04 +00004881SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004882 MVT VT = Op.getValueType();
4883 MVT EltVT = VT;
4884 if (VT.isVector())
4885 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004886 std::vector<Constant*> CV;
4887 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004888 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004889 CV.push_back(C);
4890 CV.push_back(C);
4891 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004892 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004893 CV.push_back(C);
4894 CV.push_back(C);
4895 CV.push_back(C);
4896 CV.push_back(C);
4897 }
Dan Gohman11821702007-07-27 17:16:43 +00004898 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004899 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4900 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004901 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004902 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004903 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4904}
4905
Dan Gohman8181bd12008-07-27 21:46:04 +00004906SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004907 MVT VT = Op.getValueType();
4908 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004909 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004910 if (VT.isVector()) {
4911 EltVT = VT.getVectorElementType();
4912 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004913 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004914 std::vector<Constant*> CV;
4915 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004916 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004917 CV.push_back(C);
4918 CV.push_back(C);
4919 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004920 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004921 CV.push_back(C);
4922 CV.push_back(C);
4923 CV.push_back(C);
4924 CV.push_back(C);
4925 }
Dan Gohman11821702007-07-27 17:16:43 +00004926 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004927 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4928 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004929 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004930 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004931 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004932 return DAG.getNode(ISD::BIT_CONVERT, VT,
4933 DAG.getNode(ISD::XOR, MVT::v2i64,
4934 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4935 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4936 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004937 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4938 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004939}
4940
Dan Gohman8181bd12008-07-27 21:46:04 +00004941SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4942 SDValue Op0 = Op.getOperand(0);
4943 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004944 MVT VT = Op.getValueType();
4945 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004946
4947 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004948 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004949 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4950 SrcVT = VT;
4951 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004952 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004953 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004954 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004955 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004956 }
4957
4958 // At this point the operands and the result should have the same
4959 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004960
4961 // First get the sign bit of second operand.
4962 std::vector<Constant*> CV;
4963 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004964 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4965 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004966 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004967 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4968 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4969 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4970 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004971 }
Dan Gohman11821702007-07-27 17:16:43 +00004972 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004973 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4974 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004975 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004976 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004977 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004978
4979 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004980 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004981 // Op0 is MVT::f32, Op1 is MVT::f64.
4982 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4983 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4984 DAG.getConstant(32, MVT::i32));
4985 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4986 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004987 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004988 }
4989
4990 // Clear first operand sign bit.
4991 CV.clear();
4992 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004993 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4994 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004995 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004996 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4997 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4998 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4999 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005000 }
Dan Gohman11821702007-07-27 17:16:43 +00005001 C = ConstantVector::get(CV);
5002 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00005003 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005004 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005005 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00005006 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005007
5008 // Or the value with the sign bit.
5009 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
5010}
5011
Dan Gohman8181bd12008-07-27 21:46:04 +00005012SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00005013 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00005014 SDValue Cond;
5015 SDValue Op0 = Op.getOperand(0);
5016 SDValue Op1 = Op.getOperand(1);
5017 SDValue CC = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00005018 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00005019 unsigned X86CC;
5020
Evan Cheng950aac02007-09-25 01:57:46 +00005021 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00005022 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00005023 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
5024 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00005025 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00005026 }
Evan Cheng950aac02007-09-25 01:57:46 +00005027
Evan Cheng71343822008-10-15 02:05:31 +00005028 assert(0 && "Illegal SetCC!");
5029 return SDValue();
Evan Cheng950aac02007-09-25 01:57:46 +00005030}
5031
Dan Gohman8181bd12008-07-27 21:46:04 +00005032SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5033 SDValue Cond;
5034 SDValue Op0 = Op.getOperand(0);
5035 SDValue Op1 = Op.getOperand(1);
5036 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00005037 MVT VT = Op.getValueType();
5038 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5039 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5040
5041 if (isFP) {
5042 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00005043 MVT VT0 = Op0.getValueType();
5044 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5045 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00005046 bool Swap = false;
5047
5048 switch (SetCCOpcode) {
5049 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005050 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00005051 case ISD::SETEQ: SSECC = 0; break;
5052 case ISD::SETOGT:
5053 case ISD::SETGT: Swap = true; // Fallthrough
5054 case ISD::SETLT:
5055 case ISD::SETOLT: SSECC = 1; break;
5056 case ISD::SETOGE:
5057 case ISD::SETGE: Swap = true; // Fallthrough
5058 case ISD::SETLE:
5059 case ISD::SETOLE: SSECC = 2; break;
5060 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005061 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00005062 case ISD::SETNE: SSECC = 4; break;
5063 case ISD::SETULE: Swap = true;
5064 case ISD::SETUGE: SSECC = 5; break;
5065 case ISD::SETULT: Swap = true;
5066 case ISD::SETUGT: SSECC = 6; break;
5067 case ISD::SETO: SSECC = 7; break;
5068 }
5069 if (Swap)
5070 std::swap(Op0, Op1);
5071
Nate Begeman6357f9d2008-07-25 19:05:58 +00005072 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00005073 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00005074 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005075 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005076 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5077 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5078 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
5079 }
5080 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005081 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005082 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5083 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5084 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
5085 }
5086 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00005087 }
5088 // Handle all other FP comparisons here.
5089 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
5090 }
5091
5092 // We are handling one of the integer comparisons here. Since SSE only has
5093 // GT and EQ comparisons for integer, swapping operands and multiple
5094 // operations may be required for some comparisons.
5095 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5096 bool Swap = false, Invert = false, FlipSigns = false;
5097
5098 switch (VT.getSimpleVT()) {
5099 default: break;
5100 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5101 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5102 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5103 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5104 }
5105
5106 switch (SetCCOpcode) {
5107 default: break;
5108 case ISD::SETNE: Invert = true;
5109 case ISD::SETEQ: Opc = EQOpc; break;
5110 case ISD::SETLT: Swap = true;
5111 case ISD::SETGT: Opc = GTOpc; break;
5112 case ISD::SETGE: Swap = true;
5113 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5114 case ISD::SETULT: Swap = true;
5115 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5116 case ISD::SETUGE: Swap = true;
5117 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5118 }
5119 if (Swap)
5120 std::swap(Op0, Op1);
5121
5122 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5123 // bits of the inputs before performing those operations.
5124 if (FlipSigns) {
5125 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005126 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
5127 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
5128 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005129 SignBits.size());
5130 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
5131 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
5132 }
5133
Dan Gohman8181bd12008-07-27 21:46:04 +00005134 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005135
5136 // If the logical-not of the result is required, perform that now.
5137 if (Invert) {
5138 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005139 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
5140 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
5141 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005142 NegOnes.size());
5143 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
5144 }
5145 return Result;
5146}
Evan Cheng950aac02007-09-25 01:57:46 +00005147
Evan Chengd580f022008-12-03 08:38:43 +00005148// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
5149static bool isX86LogicalCmp(unsigned Opc) {
5150 return Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI;
5151}
5152
Dan Gohman8181bd12008-07-27 21:46:04 +00005153SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005154 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005155 SDValue Cond = Op.getOperand(0);
5156 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005157
5158 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005159 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005160
Evan Cheng50d37ab2007-10-08 22:16:29 +00005161 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5162 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005163 if (Cond.getOpcode() == X86ISD::SETCC) {
5164 CC = Cond.getOperand(0);
5165
Dan Gohman8181bd12008-07-27 21:46:04 +00005166 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005167 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005168 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005169
Evan Cheng50d37ab2007-10-08 22:16:29 +00005170 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005171 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005172 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005173 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005174
Evan Chengd580f022008-12-03 08:38:43 +00005175 if (isX86LogicalCmp(Opc) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005176 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005177 addTest = false;
5178 }
5179 }
5180
5181 if (addTest) {
5182 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00005183 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005184 }
5185
Duncan Sands92c43912008-06-06 12:08:01 +00005186 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005187 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005188 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005189 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5190 // condition is true.
5191 Ops.push_back(Op.getOperand(2));
5192 Ops.push_back(Op.getOperand(1));
5193 Ops.push_back(CC);
5194 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00005195 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005196}
5197
Evan Chengd580f022008-12-03 08:38:43 +00005198// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5199// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5200// from the AND / OR.
5201static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5202 Opc = Op.getOpcode();
5203 if (Opc != ISD::OR && Opc != ISD::AND)
5204 return false;
5205 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5206 Op.getOperand(0).hasOneUse() &&
5207 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5208 Op.getOperand(1).hasOneUse());
5209}
5210
Dan Gohman8181bd12008-07-27 21:46:04 +00005211SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005212 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005213 SDValue Chain = Op.getOperand(0);
5214 SDValue Cond = Op.getOperand(1);
5215 SDValue Dest = Op.getOperand(2);
5216 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005217
5218 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005219 Cond = LowerSETCC(Cond, DAG);
Bill Wendlingf5399032008-12-12 21:15:41 +00005220 else if (Cond.getOpcode() == X86ISD::ADD ||
5221 Cond.getOpcode() == X86ISD::SUB ||
5222 Cond.getOpcode() == X86ISD::SMUL ||
5223 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling7e04be62008-12-09 22:08:41 +00005224 Cond = LowerXALUO(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005225
Evan Cheng50d37ab2007-10-08 22:16:29 +00005226 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5227 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005228 if (Cond.getOpcode() == X86ISD::SETCC) {
5229 CC = Cond.getOperand(0);
5230
Dan Gohman8181bd12008-07-27 21:46:04 +00005231 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005232 unsigned Opc = Cmp.getOpcode();
Evan Chengd580f022008-12-03 08:38:43 +00005233 if (isX86LogicalCmp(Opc)) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005234 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005235 addTest = false;
Bill Wendlingd3511522008-12-02 01:06:39 +00005236 } else {
Evan Chengd580f022008-12-03 08:38:43 +00005237 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling809e7bd2008-12-03 08:32:02 +00005238 default: break;
5239 case X86::COND_O:
5240 case X86::COND_C:
Evan Chengd580f022008-12-03 08:38:43 +00005241 // These can only come from an arithmetic instruction with overflow, e.g.
5242 // SADDO, UADDO.
Bill Wendling809e7bd2008-12-03 08:32:02 +00005243 Cond = Cond.getNode()->getOperand(1);
5244 addTest = false;
5245 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00005246 }
Evan Cheng950aac02007-09-25 01:57:46 +00005247 }
Evan Chengd580f022008-12-03 08:38:43 +00005248 } else {
5249 unsigned CondOpc;
5250 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5251 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5252 unsigned Opc = Cmp.getOpcode();
5253 if (CondOpc == ISD::OR) {
5254 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5255 // two branches instead of an explicit OR instruction with a
5256 // separate test.
5257 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5258 isX86LogicalCmp(Opc)) {
5259 CC = Cond.getOperand(0).getOperand(0);
5260 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5261 Chain, Dest, CC, Cmp);
5262 CC = Cond.getOperand(1).getOperand(0);
5263 Cond = Cmp;
5264 addTest = false;
5265 }
5266 } else { // ISD::AND
5267 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5268 // two branches instead of an explicit AND instruction with a
5269 // separate test. However, we only do this if this block doesn't
5270 // have a fall-through edge, because this requires an explicit
5271 // jmp when the condition is false.
5272 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5273 isX86LogicalCmp(Opc) &&
5274 Op.getNode()->hasOneUse()) {
5275 X86::CondCode CCode =
5276 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5277 CCode = X86::GetOppositeBranchCondition(CCode);
5278 CC = DAG.getConstant(CCode, MVT::i8);
5279 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5280 // Look for an unconditional branch following this conditional branch.
5281 // We need this because we need to reverse the successors in order
5282 // to implement FCMP_OEQ.
5283 if (User.getOpcode() == ISD::BR) {
5284 SDValue FalseBB = User.getOperand(1);
5285 SDValue NewBR =
5286 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5287 assert(NewBR == User);
5288 Dest = FalseBB;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005289
Evan Chengd580f022008-12-03 08:38:43 +00005290 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5291 Chain, Dest, CC, Cmp);
5292 X86::CondCode CCode =
5293 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5294 CCode = X86::GetOppositeBranchCondition(CCode);
5295 CC = DAG.getConstant(CCode, MVT::i8);
5296 Cond = Cmp;
5297 addTest = false;
5298 }
5299 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005300 }
5301 }
Evan Cheng950aac02007-09-25 01:57:46 +00005302 }
5303
5304 if (addTest) {
5305 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005306 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005307 }
Evan Cheng621216e2007-09-29 00:00:36 +00005308 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005309 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005310}
5311
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005312
5313// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5314// Calls to _alloca is needed to probe the stack when allocating more than 4k
5315// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5316// that the guard pages used by the OS virtual memory manager are allocated in
5317// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005318SDValue
5319X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005320 SelectionDAG &DAG) {
5321 assert(Subtarget->isTargetCygMing() &&
5322 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005323
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005324 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005325 SDValue Chain = Op.getOperand(0);
5326 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005327 // FIXME: Ensure alignment here
5328
Dan Gohman8181bd12008-07-27 21:46:04 +00005329 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005330
Duncan Sands92c43912008-06-06 12:08:01 +00005331 MVT IntPtr = getPointerTy();
5332 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005333
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005334 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005335
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005336 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5337 Flag = Chain.getValue(1);
5338
5339 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005340 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005341 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005342 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005343 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005344 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005345 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005346 Flag = Chain.getValue(1);
5347
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005348 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005349 DAG.getIntPtrConstant(0, true),
5350 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005351 Flag);
5352
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005353 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005354
Dan Gohman8181bd12008-07-27 21:46:04 +00005355 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005356 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005357}
5358
Dan Gohman8181bd12008-07-27 21:46:04 +00005359SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005360X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005361 SDValue Chain,
5362 SDValue Dst, SDValue Src,
5363 SDValue Size, unsigned Align,
5364 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005365 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005366 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005367
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005368 // If not DWORD aligned or size is more than the threshold, call the library.
5369 // The libc version is likely to be faster for these cases. It can use the
5370 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005371 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005372 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005373 ConstantSize->getZExtValue() >
5374 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005375 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005376
5377 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005378 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005379
Bill Wendling4b2e3782008-10-01 00:59:58 +00005380 if (const char *bzeroEntry = V &&
5381 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5382 MVT IntPtr = getPointerTy();
5383 const Type *IntPtrTy = TD->getIntPtrType();
5384 TargetLowering::ArgListTy Args;
5385 TargetLowering::ArgListEntry Entry;
5386 Entry.Node = Dst;
5387 Entry.Ty = IntPtrTy;
5388 Args.push_back(Entry);
5389 Entry.Node = Size;
5390 Args.push_back(Entry);
5391 std::pair<SDValue,SDValue> CallResult =
5392 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5393 CallingConv::C, false,
5394 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG);
5395 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005396 }
5397
Dan Gohmane8b391e2008-04-12 04:36:06 +00005398 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005399 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005400 }
5401
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005402 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005403 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005404 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005405 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005406 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005407 unsigned BytesLeft = 0;
5408 bool TwoRepStos = false;
5409 if (ValC) {
5410 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005411 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005412
5413 // If the value is a constant, then we can potentially use larger sets.
5414 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005415 case 2: // WORD aligned
5416 AVT = MVT::i16;
5417 ValReg = X86::AX;
5418 Val = (Val << 8) | Val;
5419 break;
5420 case 0: // DWORD aligned
5421 AVT = MVT::i32;
5422 ValReg = X86::EAX;
5423 Val = (Val << 8) | Val;
5424 Val = (Val << 16) | Val;
5425 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5426 AVT = MVT::i64;
5427 ValReg = X86::RAX;
5428 Val = (Val << 32) | Val;
5429 }
5430 break;
5431 default: // Byte aligned
5432 AVT = MVT::i8;
5433 ValReg = X86::AL;
5434 Count = DAG.getIntPtrConstant(SizeVal);
5435 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005436 }
5437
Duncan Sandsec142ee2008-06-08 20:54:56 +00005438 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005439 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005440 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5441 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005442 }
5443
5444 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5445 InFlag);
5446 InFlag = Chain.getValue(1);
5447 } else {
5448 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005449 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005450 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005451 InFlag = Chain.getValue(1);
5452 }
5453
5454 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5455 Count, InFlag);
5456 InFlag = Chain.getValue(1);
5457 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005458 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005459 InFlag = Chain.getValue(1);
5460
5461 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005462 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005463 Ops.push_back(Chain);
5464 Ops.push_back(DAG.getValueType(AVT));
5465 Ops.push_back(InFlag);
5466 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5467
5468 if (TwoRepStos) {
5469 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005470 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005471 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005472 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005473 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5474 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5475 Left, InFlag);
5476 InFlag = Chain.getValue(1);
5477 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5478 Ops.clear();
5479 Ops.push_back(Chain);
5480 Ops.push_back(DAG.getValueType(MVT::i8));
5481 Ops.push_back(InFlag);
5482 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5483 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005484 // Handle the last 1 - 7 bytes.
5485 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005486 MVT AddrVT = Dst.getValueType();
5487 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005488
5489 Chain = DAG.getMemset(Chain,
5490 DAG.getNode(ISD::ADD, AddrVT, Dst,
5491 DAG.getConstant(Offset, AddrVT)),
5492 Src,
5493 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005494 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005495 }
5496
Dan Gohmane8b391e2008-04-12 04:36:06 +00005497 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005498 return Chain;
5499}
5500
Dan Gohman8181bd12008-07-27 21:46:04 +00005501SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005502X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005503 SDValue Chain, SDValue Dst, SDValue Src,
5504 SDValue Size, unsigned Align,
5505 bool AlwaysInline,
5506 const Value *DstSV, uint64_t DstSVOff,
5507 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005508 // This requires the copy size to be a constant, preferrably
5509 // within a subtarget-specific limit.
5510 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5511 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005512 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005513 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005514 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005515 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005516
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005517 /// If not DWORD aligned, call the library.
5518 if ((Align & 3) != 0)
5519 return SDValue();
5520
5521 // DWORD aligned
5522 MVT AVT = MVT::i32;
5523 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005524 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005525
Duncan Sands92c43912008-06-06 12:08:01 +00005526 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005527 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005528 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005529 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005530
Dan Gohman8181bd12008-07-27 21:46:04 +00005531 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005532 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5533 Count, InFlag);
5534 InFlag = Chain.getValue(1);
5535 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005536 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005537 InFlag = Chain.getValue(1);
5538 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005539 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005540 InFlag = Chain.getValue(1);
5541
5542 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005543 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005544 Ops.push_back(Chain);
5545 Ops.push_back(DAG.getValueType(AVT));
5546 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005547 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005548
Dan Gohman8181bd12008-07-27 21:46:04 +00005549 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005550 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005551 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005552 // Handle the last 1 - 7 bytes.
5553 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005554 MVT DstVT = Dst.getValueType();
5555 MVT SrcVT = Src.getValueType();
5556 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005557 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005558 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005559 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005560 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005561 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005562 DAG.getConstant(BytesLeft, SizeVT),
5563 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005564 DstSV, DstSVOff + Offset,
5565 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005566 }
5567
Dan Gohmane8b391e2008-04-12 04:36:06 +00005568 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005569}
5570
Dan Gohman8181bd12008-07-27 21:46:04 +00005571SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005572 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005573
5574 if (!Subtarget->is64Bit()) {
5575 // vastart just stores the address of the VarArgsFrameIndex slot into the
5576 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005577 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005578 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005579 }
5580
5581 // __va_list_tag:
5582 // gp_offset (0 - 6 * 8)
5583 // fp_offset (48 - 48 + 8 * 16)
5584 // overflow_arg_area (point to parameters coming in memory).
5585 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005586 SmallVector<SDValue, 8> MemOps;
5587 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005588 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005589 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005590 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005591 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005592 MemOps.push_back(Store);
5593
5594 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005595 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005596 Store = DAG.getStore(Op.getOperand(0),
5597 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005598 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005599 MemOps.push_back(Store);
5600
5601 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005602 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005603 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005604 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005605 MemOps.push_back(Store);
5606
5607 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005608 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005609 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005610 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005611 MemOps.push_back(Store);
5612 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5613}
5614
Dan Gohman8181bd12008-07-27 21:46:04 +00005615SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005616 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5617 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005618 SDValue Chain = Op.getOperand(0);
5619 SDValue SrcPtr = Op.getOperand(1);
5620 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005621
5622 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5623 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005624 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005625}
5626
Dan Gohman8181bd12008-07-27 21:46:04 +00005627SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005628 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005629 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005630 SDValue Chain = Op.getOperand(0);
5631 SDValue DstPtr = Op.getOperand(1);
5632 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005633 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5634 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005635
Dan Gohman840ff5c2008-04-18 20:55:41 +00005636 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5637 DAG.getIntPtrConstant(24), 8, false,
5638 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005639}
5640
Dan Gohman8181bd12008-07-27 21:46:04 +00005641SDValue
5642X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005643 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005644 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005645 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005646 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005647 case Intrinsic::x86_sse_comieq_ss:
5648 case Intrinsic::x86_sse_comilt_ss:
5649 case Intrinsic::x86_sse_comile_ss:
5650 case Intrinsic::x86_sse_comigt_ss:
5651 case Intrinsic::x86_sse_comige_ss:
5652 case Intrinsic::x86_sse_comineq_ss:
5653 case Intrinsic::x86_sse_ucomieq_ss:
5654 case Intrinsic::x86_sse_ucomilt_ss:
5655 case Intrinsic::x86_sse_ucomile_ss:
5656 case Intrinsic::x86_sse_ucomigt_ss:
5657 case Intrinsic::x86_sse_ucomige_ss:
5658 case Intrinsic::x86_sse_ucomineq_ss:
5659 case Intrinsic::x86_sse2_comieq_sd:
5660 case Intrinsic::x86_sse2_comilt_sd:
5661 case Intrinsic::x86_sse2_comile_sd:
5662 case Intrinsic::x86_sse2_comigt_sd:
5663 case Intrinsic::x86_sse2_comige_sd:
5664 case Intrinsic::x86_sse2_comineq_sd:
5665 case Intrinsic::x86_sse2_ucomieq_sd:
5666 case Intrinsic::x86_sse2_ucomilt_sd:
5667 case Intrinsic::x86_sse2_ucomile_sd:
5668 case Intrinsic::x86_sse2_ucomigt_sd:
5669 case Intrinsic::x86_sse2_ucomige_sd:
5670 case Intrinsic::x86_sse2_ucomineq_sd: {
5671 unsigned Opc = 0;
5672 ISD::CondCode CC = ISD::SETCC_INVALID;
5673 switch (IntNo) {
5674 default: break;
5675 case Intrinsic::x86_sse_comieq_ss:
5676 case Intrinsic::x86_sse2_comieq_sd:
5677 Opc = X86ISD::COMI;
5678 CC = ISD::SETEQ;
5679 break;
5680 case Intrinsic::x86_sse_comilt_ss:
5681 case Intrinsic::x86_sse2_comilt_sd:
5682 Opc = X86ISD::COMI;
5683 CC = ISD::SETLT;
5684 break;
5685 case Intrinsic::x86_sse_comile_ss:
5686 case Intrinsic::x86_sse2_comile_sd:
5687 Opc = X86ISD::COMI;
5688 CC = ISD::SETLE;
5689 break;
5690 case Intrinsic::x86_sse_comigt_ss:
5691 case Intrinsic::x86_sse2_comigt_sd:
5692 Opc = X86ISD::COMI;
5693 CC = ISD::SETGT;
5694 break;
5695 case Intrinsic::x86_sse_comige_ss:
5696 case Intrinsic::x86_sse2_comige_sd:
5697 Opc = X86ISD::COMI;
5698 CC = ISD::SETGE;
5699 break;
5700 case Intrinsic::x86_sse_comineq_ss:
5701 case Intrinsic::x86_sse2_comineq_sd:
5702 Opc = X86ISD::COMI;
5703 CC = ISD::SETNE;
5704 break;
5705 case Intrinsic::x86_sse_ucomieq_ss:
5706 case Intrinsic::x86_sse2_ucomieq_sd:
5707 Opc = X86ISD::UCOMI;
5708 CC = ISD::SETEQ;
5709 break;
5710 case Intrinsic::x86_sse_ucomilt_ss:
5711 case Intrinsic::x86_sse2_ucomilt_sd:
5712 Opc = X86ISD::UCOMI;
5713 CC = ISD::SETLT;
5714 break;
5715 case Intrinsic::x86_sse_ucomile_ss:
5716 case Intrinsic::x86_sse2_ucomile_sd:
5717 Opc = X86ISD::UCOMI;
5718 CC = ISD::SETLE;
5719 break;
5720 case Intrinsic::x86_sse_ucomigt_ss:
5721 case Intrinsic::x86_sse2_ucomigt_sd:
5722 Opc = X86ISD::UCOMI;
5723 CC = ISD::SETGT;
5724 break;
5725 case Intrinsic::x86_sse_ucomige_ss:
5726 case Intrinsic::x86_sse2_ucomige_sd:
5727 Opc = X86ISD::UCOMI;
5728 CC = ISD::SETGE;
5729 break;
5730 case Intrinsic::x86_sse_ucomineq_ss:
5731 case Intrinsic::x86_sse2_ucomineq_sd:
5732 Opc = X86ISD::UCOMI;
5733 CC = ISD::SETNE;
5734 break;
5735 }
5736
5737 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005738 SDValue LHS = Op.getOperand(1);
5739 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005740 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5741
Dan Gohman8181bd12008-07-27 21:46:04 +00005742 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5743 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005744 DAG.getConstant(X86CC, MVT::i8), Cond);
5745 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005746 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005747
5748 // Fix vector shift instructions where the last operand is a non-immediate
5749 // i32 value.
5750 case Intrinsic::x86_sse2_pslli_w:
5751 case Intrinsic::x86_sse2_pslli_d:
5752 case Intrinsic::x86_sse2_pslli_q:
5753 case Intrinsic::x86_sse2_psrli_w:
5754 case Intrinsic::x86_sse2_psrli_d:
5755 case Intrinsic::x86_sse2_psrli_q:
5756 case Intrinsic::x86_sse2_psrai_w:
5757 case Intrinsic::x86_sse2_psrai_d:
5758 case Intrinsic::x86_mmx_pslli_w:
5759 case Intrinsic::x86_mmx_pslli_d:
5760 case Intrinsic::x86_mmx_pslli_q:
5761 case Intrinsic::x86_mmx_psrli_w:
5762 case Intrinsic::x86_mmx_psrli_d:
5763 case Intrinsic::x86_mmx_psrli_q:
5764 case Intrinsic::x86_mmx_psrai_w:
5765 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005766 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005767 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005768 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005769
5770 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005771 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005772 switch (IntNo) {
5773 case Intrinsic::x86_sse2_pslli_w:
5774 NewIntNo = Intrinsic::x86_sse2_psll_w;
5775 break;
5776 case Intrinsic::x86_sse2_pslli_d:
5777 NewIntNo = Intrinsic::x86_sse2_psll_d;
5778 break;
5779 case Intrinsic::x86_sse2_pslli_q:
5780 NewIntNo = Intrinsic::x86_sse2_psll_q;
5781 break;
5782 case Intrinsic::x86_sse2_psrli_w:
5783 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5784 break;
5785 case Intrinsic::x86_sse2_psrli_d:
5786 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5787 break;
5788 case Intrinsic::x86_sse2_psrli_q:
5789 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5790 break;
5791 case Intrinsic::x86_sse2_psrai_w:
5792 NewIntNo = Intrinsic::x86_sse2_psra_w;
5793 break;
5794 case Intrinsic::x86_sse2_psrai_d:
5795 NewIntNo = Intrinsic::x86_sse2_psra_d;
5796 break;
5797 default: {
5798 ShAmtVT = MVT::v2i32;
5799 switch (IntNo) {
5800 case Intrinsic::x86_mmx_pslli_w:
5801 NewIntNo = Intrinsic::x86_mmx_psll_w;
5802 break;
5803 case Intrinsic::x86_mmx_pslli_d:
5804 NewIntNo = Intrinsic::x86_mmx_psll_d;
5805 break;
5806 case Intrinsic::x86_mmx_pslli_q:
5807 NewIntNo = Intrinsic::x86_mmx_psll_q;
5808 break;
5809 case Intrinsic::x86_mmx_psrli_w:
5810 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5811 break;
5812 case Intrinsic::x86_mmx_psrli_d:
5813 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5814 break;
5815 case Intrinsic::x86_mmx_psrli_q:
5816 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5817 break;
5818 case Intrinsic::x86_mmx_psrai_w:
5819 NewIntNo = Intrinsic::x86_mmx_psra_w;
5820 break;
5821 case Intrinsic::x86_mmx_psrai_d:
5822 NewIntNo = Intrinsic::x86_mmx_psra_d;
5823 break;
5824 default: abort(); // Can't reach here.
5825 }
5826 break;
5827 }
5828 }
Duncan Sands92c43912008-06-06 12:08:01 +00005829 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005830 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5831 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5832 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5833 DAG.getConstant(NewIntNo, MVT::i32),
5834 Op.getOperand(1), ShAmt);
5835 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005836 }
5837}
5838
Dan Gohman8181bd12008-07-27 21:46:04 +00005839SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005840 // Depths > 0 not supported yet!
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005841 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005842 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005843
5844 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005845 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005846 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5847}
5848
Dan Gohman8181bd12008-07-27 21:46:04 +00005849SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00005850 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
5851 MFI->setFrameAddressIsTaken(true);
5852 MVT VT = Op.getValueType();
5853 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5854 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
5855 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), FrameReg, VT);
5856 while (Depth--)
5857 FrameAddr = DAG.getLoad(VT, DAG.getEntryNode(), FrameAddr, NULL, 0);
5858 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005859}
5860
Dan Gohman8181bd12008-07-27 21:46:04 +00005861SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005862 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005863 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005864}
5865
Dan Gohman8181bd12008-07-27 21:46:04 +00005866SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005867{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005868 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005869 SDValue Chain = Op.getOperand(0);
5870 SDValue Offset = Op.getOperand(1);
5871 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005872
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005873 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5874 getPointerTy());
5875 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005876
Dan Gohman8181bd12008-07-27 21:46:04 +00005877 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005878 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005879 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5880 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005881 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5882 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005883
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005884 return DAG.getNode(X86ISD::EH_RETURN,
5885 MVT::Other,
5886 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005887}
5888
Dan Gohman8181bd12008-07-27 21:46:04 +00005889SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005890 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005891 SDValue Root = Op.getOperand(0);
5892 SDValue Trmp = Op.getOperand(1); // trampoline
5893 SDValue FPtr = Op.getOperand(2); // nested function
5894 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005895
Dan Gohman12a9c082008-02-06 22:27:42 +00005896 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005897
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005898 const X86InstrInfo *TII =
5899 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5900
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005901 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005902 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005903
5904 // Large code-model.
5905
5906 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5907 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5908
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005909 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5910 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005911
5912 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5913
5914 // Load the pointer to the nested function into R11.
5915 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005916 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005917 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005918 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005919
5920 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005921 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005922
5923 // Load the 'nest' parameter value into R10.
5924 // R10 is specified in X86CallingConv.td
5925 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5926 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5927 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005928 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005929
5930 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005931 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005932
5933 // Jump to the nested function.
5934 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5935 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5936 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005937 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005938
5939 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5940 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5941 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005942 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005943
Dan Gohman8181bd12008-07-27 21:46:04 +00005944 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005945 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005946 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005947 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005948 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005949 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5950 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005951 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005952
5953 switch (CC) {
5954 default:
5955 assert(0 && "Unsupported calling convention");
5956 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005957 case CallingConv::X86_StdCall: {
5958 // Pass 'nest' parameter in ECX.
5959 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005960 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005961
5962 // Check that ECX wasn't needed by an 'inreg' parameter.
5963 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00005964 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005965
Chris Lattner1c8733e2008-03-12 17:45:29 +00005966 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005967 unsigned InRegCount = 0;
5968 unsigned Idx = 1;
5969
5970 for (FunctionType::param_iterator I = FTy->param_begin(),
5971 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00005972 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005973 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005974 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005975
5976 if (InRegCount > 2) {
5977 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5978 abort();
5979 }
5980 }
5981 break;
5982 }
5983 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00005984 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005985 // Pass 'nest' parameter in EAX.
5986 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005987 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005988 break;
5989 }
5990
Dan Gohman8181bd12008-07-27 21:46:04 +00005991 SDValue OutChains[4];
5992 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005993
5994 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5995 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5996
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005997 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005998 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005999 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00006000 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006001
6002 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006003 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006004
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006005 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006006 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
6007 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006008 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006009
6010 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006011 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006012
Dan Gohman8181bd12008-07-27 21:46:04 +00006013 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00006014 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00006015 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006016 }
6017}
6018
Dan Gohman8181bd12008-07-27 21:46:04 +00006019SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006020 /*
6021 The rounding mode is in bits 11:10 of FPSR, and has the following
6022 settings:
6023 00 Round to nearest
6024 01 Round to -inf
6025 10 Round to +inf
6026 11 Round to 0
6027
6028 FLT_ROUNDS, on the other hand, expects the following:
6029 -1 Undefined
6030 0 Round to 0
6031 1 Round to nearest
6032 2 Round to +inf
6033 3 Round to -inf
6034
6035 To perform the conversion, we do:
6036 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6037 */
6038
6039 MachineFunction &MF = DAG.getMachineFunction();
6040 const TargetMachine &TM = MF.getTarget();
6041 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6042 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00006043 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006044
6045 // Save FP Control Word to stack slot
6046 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00006047 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006048
Dan Gohman8181bd12008-07-27 21:46:04 +00006049 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00006050 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006051
6052 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00006053 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006054
6055 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00006056 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006057 DAG.getNode(ISD::SRL, MVT::i16,
6058 DAG.getNode(ISD::AND, MVT::i16,
6059 CWD, DAG.getConstant(0x800, MVT::i16)),
6060 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006061 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006062 DAG.getNode(ISD::SRL, MVT::i16,
6063 DAG.getNode(ISD::AND, MVT::i16,
6064 CWD, DAG.getConstant(0x400, MVT::i16)),
6065 DAG.getConstant(9, MVT::i8));
6066
Dan Gohman8181bd12008-07-27 21:46:04 +00006067 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006068 DAG.getNode(ISD::AND, MVT::i16,
6069 DAG.getNode(ISD::ADD, MVT::i16,
6070 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
6071 DAG.getConstant(1, MVT::i16)),
6072 DAG.getConstant(3, MVT::i16));
6073
6074
Duncan Sands92c43912008-06-06 12:08:01 +00006075 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006076 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6077}
6078
Dan Gohman8181bd12008-07-27 21:46:04 +00006079SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006080 MVT VT = Op.getValueType();
6081 MVT OpVT = VT;
6082 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006083
6084 Op = Op.getOperand(0);
6085 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006086 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00006087 OpVT = MVT::i32;
6088 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6089 }
Evan Cheng48679f42007-12-14 02:13:44 +00006090
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006091 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6092 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6093 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
6094
6095 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006096 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006097 Ops.push_back(Op);
6098 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6099 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6100 Ops.push_back(Op.getValue(1));
6101 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6102
6103 // Finally xor with NumBits-1.
6104 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
6105
Evan Cheng48679f42007-12-14 02:13:44 +00006106 if (VT == MVT::i8)
6107 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6108 return Op;
6109}
6110
Dan Gohman8181bd12008-07-27 21:46:04 +00006111SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006112 MVT VT = Op.getValueType();
6113 MVT OpVT = VT;
6114 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006115
6116 Op = Op.getOperand(0);
6117 if (VT == MVT::i8) {
6118 OpVT = MVT::i32;
6119 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6120 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006121
6122 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6123 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6124 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
6125
6126 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006127 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006128 Ops.push_back(Op);
6129 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6130 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6131 Ops.push_back(Op.getValue(1));
6132 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6133
Evan Cheng48679f42007-12-14 02:13:44 +00006134 if (VT == MVT::i8)
6135 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6136 return Op;
6137}
6138
Bill Wendling7e04be62008-12-09 22:08:41 +00006139SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6140 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6141 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendlingd3511522008-12-02 01:06:39 +00006142 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6143 // has only one use.
Bill Wendlingd06b4202008-11-26 22:37:40 +00006144 SDNode *N = Op.getNode();
Bill Wendlingd3511522008-12-02 01:06:39 +00006145 SDValue LHS = N->getOperand(0);
6146 SDValue RHS = N->getOperand(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00006147 unsigned BaseOp = 0;
6148 unsigned Cond = 0;
6149
6150 switch (Op.getOpcode()) {
6151 default: assert(0 && "Unknown ovf instruction!");
6152 case ISD::SADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006153 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00006154 Cond = X86::COND_O;
6155 break;
6156 case ISD::UADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006157 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00006158 Cond = X86::COND_C;
6159 break;
6160 case ISD::SSUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006161 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00006162 Cond = X86::COND_O;
6163 break;
6164 case ISD::USUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006165 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00006166 Cond = X86::COND_C;
6167 break;
6168 case ISD::SMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006169 BaseOp = X86ISD::SMUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00006170 Cond = X86::COND_O;
6171 break;
6172 case ISD::UMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006173 BaseOp = X86ISD::UMUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00006174 Cond = X86::COND_C;
6175 break;
6176 }
Bill Wendlingd06b4202008-11-26 22:37:40 +00006177
Bill Wendlingd3511522008-12-02 01:06:39 +00006178 // Also sets EFLAGS.
6179 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Bill Wendling7e04be62008-12-09 22:08:41 +00006180 SDValue Sum = DAG.getNode(BaseOp, VTs, LHS, RHS);
Bill Wendlingd06b4202008-11-26 22:37:40 +00006181
Bill Wendlingd3511522008-12-02 01:06:39 +00006182 SDValue SetCC =
6183 DAG.getNode(X86ISD::SETCC, N->getValueType(1),
Bill Wendling35f1a9d2008-12-10 02:01:32 +00006184 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendlingd06b4202008-11-26 22:37:40 +00006185
Bill Wendlingd3511522008-12-02 01:06:39 +00006186 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6187 return Sum;
Bill Wendling4c134df2008-11-24 19:21:46 +00006188}
6189
Dan Gohman8181bd12008-07-27 21:46:04 +00006190SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006191 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00006192 unsigned Reg = 0;
6193 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006194 switch(T.getSimpleVT()) {
6195 default:
6196 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006197 case MVT::i8: Reg = X86::AL; size = 1; break;
6198 case MVT::i16: Reg = X86::AX; size = 2; break;
6199 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00006200 case MVT::i64:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006201 assert(Subtarget->is64Bit() && "Node not type legal!");
6202 Reg = X86::RAX; size = 8;
Andrew Lenharth81580822008-03-05 01:15:49 +00006203 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00006204 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006205 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00006206 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00006207 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006208 Op.getOperand(1),
6209 Op.getOperand(3),
6210 DAG.getTargetConstant(size, MVT::i8),
6211 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006212 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006213 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
6214 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006215 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
6216 return cpOut;
6217}
6218
Duncan Sands7d9834b2008-12-01 11:39:25 +00006219SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif825aa892008-08-28 23:19:51 +00006220 SelectionDAG &DAG) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006221 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharth81580822008-03-05 01:15:49 +00006222 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006223 SDValue TheChain = Op.getOperand(0);
6224 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6225 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
6226 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX, MVT::i64,
6227 rax.getValue(2));
6228 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
6229 DAG.getConstant(32, MVT::i8));
6230 SDValue Ops[] = {
6231 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp),
6232 rdx.getValue(1)
6233 };
6234 return DAG.getMergeValues(Ops, 2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006235}
6236
Dale Johannesen9011d872008-09-29 22:25:26 +00006237SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6238 SDNode *Node = Op.getNode();
6239 MVT T = Node->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006240 SDValue negOp = DAG.getNode(ISD::SUB, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006241 DAG.getConstant(0, T), Node->getOperand(2));
6242 return DAG.getAtomic((Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_8 ?
6243 ISD::ATOMIC_LOAD_ADD_8 :
6244 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_16 ?
6245 ISD::ATOMIC_LOAD_ADD_16 :
6246 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_32 ?
6247 ISD::ATOMIC_LOAD_ADD_32 :
6248 ISD::ATOMIC_LOAD_ADD_64),
6249 Node->getOperand(0),
6250 Node->getOperand(1), negOp,
6251 cast<AtomicSDNode>(Node)->getSrcValue(),
6252 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006253}
6254
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006255/// LowerOperation - Provide custom lowering hooks for some operations.
6256///
Dan Gohman8181bd12008-07-27 21:46:04 +00006257SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006258 switch (Op.getOpcode()) {
6259 default: assert(0 && "Should not custom lower this!");
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006260 case ISD::ATOMIC_CMP_SWAP_8:
6261 case ISD::ATOMIC_CMP_SWAP_16:
6262 case ISD::ATOMIC_CMP_SWAP_32:
Dale Johannesenbc187662008-08-28 02:44:49 +00006263 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006264 case ISD::ATOMIC_LOAD_SUB_8:
6265 case ISD::ATOMIC_LOAD_SUB_16:
Dale Johannesen9011d872008-09-29 22:25:26 +00006266 case ISD::ATOMIC_LOAD_SUB_32: return LowerLOAD_SUB(Op,DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006267 case ISD::ATOMIC_LOAD_SUB_64: return LowerLOAD_SUB(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006268 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6269 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6270 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6271 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6272 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6273 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6274 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6275 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006276 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006277 case ISD::SHL_PARTS:
6278 case ISD::SRA_PARTS:
6279 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6280 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00006281 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006282 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6283 case ISD::FABS: return LowerFABS(Op, DAG);
6284 case ISD::FNEG: return LowerFNEG(Op, DAG);
6285 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006286 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006287 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006288 case ISD::SELECT: return LowerSELECT(Op, DAG);
6289 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006290 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6291 case ISD::CALL: return LowerCALL(Op, DAG);
6292 case ISD::RET: return LowerRET(Op, DAG);
6293 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006294 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006295 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006296 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6297 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6298 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6299 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6300 case ISD::FRAME_TO_ARGS_OFFSET:
6301 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6302 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6303 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006304 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006305 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006306 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6307 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Bill Wendling7e04be62008-12-09 22:08:41 +00006308 case ISD::SADDO:
6309 case ISD::UADDO:
6310 case ISD::SSUBO:
6311 case ISD::USUBO:
6312 case ISD::SMULO:
6313 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006314 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006315 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006316}
6317
Duncan Sands7d9834b2008-12-01 11:39:25 +00006318void X86TargetLowering::
6319ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6320 SelectionDAG &DAG, unsigned NewOp) {
6321 MVT T = Node->getValueType(0);
6322 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6323
6324 SDValue Chain = Node->getOperand(0);
6325 SDValue In1 = Node->getOperand(1);
6326 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6327 Node->getOperand(2), DAG.getIntPtrConstant(0));
6328 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6329 Node->getOperand(2), DAG.getIntPtrConstant(1));
6330 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6331 // have a MemOperand. Pass the info through as a normal operand.
6332 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6333 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6334 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
6335 SDValue Result = DAG.getNode(NewOp, Tys, Ops, 5);
6336 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
6337 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6338 Results.push_back(Result.getValue(2));
6339}
6340
Duncan Sandsac496a12008-07-04 11:47:58 +00006341/// ReplaceNodeResults - Replace a node with an illegal result type
6342/// with a new node built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00006343void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6344 SmallVectorImpl<SDValue>&Results,
6345 SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006346 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006347 default:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006348 assert(false && "Do not know how to custom type legalize this operation!");
6349 return;
6350 case ISD::FP_TO_SINT: {
6351 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
6352 SDValue FIST = Vals.first, StackSlot = Vals.second;
6353 if (FIST.getNode() != 0) {
6354 MVT VT = N->getValueType(0);
6355 // Return a load from the stack slot.
6356 Results.push_back(DAG.getLoad(VT, FIST, StackSlot, NULL, 0));
6357 }
6358 return;
6359 }
6360 case ISD::READCYCLECOUNTER: {
6361 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6362 SDValue TheChain = N->getOperand(0);
6363 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6364 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
6365 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX, MVT::i32,
6366 eax.getValue(2));
6367 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6368 SDValue Ops[] = { eax, edx };
6369 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2));
6370 Results.push_back(edx.getValue(1));
6371 return;
6372 }
6373 case ISD::ATOMIC_CMP_SWAP_64: {
6374 MVT T = N->getValueType(0);
6375 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6376 SDValue cpInL, cpInH;
6377 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6378 DAG.getConstant(0, MVT::i32));
6379 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6380 DAG.getConstant(1, MVT::i32));
6381 cpInL = DAG.getCopyToReg(N->getOperand(0), X86::EAX, cpInL, SDValue());
6382 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX, cpInH,
6383 cpInL.getValue(1));
6384 SDValue swapInL, swapInH;
6385 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6386 DAG.getConstant(0, MVT::i32));
6387 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6388 DAG.getConstant(1, MVT::i32));
6389 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX, swapInL,
6390 cpInH.getValue(1));
6391 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX, swapInH,
6392 swapInL.getValue(1));
6393 SDValue Ops[] = { swapInH.getValue(0),
6394 N->getOperand(1),
6395 swapInH.getValue(1) };
6396 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6397 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
6398 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
6399 Result.getValue(1));
6400 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
6401 cpOutL.getValue(2));
6402 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
6403 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6404 Results.push_back(cpOutH.getValue(1));
6405 return;
6406 }
6407 case ISD::ATOMIC_LOAD_ADD_64:
6408 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6409 return;
6410 case ISD::ATOMIC_LOAD_AND_64:
6411 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6412 return;
6413 case ISD::ATOMIC_LOAD_NAND_64:
6414 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6415 return;
6416 case ISD::ATOMIC_LOAD_OR_64:
6417 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6418 return;
6419 case ISD::ATOMIC_LOAD_SUB_64:
6420 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6421 return;
6422 case ISD::ATOMIC_LOAD_XOR_64:
6423 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6424 return;
6425 case ISD::ATOMIC_SWAP_64:
6426 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6427 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006428 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006429}
6430
6431const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6432 switch (Opcode) {
6433 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006434 case X86ISD::BSF: return "X86ISD::BSF";
6435 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006436 case X86ISD::SHLD: return "X86ISD::SHLD";
6437 case X86ISD::SHRD: return "X86ISD::SHRD";
6438 case X86ISD::FAND: return "X86ISD::FAND";
6439 case X86ISD::FOR: return "X86ISD::FOR";
6440 case X86ISD::FXOR: return "X86ISD::FXOR";
6441 case X86ISD::FSRL: return "X86ISD::FSRL";
6442 case X86ISD::FILD: return "X86ISD::FILD";
6443 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6444 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6445 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6446 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6447 case X86ISD::FLD: return "X86ISD::FLD";
6448 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006449 case X86ISD::CALL: return "X86ISD::CALL";
6450 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6451 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6452 case X86ISD::CMP: return "X86ISD::CMP";
6453 case X86ISD::COMI: return "X86ISD::COMI";
6454 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6455 case X86ISD::SETCC: return "X86ISD::SETCC";
6456 case X86ISD::CMOV: return "X86ISD::CMOV";
6457 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6458 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6459 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6460 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006461 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6462 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006463 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006464 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006465 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6466 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006467 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6468 case X86ISD::FMAX: return "X86ISD::FMAX";
6469 case X86ISD::FMIN: return "X86ISD::FMIN";
6470 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6471 case X86ISD::FRCP: return "X86ISD::FRCP";
6472 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6473 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6474 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006475 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006476 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006477 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6478 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006479 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6480 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6481 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6482 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6483 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6484 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006485 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6486 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006487 case X86ISD::VSHL: return "X86ISD::VSHL";
6488 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006489 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6490 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6491 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6492 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6493 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6494 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6495 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6496 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6497 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6498 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingae034ed2008-12-12 00:56:36 +00006499 case X86ISD::ADD: return "X86ISD::ADD";
6500 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingf5399032008-12-12 21:15:41 +00006501 case X86ISD::SMUL: return "X86ISD::SMUL";
6502 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006503 }
6504}
6505
6506// isLegalAddressingMode - Return true if the addressing mode represented
6507// by AM is legal for this target, for a load/store of the specified type.
6508bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6509 const Type *Ty) const {
6510 // X86 supports extremely general addressing modes.
6511
6512 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6513 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6514 return false;
6515
6516 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006517 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006518 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6519 return false;
Dale Johannesen64660e92008-12-05 21:47:27 +00006520 // If BaseGV requires a register, we cannot also have a BaseReg.
6521 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6522 AM.HasBaseReg)
6523 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006524
6525 // X86-64 only supports addr of globals in small code model.
6526 if (Subtarget->is64Bit()) {
6527 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6528 return false;
6529 // If lower 4G is not available, then we must use rip-relative addressing.
6530 if (AM.BaseOffs || AM.Scale > 1)
6531 return false;
6532 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006533 }
6534
6535 switch (AM.Scale) {
6536 case 0:
6537 case 1:
6538 case 2:
6539 case 4:
6540 case 8:
6541 // These scales always work.
6542 break;
6543 case 3:
6544 case 5:
6545 case 9:
6546 // These scales are formed with basereg+scalereg. Only accept if there is
6547 // no basereg yet.
6548 if (AM.HasBaseReg)
6549 return false;
6550 break;
6551 default: // Other stuff never works.
6552 return false;
6553 }
6554
6555 return true;
6556}
6557
6558
Evan Cheng27a820a2007-10-26 01:56:11 +00006559bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6560 if (!Ty1->isInteger() || !Ty2->isInteger())
6561 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006562 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6563 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006564 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006565 return false;
6566 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006567}
6568
Duncan Sands92c43912008-06-06 12:08:01 +00006569bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6570 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006571 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006572 unsigned NumBits1 = VT1.getSizeInBits();
6573 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006574 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006575 return false;
6576 return Subtarget->is64Bit() || NumBits1 < 64;
6577}
Evan Cheng27a820a2007-10-26 01:56:11 +00006578
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006579/// isShuffleMaskLegal - Targets can use this to indicate that they only
6580/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6581/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6582/// are assumed to be legal.
6583bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006584X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006585 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006586 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006587 return (Mask.getNode()->getNumOperands() <= 4 ||
6588 isIdentityMask(Mask.getNode()) ||
6589 isIdentityMask(Mask.getNode(), true) ||
6590 isSplatMask(Mask.getNode()) ||
6591 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6592 X86::isUNPCKLMask(Mask.getNode()) ||
6593 X86::isUNPCKHMask(Mask.getNode()) ||
6594 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6595 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006596}
6597
Dan Gohman48d5f062008-04-09 20:09:42 +00006598bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006599X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006600 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006601 unsigned NumElts = BVOps.size();
6602 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006603 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006604 if (NumElts == 2) return true;
6605 if (NumElts == 4) {
6606 return (isMOVLMask(&BVOps[0], 4) ||
6607 isCommutedMOVL(&BVOps[0], 4, true) ||
6608 isSHUFPMask(&BVOps[0], 4) ||
6609 isCommutedSHUFP(&BVOps[0], 4));
6610 }
6611 return false;
6612}
6613
6614//===----------------------------------------------------------------------===//
6615// X86 Scheduler Hooks
6616//===----------------------------------------------------------------------===//
6617
Mon P Wang078a62d2008-05-05 19:05:59 +00006618// private utility function
6619MachineBasicBlock *
6620X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6621 MachineBasicBlock *MBB,
6622 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006623 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006624 unsigned LoadOpc,
6625 unsigned CXchgOpc,
6626 unsigned copyOpc,
6627 unsigned notOpc,
6628 unsigned EAXreg,
6629 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006630 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006631 // For the atomic bitwise operator, we generate
6632 // thisMBB:
6633 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006634 // ld t1 = [bitinstr.addr]
6635 // op t2 = t1, [bitinstr.val]
6636 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006637 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6638 // bz newMBB
6639 // fallthrough -->nextMBB
6640 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6641 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006642 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006643 ++MBBIter;
6644
6645 /// First build the CFG
6646 MachineFunction *F = MBB->getParent();
6647 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006648 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6649 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6650 F->insert(MBBIter, newMBB);
6651 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006652
6653 // Move all successors to thisMBB to nextMBB
6654 nextMBB->transferSuccessors(thisMBB);
6655
6656 // Update thisMBB to fall through to newMBB
6657 thisMBB->addSuccessor(newMBB);
6658
6659 // newMBB jumps to itself and fall through to nextMBB
6660 newMBB->addSuccessor(nextMBB);
6661 newMBB->addSuccessor(newMBB);
6662
6663 // Insert instructions into newMBB based on incoming instruction
6664 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6665 MachineOperand& destOper = bInstr->getOperand(0);
6666 MachineOperand* argOpers[6];
6667 int numArgs = bInstr->getNumOperands() - 1;
6668 for (int i=0; i < numArgs; ++i)
6669 argOpers[i] = &bInstr->getOperand(i+1);
6670
6671 // x86 address has 4 operands: base, index, scale, and displacement
6672 int lastAddrIndx = 3; // [0,3]
6673 int valArgIndx = 4;
6674
Dale Johannesend20e4452008-08-19 18:47:28 +00006675 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6676 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006677 for (int i=0; i <= lastAddrIndx; ++i)
6678 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006679
Dale Johannesend20e4452008-08-19 18:47:28 +00006680 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006681 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006682 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006683 }
6684 else
6685 tt = t1;
6686
Dale Johannesend20e4452008-08-19 18:47:28 +00006687 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006688 assert((argOpers[valArgIndx]->isReg() ||
6689 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006690 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006691 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006692 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6693 else
6694 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006695 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006696 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006697
Dale Johannesend20e4452008-08-19 18:47:28 +00006698 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006699 MIB.addReg(t1);
6700
Dale Johannesend20e4452008-08-19 18:47:28 +00006701 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006702 for (int i=0; i <= lastAddrIndx; ++i)
6703 (*MIB).addOperand(*argOpers[i]);
6704 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006705 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6706 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6707
Dale Johannesend20e4452008-08-19 18:47:28 +00006708 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6709 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006710
6711 // insert branch
6712 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6713
Dan Gohman221a4372008-07-07 23:14:23 +00006714 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006715 return nextMBB;
6716}
6717
Dale Johannesen44eb5372008-10-03 19:41:08 +00006718// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00006719MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00006720X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
6721 MachineBasicBlock *MBB,
6722 unsigned regOpcL,
6723 unsigned regOpcH,
6724 unsigned immOpcL,
6725 unsigned immOpcH,
6726 bool invSrc) {
6727 // For the atomic bitwise operator, we generate
6728 // thisMBB (instructions are in pairs, except cmpxchg8b)
6729 // ld t1,t2 = [bitinstr.addr]
6730 // newMBB:
6731 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
6732 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006733 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00006734 // mov ECX, EBX <- t5, t6
6735 // mov EAX, EDX <- t1, t2
6736 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
6737 // mov t3, t4 <- EAX, EDX
6738 // bz newMBB
6739 // result in out1, out2
6740 // fallthrough -->nextMBB
6741
6742 const TargetRegisterClass *RC = X86::GR32RegisterClass;
6743 const unsigned LoadOpc = X86::MOV32rm;
6744 const unsigned copyOpc = X86::MOV32rr;
6745 const unsigned NotOpc = X86::NOT32r;
6746 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6747 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6748 MachineFunction::iterator MBBIter = MBB;
6749 ++MBBIter;
6750
6751 /// First build the CFG
6752 MachineFunction *F = MBB->getParent();
6753 MachineBasicBlock *thisMBB = MBB;
6754 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6755 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6756 F->insert(MBBIter, newMBB);
6757 F->insert(MBBIter, nextMBB);
6758
6759 // Move all successors to thisMBB to nextMBB
6760 nextMBB->transferSuccessors(thisMBB);
6761
6762 // Update thisMBB to fall through to newMBB
6763 thisMBB->addSuccessor(newMBB);
6764
6765 // newMBB jumps to itself and fall through to nextMBB
6766 newMBB->addSuccessor(nextMBB);
6767 newMBB->addSuccessor(newMBB);
6768
6769 // Insert instructions into newMBB based on incoming instruction
6770 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
6771 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
6772 MachineOperand& dest1Oper = bInstr->getOperand(0);
6773 MachineOperand& dest2Oper = bInstr->getOperand(1);
6774 MachineOperand* argOpers[6];
6775 for (int i=0; i < 6; ++i)
6776 argOpers[i] = &bInstr->getOperand(i+2);
6777
6778 // x86 address has 4 operands: base, index, scale, and displacement
6779 int lastAddrIndx = 3; // [0,3]
6780
6781 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6782 MachineInstrBuilder MIB = BuildMI(thisMBB, TII->get(LoadOpc), t1);
6783 for (int i=0; i <= lastAddrIndx; ++i)
6784 (*MIB).addOperand(*argOpers[i]);
6785 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
6786 MIB = BuildMI(thisMBB, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006787 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00006788 for (int i=0; i <= lastAddrIndx-1; ++i)
6789 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006790 MachineOperand newOp3 = *(argOpers[3]);
6791 if (newOp3.isImm())
6792 newOp3.setImm(newOp3.getImm()+4);
6793 else
6794 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00006795 (*MIB).addOperand(newOp3);
6796
6797 // t3/4 are defined later, at the bottom of the loop
6798 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
6799 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
6800 BuildMI(newMBB, TII->get(X86::PHI), dest1Oper.getReg())
6801 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
6802 BuildMI(newMBB, TII->get(X86::PHI), dest2Oper.getReg())
6803 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
6804
6805 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
6806 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
6807 if (invSrc) {
6808 MIB = BuildMI(newMBB, TII->get(NotOpc), tt1).addReg(t1);
6809 MIB = BuildMI(newMBB, TII->get(NotOpc), tt2).addReg(t2);
6810 } else {
6811 tt1 = t1;
6812 tt2 = t2;
6813 }
6814
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006815 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00006816 "invalid operand");
6817 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
6818 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006819 if (argOpers[4]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006820 MIB = BuildMI(newMBB, TII->get(regOpcL), t5);
6821 else
6822 MIB = BuildMI(newMBB, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006823 if (regOpcL != X86::MOV32rr)
6824 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00006825 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006826 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
6827 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
6828 if (argOpers[5]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006829 MIB = BuildMI(newMBB, TII->get(regOpcH), t6);
6830 else
6831 MIB = BuildMI(newMBB, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006832 if (regOpcH != X86::MOV32rr)
6833 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006834 (*MIB).addOperand(*argOpers[5]);
6835
6836 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EAX);
6837 MIB.addReg(t1);
6838 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EDX);
6839 MIB.addReg(t2);
6840
6841 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EBX);
6842 MIB.addReg(t5);
6843 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::ECX);
6844 MIB.addReg(t6);
6845
6846 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG8B));
6847 for (int i=0; i <= lastAddrIndx; ++i)
6848 (*MIB).addOperand(*argOpers[i]);
6849
6850 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6851 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6852
6853 MIB = BuildMI(newMBB, TII->get(copyOpc), t3);
6854 MIB.addReg(X86::EAX);
6855 MIB = BuildMI(newMBB, TII->get(copyOpc), t4);
6856 MIB.addReg(X86::EDX);
6857
6858 // insert branch
6859 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6860
6861 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
6862 return nextMBB;
6863}
6864
6865// private utility function
6866MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00006867X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6868 MachineBasicBlock *MBB,
6869 unsigned cmovOpc) {
6870 // For the atomic min/max operator, we generate
6871 // thisMBB:
6872 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006873 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006874 // mov t2 = [min/max.val]
6875 // cmp t1, t2
6876 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006877 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006878 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6879 // bz newMBB
6880 // fallthrough -->nextMBB
6881 //
6882 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6883 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006884 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006885 ++MBBIter;
6886
6887 /// First build the CFG
6888 MachineFunction *F = MBB->getParent();
6889 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006890 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6891 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6892 F->insert(MBBIter, newMBB);
6893 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006894
6895 // Move all successors to thisMBB to nextMBB
6896 nextMBB->transferSuccessors(thisMBB);
6897
6898 // Update thisMBB to fall through to newMBB
6899 thisMBB->addSuccessor(newMBB);
6900
6901 // newMBB jumps to newMBB and fall through to nextMBB
6902 newMBB->addSuccessor(nextMBB);
6903 newMBB->addSuccessor(newMBB);
6904
6905 // Insert instructions into newMBB based on incoming instruction
6906 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6907 MachineOperand& destOper = mInstr->getOperand(0);
6908 MachineOperand* argOpers[6];
6909 int numArgs = mInstr->getNumOperands() - 1;
6910 for (int i=0; i < numArgs; ++i)
6911 argOpers[i] = &mInstr->getOperand(i+1);
6912
6913 // x86 address has 4 operands: base, index, scale, and displacement
6914 int lastAddrIndx = 3; // [0,3]
6915 int valArgIndx = 4;
6916
Mon P Wang318b0372008-05-05 22:56:23 +00006917 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6918 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006919 for (int i=0; i <= lastAddrIndx; ++i)
6920 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006921
Mon P Wang078a62d2008-05-05 19:05:59 +00006922 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006923 assert((argOpers[valArgIndx]->isReg() ||
6924 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006925 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00006926
6927 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006928 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006929 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6930 else
6931 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6932 (*MIB).addOperand(*argOpers[valArgIndx]);
6933
Mon P Wang318b0372008-05-05 22:56:23 +00006934 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6935 MIB.addReg(t1);
6936
Mon P Wang078a62d2008-05-05 19:05:59 +00006937 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6938 MIB.addReg(t1);
6939 MIB.addReg(t2);
6940
6941 // Generate movc
6942 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6943 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6944 MIB.addReg(t2);
6945 MIB.addReg(t1);
6946
6947 // Cmp and exchange if none has modified the memory location
6948 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6949 for (int i=0; i <= lastAddrIndx; ++i)
6950 (*MIB).addOperand(*argOpers[i]);
6951 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006952 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6953 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006954
6955 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6956 MIB.addReg(X86::EAX);
6957
6958 // insert branch
6959 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6960
Dan Gohman221a4372008-07-07 23:14:23 +00006961 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006962 return nextMBB;
6963}
6964
6965
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006966MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006967X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6968 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006969 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6970 switch (MI->getOpcode()) {
6971 default: assert(false && "Unexpected instr type to insert");
Mon P Wang83edba52008-12-12 01:25:51 +00006972 case X86::CMOV_V1I64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006973 case X86::CMOV_FR32:
6974 case X86::CMOV_FR64:
6975 case X86::CMOV_V4F32:
6976 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006977 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006978 // To "insert" a SELECT_CC instruction, we actually have to insert the
6979 // diamond control-flow pattern. The incoming instruction knows the
6980 // destination vreg to set, the condition code register to branch on, the
6981 // true/false values to select between, and a branch opcode to use.
6982 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006983 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006984 ++It;
6985
6986 // thisMBB:
6987 // ...
6988 // TrueVal = ...
6989 // cmpTY ccX, r1, r2
6990 // bCC copy1MBB
6991 // fallthrough --> copy0MBB
6992 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006993 MachineFunction *F = BB->getParent();
6994 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6995 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006996 unsigned Opc =
6997 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6998 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006999 F->insert(It, copy0MBB);
7000 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007001 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007002 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00007003 sinkMBB->transferSuccessors(BB);
7004
7005 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007006 BB->addSuccessor(copy0MBB);
7007 BB->addSuccessor(sinkMBB);
7008
7009 // copy0MBB:
7010 // %FalseValue = ...
7011 // # fallthrough to sinkMBB
7012 BB = copy0MBB;
7013
7014 // Update machine-CFG edges
7015 BB->addSuccessor(sinkMBB);
7016
7017 // sinkMBB:
7018 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7019 // ...
7020 BB = sinkMBB;
7021 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
7022 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7023 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7024
Dan Gohman221a4372008-07-07 23:14:23 +00007025 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007026 return BB;
7027 }
7028
7029 case X86::FP32_TO_INT16_IN_MEM:
7030 case X86::FP32_TO_INT32_IN_MEM:
7031 case X86::FP32_TO_INT64_IN_MEM:
7032 case X86::FP64_TO_INT16_IN_MEM:
7033 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007034 case X86::FP64_TO_INT64_IN_MEM:
7035 case X86::FP80_TO_INT16_IN_MEM:
7036 case X86::FP80_TO_INT32_IN_MEM:
7037 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007038 // Change the floating point control register to use "round towards zero"
7039 // mode when truncating to an integer value.
7040 MachineFunction *F = BB->getParent();
7041 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
7042 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
7043
7044 // Load the old value of the high byte of the control word...
7045 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00007046 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007047 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
7048
7049 // Set the high part to be round to zero...
7050 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
7051 .addImm(0xC7F);
7052
7053 // Reload the modified control word now...
7054 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7055
7056 // Restore the memory image of control word to original value
7057 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
7058 .addReg(OldCW);
7059
7060 // Get the X86 opcode to use.
7061 unsigned Opc;
7062 switch (MI->getOpcode()) {
7063 default: assert(0 && "illegal opcode!");
7064 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7065 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7066 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7067 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7068 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7069 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007070 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7071 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7072 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007073 }
7074
7075 X86AddressMode AM;
7076 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007077 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007078 AM.BaseType = X86AddressMode::RegBase;
7079 AM.Base.Reg = Op.getReg();
7080 } else {
7081 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00007082 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007083 }
7084 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007085 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007086 AM.Scale = Op.getImm();
7087 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007088 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007089 AM.IndexReg = Op.getImm();
7090 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007091 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007092 AM.GV = Op.getGlobal();
7093 } else {
7094 AM.Disp = Op.getImm();
7095 }
7096 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
7097 .addReg(MI->getOperand(4).getReg());
7098
7099 // Reload the original control word now.
7100 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7101
Dan Gohman221a4372008-07-07 23:14:23 +00007102 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007103 return BB;
7104 }
Mon P Wang078a62d2008-05-05 19:05:59 +00007105 case X86::ATOMAND32:
7106 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007107 X86::AND32ri, X86::MOV32rm,
7108 X86::LCMPXCHG32, X86::MOV32rr,
7109 X86::NOT32r, X86::EAX,
7110 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007111 case X86::ATOMOR32:
7112 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007113 X86::OR32ri, X86::MOV32rm,
7114 X86::LCMPXCHG32, X86::MOV32rr,
7115 X86::NOT32r, X86::EAX,
7116 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007117 case X86::ATOMXOR32:
7118 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007119 X86::XOR32ri, X86::MOV32rm,
7120 X86::LCMPXCHG32, X86::MOV32rr,
7121 X86::NOT32r, X86::EAX,
7122 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007123 case X86::ATOMNAND32:
7124 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007125 X86::AND32ri, X86::MOV32rm,
7126 X86::LCMPXCHG32, X86::MOV32rr,
7127 X86::NOT32r, X86::EAX,
7128 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00007129 case X86::ATOMMIN32:
7130 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7131 case X86::ATOMMAX32:
7132 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7133 case X86::ATOMUMIN32:
7134 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7135 case X86::ATOMUMAX32:
7136 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00007137
7138 case X86::ATOMAND16:
7139 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7140 X86::AND16ri, X86::MOV16rm,
7141 X86::LCMPXCHG16, X86::MOV16rr,
7142 X86::NOT16r, X86::AX,
7143 X86::GR16RegisterClass);
7144 case X86::ATOMOR16:
7145 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7146 X86::OR16ri, X86::MOV16rm,
7147 X86::LCMPXCHG16, X86::MOV16rr,
7148 X86::NOT16r, X86::AX,
7149 X86::GR16RegisterClass);
7150 case X86::ATOMXOR16:
7151 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7152 X86::XOR16ri, X86::MOV16rm,
7153 X86::LCMPXCHG16, X86::MOV16rr,
7154 X86::NOT16r, X86::AX,
7155 X86::GR16RegisterClass);
7156 case X86::ATOMNAND16:
7157 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7158 X86::AND16ri, X86::MOV16rm,
7159 X86::LCMPXCHG16, X86::MOV16rr,
7160 X86::NOT16r, X86::AX,
7161 X86::GR16RegisterClass, true);
7162 case X86::ATOMMIN16:
7163 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7164 case X86::ATOMMAX16:
7165 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7166 case X86::ATOMUMIN16:
7167 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7168 case X86::ATOMUMAX16:
7169 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7170
7171 case X86::ATOMAND8:
7172 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7173 X86::AND8ri, X86::MOV8rm,
7174 X86::LCMPXCHG8, X86::MOV8rr,
7175 X86::NOT8r, X86::AL,
7176 X86::GR8RegisterClass);
7177 case X86::ATOMOR8:
7178 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7179 X86::OR8ri, X86::MOV8rm,
7180 X86::LCMPXCHG8, X86::MOV8rr,
7181 X86::NOT8r, X86::AL,
7182 X86::GR8RegisterClass);
7183 case X86::ATOMXOR8:
7184 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7185 X86::XOR8ri, X86::MOV8rm,
7186 X86::LCMPXCHG8, X86::MOV8rr,
7187 X86::NOT8r, X86::AL,
7188 X86::GR8RegisterClass);
7189 case X86::ATOMNAND8:
7190 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7191 X86::AND8ri, X86::MOV8rm,
7192 X86::LCMPXCHG8, X86::MOV8rr,
7193 X86::NOT8r, X86::AL,
7194 X86::GR8RegisterClass, true);
7195 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00007196 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007197 case X86::ATOMAND64:
7198 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7199 X86::AND64ri32, X86::MOV64rm,
7200 X86::LCMPXCHG64, X86::MOV64rr,
7201 X86::NOT64r, X86::RAX,
7202 X86::GR64RegisterClass);
7203 case X86::ATOMOR64:
7204 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7205 X86::OR64ri32, X86::MOV64rm,
7206 X86::LCMPXCHG64, X86::MOV64rr,
7207 X86::NOT64r, X86::RAX,
7208 X86::GR64RegisterClass);
7209 case X86::ATOMXOR64:
7210 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7211 X86::XOR64ri32, X86::MOV64rm,
7212 X86::LCMPXCHG64, X86::MOV64rr,
7213 X86::NOT64r, X86::RAX,
7214 X86::GR64RegisterClass);
7215 case X86::ATOMNAND64:
7216 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7217 X86::AND64ri32, X86::MOV64rm,
7218 X86::LCMPXCHG64, X86::MOV64rr,
7219 X86::NOT64r, X86::RAX,
7220 X86::GR64RegisterClass, true);
7221 case X86::ATOMMIN64:
7222 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7223 case X86::ATOMMAX64:
7224 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7225 case X86::ATOMUMIN64:
7226 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7227 case X86::ATOMUMAX64:
7228 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00007229
7230 // This group does 64-bit operations on a 32-bit host.
7231 case X86::ATOMAND6432:
7232 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7233 X86::AND32rr, X86::AND32rr,
7234 X86::AND32ri, X86::AND32ri,
7235 false);
7236 case X86::ATOMOR6432:
7237 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7238 X86::OR32rr, X86::OR32rr,
7239 X86::OR32ri, X86::OR32ri,
7240 false);
7241 case X86::ATOMXOR6432:
7242 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7243 X86::XOR32rr, X86::XOR32rr,
7244 X86::XOR32ri, X86::XOR32ri,
7245 false);
7246 case X86::ATOMNAND6432:
7247 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7248 X86::AND32rr, X86::AND32rr,
7249 X86::AND32ri, X86::AND32ri,
7250 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00007251 case X86::ATOMADD6432:
7252 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7253 X86::ADD32rr, X86::ADC32rr,
7254 X86::ADD32ri, X86::ADC32ri,
7255 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00007256 case X86::ATOMSUB6432:
7257 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7258 X86::SUB32rr, X86::SBB32rr,
7259 X86::SUB32ri, X86::SBB32ri,
7260 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007261 case X86::ATOMSWAP6432:
7262 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7263 X86::MOV32rr, X86::MOV32rr,
7264 X86::MOV32ri, X86::MOV32ri,
7265 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007266 }
7267}
7268
7269//===----------------------------------------------------------------------===//
7270// X86 Optimization Hooks
7271//===----------------------------------------------------------------------===//
7272
Dan Gohman8181bd12008-07-27 21:46:04 +00007273void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00007274 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00007275 APInt &KnownZero,
7276 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007277 const SelectionDAG &DAG,
7278 unsigned Depth) const {
7279 unsigned Opc = Op.getOpcode();
7280 assert((Opc >= ISD::BUILTIN_OP_END ||
7281 Opc == ISD::INTRINSIC_WO_CHAIN ||
7282 Opc == ISD::INTRINSIC_W_CHAIN ||
7283 Opc == ISD::INTRINSIC_VOID) &&
7284 "Should use MaskedValueIsZero if you don't know whether Op"
7285 " is a target node!");
7286
Dan Gohman1d79e432008-02-13 23:07:24 +00007287 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007288 switch (Opc) {
7289 default: break;
7290 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007291 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7292 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007293 break;
7294 }
7295}
7296
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007297/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007298/// node is a GlobalAddress + offset.
7299bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7300 GlobalValue* &GA, int64_t &Offset) const{
7301 if (N->getOpcode() == X86ISD::Wrapper) {
7302 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007303 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00007304 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007305 return true;
7306 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007307 }
Evan Chengef7be082008-05-12 19:56:52 +00007308 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007309}
7310
Evan Chengef7be082008-05-12 19:56:52 +00007311static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7312 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007313 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007314 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007315 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007316 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007317 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007318 return false;
7319}
7320
Dan Gohman8181bd12008-07-27 21:46:04 +00007321static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007322 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007323 SDNode *&Base,
7324 SelectionDAG &DAG, MachineFrameInfo *MFI,
7325 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007326 Base = NULL;
7327 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007328 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007329 if (Idx.getOpcode() == ISD::UNDEF) {
7330 if (!Base)
7331 return false;
7332 continue;
7333 }
7334
Dan Gohman8181bd12008-07-27 21:46:04 +00007335 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007336 if (!Elt.getNode() ||
7337 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007338 return false;
7339 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007340 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007341 if (Base->getOpcode() == ISD::UNDEF)
7342 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007343 continue;
7344 }
7345 if (Elt.getOpcode() == ISD::UNDEF)
7346 continue;
7347
Gabor Greif1c80d112008-08-28 21:40:38 +00007348 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007349 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007350 return false;
7351 }
7352 return true;
7353}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007354
7355/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7356/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7357/// if the load addresses are consecutive, non-overlapping, and in the right
7358/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007359static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007360 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007361 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00007362 MVT VT = N->getValueType(0);
7363 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007364 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007365 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007366 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007367 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7368 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007369 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007370
Dan Gohman11821702007-07-27 17:16:43 +00007371 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007372 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007373 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00007374 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00007375 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
7376 LD->getSrcValueOffset(), LD->isVolatile(),
7377 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007378}
7379
Evan Chengb6290462008-05-12 23:04:07 +00007380/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007381static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng6617eed2008-09-24 23:26:36 +00007382 const X86Subtarget *Subtarget,
7383 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007384 unsigned NumOps = N->getNumOperands();
7385
Evan Chenge9b9c672008-05-09 21:53:03 +00007386 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007387 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007388 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007389
Duncan Sands92c43912008-06-06 12:08:01 +00007390 MVT VT = N->getValueType(0);
7391 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007392 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7393 // We are looking for load i64 and zero extend. We want to transform
7394 // it before legalizer has a chance to expand it. Also look for i64
7395 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007396 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007397 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007398 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007399 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007400 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007401
7402 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007403 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007404 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007405 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007406 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007407 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007408 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007409 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007410 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007411
7412 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007413 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007414
7415 // Load must not be an extload.
7416 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007417 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00007418
Evan Cheng6617eed2008-09-24 23:26:36 +00007419 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7420 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7421 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
7422 DAG.ReplaceAllUsesOfValueWith(SDValue(Base, 1), ResNode.getValue(1));
7423 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007424}
7425
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007426/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007427static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007428 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007429 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007430
7431 // If we have SSE[12] support, try to form min/max nodes.
7432 if (Subtarget->hasSSE2() &&
7433 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7434 if (Cond.getOpcode() == ISD::SETCC) {
7435 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007436 SDValue LHS = N->getOperand(1);
7437 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007438 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7439
7440 unsigned Opcode = 0;
7441 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7442 switch (CC) {
7443 default: break;
7444 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7445 case ISD::SETULE:
7446 case ISD::SETLE:
7447 if (!UnsafeFPMath) break;
7448 // FALL THROUGH.
7449 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7450 case ISD::SETLT:
7451 Opcode = X86ISD::FMIN;
7452 break;
7453
7454 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7455 case ISD::SETUGT:
7456 case ISD::SETGT:
7457 if (!UnsafeFPMath) break;
7458 // FALL THROUGH.
7459 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7460 case ISD::SETGE:
7461 Opcode = X86ISD::FMAX;
7462 break;
7463 }
7464 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7465 switch (CC) {
7466 default: break;
7467 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7468 case ISD::SETUGT:
7469 case ISD::SETGT:
7470 if (!UnsafeFPMath) break;
7471 // FALL THROUGH.
7472 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7473 case ISD::SETGE:
7474 Opcode = X86ISD::FMIN;
7475 break;
7476
7477 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7478 case ISD::SETULE:
7479 case ISD::SETLE:
7480 if (!UnsafeFPMath) break;
7481 // FALL THROUGH.
7482 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7483 case ISD::SETLT:
7484 Opcode = X86ISD::FMAX;
7485 break;
7486 }
7487 }
7488
7489 if (Opcode)
7490 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
7491 }
7492
7493 }
7494
Dan Gohman8181bd12008-07-27 21:46:04 +00007495 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007496}
7497
Chris Lattnerce84ae42008-02-22 02:09:43 +00007498/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007499static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007500 const X86Subtarget *Subtarget) {
7501 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7502 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007503 // A preferable solution to the general problem is to figure out the right
7504 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007505 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007506 if (St->getValue().getValueType().isVector() &&
7507 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007508 isa<LoadSDNode>(St->getValue()) &&
7509 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7510 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007511 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007512 LoadSDNode *Ld = 0;
7513 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007514 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007515 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007516 // Must be a store of a load. We currently handle two cases: the load
7517 // is a direct child, and it's under an intervening TokenFactor. It is
7518 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007519 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007520 Ld = cast<LoadSDNode>(St->getChain());
7521 else if (St->getValue().hasOneUse() &&
7522 ChainVal->getOpcode() == ISD::TokenFactor) {
7523 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007524 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007525 TokenFactorIndex = i;
7526 Ld = cast<LoadSDNode>(St->getValue());
7527 } else
7528 Ops.push_back(ChainVal->getOperand(i));
7529 }
7530 }
7531 if (Ld) {
7532 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7533 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007534 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007535 Ld->getBasePtr(), Ld->getSrcValue(),
7536 Ld->getSrcValueOffset(), Ld->isVolatile(),
7537 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007538 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007539 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00007540 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00007541 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7542 Ops.size());
7543 }
7544 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
7545 St->getSrcValue(), St->getSrcValueOffset(),
7546 St->isVolatile(), St->getAlignment());
7547 }
7548
7549 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00007550 SDValue LoAddr = Ld->getBasePtr();
7551 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007552 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007553
Dan Gohman8181bd12008-07-27 21:46:04 +00007554 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007555 Ld->getSrcValue(), Ld->getSrcValueOffset(),
7556 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007557 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007558 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
7559 Ld->isVolatile(),
7560 MinAlign(Ld->getAlignment(), 4));
7561
Dan Gohman8181bd12008-07-27 21:46:04 +00007562 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007563 if (TokenFactorIndex != -1) {
7564 Ops.push_back(LoLd);
7565 Ops.push_back(HiLd);
7566 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7567 Ops.size());
7568 }
7569
7570 LoAddr = St->getBasePtr();
7571 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007572 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007573
Dan Gohman8181bd12008-07-27 21:46:04 +00007574 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007575 St->getSrcValue(), St->getSrcValueOffset(),
7576 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007577 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00007578 St->getSrcValue(),
7579 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00007580 St->isVolatile(),
7581 MinAlign(St->getAlignment(), 4));
7582 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00007583 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00007584 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007585 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00007586}
7587
Chris Lattner470d5dc2008-01-25 06:14:17 +00007588/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7589/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007590static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00007591 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7592 // F[X]OR(0.0, x) -> x
7593 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00007594 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7595 if (C->getValueAPF().isPosZero())
7596 return N->getOperand(1);
7597 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7598 if (C->getValueAPF().isPosZero())
7599 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007600 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007601}
7602
7603/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007604static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007605 // FAND(0.0, x) -> 0.0
7606 // FAND(x, 0.0) -> 0.0
7607 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7608 if (C->getValueAPF().isPosZero())
7609 return N->getOperand(0);
7610 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7611 if (C->getValueAPF().isPosZero())
7612 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007613 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007614}
7615
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007616
Dan Gohman8181bd12008-07-27 21:46:04 +00007617SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00007618 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007619 SelectionDAG &DAG = DCI.DAG;
7620 switch (N->getOpcode()) {
7621 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007622 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7623 case ISD::BUILD_VECTOR:
7624 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007625 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007626 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007627 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007628 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7629 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007630 }
7631
Dan Gohman8181bd12008-07-27 21:46:04 +00007632 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007633}
7634
7635//===----------------------------------------------------------------------===//
7636// X86 Inline Assembly Support
7637//===----------------------------------------------------------------------===//
7638
7639/// getConstraintType - Given a constraint letter, return the type of
7640/// constraint it is for this target.
7641X86TargetLowering::ConstraintType
7642X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7643 if (Constraint.size() == 1) {
7644 switch (Constraint[0]) {
7645 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +00007646 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +00007647 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007648 case 'r':
7649 case 'R':
7650 case 'l':
7651 case 'q':
7652 case 'Q':
7653 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007654 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007655 case 'Y':
7656 return C_RegisterClass;
7657 default:
7658 break;
7659 }
7660 }
7661 return TargetLowering::getConstraintType(Constraint);
7662}
7663
Dale Johannesene99fc902008-01-29 02:21:21 +00007664/// LowerXConstraint - try to replace an X constraint, which matches anything,
7665/// with another that has more specific requirements based on the type of the
7666/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007667const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007668LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007669 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7670 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007671 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007672 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007673 return "Y";
7674 if (Subtarget->hasSSE1())
7675 return "x";
7676 }
7677
7678 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007679}
7680
Chris Lattnera531abc2007-08-25 00:47:38 +00007681/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7682/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007683void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007684 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00007685 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00007686 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007687 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007688 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007689
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007690 switch (Constraint) {
7691 default: break;
7692 case 'I':
7693 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007694 if (C->getZExtValue() <= 31) {
7695 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007696 break;
7697 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007698 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007699 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00007700 case 'J':
7701 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
7702 if (C->getZExtValue() <= 63) {
7703 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
7704 break;
7705 }
7706 }
7707 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007708 case 'N':
7709 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007710 if (C->getZExtValue() <= 255) {
7711 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007712 break;
7713 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007714 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007715 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007716 case 'i': {
7717 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007718 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007719 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007720 break;
7721 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007722
7723 // If we are in non-pic codegen mode, we allow the address of a global (with
7724 // an optional displacement) to be used with 'i'.
7725 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7726 int64_t Offset = 0;
7727
7728 // Match either (GA) or (GA+C)
7729 if (GA) {
7730 Offset = GA->getOffset();
7731 } else if (Op.getOpcode() == ISD::ADD) {
7732 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7733 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7734 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007735 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007736 } else {
7737 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7738 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7739 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007740 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007741 else
7742 C = 0, GA = 0;
7743 }
7744 }
7745
7746 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00007747 if (hasMemory)
Dan Gohman36322c72008-10-18 02:06:02 +00007748 Op = LowerGlobalAddress(GA->getGlobal(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00007749 else
7750 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7751 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007752 Result = Op;
7753 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007754 }
7755
7756 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007757 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007758 }
7759 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007760
Gabor Greif1c80d112008-08-28 21:40:38 +00007761 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007762 Ops.push_back(Result);
7763 return;
7764 }
Evan Cheng7f250d62008-09-24 00:05:32 +00007765 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
7766 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007767}
7768
7769std::vector<unsigned> X86TargetLowering::
7770getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007771 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007772 if (Constraint.size() == 1) {
7773 // FIXME: not handling fp-stack yet!
7774 switch (Constraint[0]) { // GCC X86 Constraint Letters
7775 default: break; // Unknown constraint letter
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007776 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7777 case 'Q': // Q_REGS
7778 if (VT == MVT::i32)
7779 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7780 else if (VT == MVT::i16)
7781 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7782 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007783 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007784 else if (VT == MVT::i64)
7785 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7786 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007787 }
7788 }
7789
7790 return std::vector<unsigned>();
7791}
7792
7793std::pair<unsigned, const TargetRegisterClass*>
7794X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007795 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007796 // First, see if this is a constraint that directly corresponds to an LLVM
7797 // register class.
7798 if (Constraint.size() == 1) {
7799 // GCC Constraint Letters
7800 switch (Constraint[0]) {
7801 default: break;
7802 case 'r': // GENERAL_REGS
7803 case 'R': // LEGACY_REGS
7804 case 'l': // INDEX_REGS
Chris Lattnerbbfea052008-10-17 18:15:05 +00007805 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007806 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00007807 if (VT == MVT::i16)
7808 return std::make_pair(0U, X86::GR16RegisterClass);
7809 if (VT == MVT::i32 || !Subtarget->is64Bit())
7810 return std::make_pair(0U, X86::GR32RegisterClass);
7811 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00007812 case 'f': // FP Stack registers.
7813 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7814 // value to the correct fpstack register class.
7815 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7816 return std::make_pair(0U, X86::RFP32RegisterClass);
7817 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7818 return std::make_pair(0U, X86::RFP64RegisterClass);
7819 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007820 case 'y': // MMX_REGS if MMX allowed.
7821 if (!Subtarget->hasMMX()) break;
7822 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007823 case 'Y': // SSE_REGS if SSE2 allowed
7824 if (!Subtarget->hasSSE2()) break;
7825 // FALL THROUGH.
7826 case 'x': // SSE_REGS if SSE1 allowed
7827 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007828
7829 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007830 default: break;
7831 // Scalar SSE types.
7832 case MVT::f32:
7833 case MVT::i32:
7834 return std::make_pair(0U, X86::FR32RegisterClass);
7835 case MVT::f64:
7836 case MVT::i64:
7837 return std::make_pair(0U, X86::FR64RegisterClass);
7838 // Vector types.
7839 case MVT::v16i8:
7840 case MVT::v8i16:
7841 case MVT::v4i32:
7842 case MVT::v2i64:
7843 case MVT::v4f32:
7844 case MVT::v2f64:
7845 return std::make_pair(0U, X86::VR128RegisterClass);
7846 }
7847 break;
7848 }
7849 }
7850
7851 // Use the default implementation in TargetLowering to convert the register
7852 // constraint into a member of a register class.
7853 std::pair<unsigned, const TargetRegisterClass*> Res;
7854 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7855
7856 // Not found as a standard register?
7857 if (Res.second == 0) {
7858 // GCC calls "st(0)" just plain "st".
7859 if (StringsEqualNoCase("{st}", Constraint)) {
7860 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007861 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007862 }
Dale Johannesen73920c02008-11-13 21:52:36 +00007863 // 'A' means EAX + EDX.
7864 if (Constraint == "A") {
7865 Res.first = X86::EAX;
7866 Res.second = X86::GRADRegisterClass;
7867 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007868 return Res;
7869 }
7870
7871 // Otherwise, check to see if this is a register class of the wrong value
7872 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7873 // turn into {ax},{dx}.
7874 if (Res.second->hasType(VT))
7875 return Res; // Correct type already, nothing to do.
7876
7877 // All of the single-register GCC register classes map their values onto
7878 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7879 // really want an 8-bit or 32-bit register, map to the appropriate register
7880 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007881 if (Res.second == X86::GR16RegisterClass) {
7882 if (VT == MVT::i8) {
7883 unsigned DestReg = 0;
7884 switch (Res.first) {
7885 default: break;
7886 case X86::AX: DestReg = X86::AL; break;
7887 case X86::DX: DestReg = X86::DL; break;
7888 case X86::CX: DestReg = X86::CL; break;
7889 case X86::BX: DestReg = X86::BL; break;
7890 }
7891 if (DestReg) {
7892 Res.first = DestReg;
7893 Res.second = Res.second = X86::GR8RegisterClass;
7894 }
7895 } else if (VT == MVT::i32) {
7896 unsigned DestReg = 0;
7897 switch (Res.first) {
7898 default: break;
7899 case X86::AX: DestReg = X86::EAX; break;
7900 case X86::DX: DestReg = X86::EDX; break;
7901 case X86::CX: DestReg = X86::ECX; break;
7902 case X86::BX: DestReg = X86::EBX; break;
7903 case X86::SI: DestReg = X86::ESI; break;
7904 case X86::DI: DestReg = X86::EDI; break;
7905 case X86::BP: DestReg = X86::EBP; break;
7906 case X86::SP: DestReg = X86::ESP; break;
7907 }
7908 if (DestReg) {
7909 Res.first = DestReg;
7910 Res.second = Res.second = X86::GR32RegisterClass;
7911 }
7912 } else if (VT == MVT::i64) {
7913 unsigned DestReg = 0;
7914 switch (Res.first) {
7915 default: break;
7916 case X86::AX: DestReg = X86::RAX; break;
7917 case X86::DX: DestReg = X86::RDX; break;
7918 case X86::CX: DestReg = X86::RCX; break;
7919 case X86::BX: DestReg = X86::RBX; break;
7920 case X86::SI: DestReg = X86::RSI; break;
7921 case X86::DI: DestReg = X86::RDI; break;
7922 case X86::BP: DestReg = X86::RBP; break;
7923 case X86::SP: DestReg = X86::RSP; break;
7924 }
7925 if (DestReg) {
7926 Res.first = DestReg;
7927 Res.second = Res.second = X86::GR64RegisterClass;
7928 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007929 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007930 } else if (Res.second == X86::FR32RegisterClass ||
7931 Res.second == X86::FR64RegisterClass ||
7932 Res.second == X86::VR128RegisterClass) {
7933 // Handle references to XMM physical registers that got mapped into the
7934 // wrong class. This can happen with constraints like {xmm0} where the
7935 // target independent register mapper will just pick the first match it can
7936 // find, ignoring the required type.
7937 if (VT == MVT::f32)
7938 Res.second = X86::FR32RegisterClass;
7939 else if (VT == MVT::f64)
7940 Res.second = X86::FR64RegisterClass;
7941 else if (X86::VR128RegisterClass->hasType(VT))
7942 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007943 }
7944
7945 return Res;
7946}
Mon P Wang1448aad2008-10-30 08:01:45 +00007947
7948//===----------------------------------------------------------------------===//
7949// X86 Widen vector type
7950//===----------------------------------------------------------------------===//
7951
7952/// getWidenVectorType: given a vector type, returns the type to widen
7953/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
7954/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +00007955/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +00007956/// scalarizing vs using the wider vector type.
7957
7958MVT X86TargetLowering::getWidenVectorType(MVT VT) {
7959 assert(VT.isVector());
7960 if (isTypeLegal(VT))
7961 return VT;
7962
7963 // TODO: In computeRegisterProperty, we can compute the list of legal vector
7964 // type based on element type. This would speed up our search (though
7965 // it may not be worth it since the size of the list is relatively
7966 // small).
7967 MVT EltVT = VT.getVectorElementType();
7968 unsigned NElts = VT.getVectorNumElements();
7969
7970 // On X86, it make sense to widen any vector wider than 1
7971 if (NElts <= 1)
7972 return MVT::Other;
7973
7974 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
7975 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
7976 MVT SVT = (MVT::SimpleValueType)nVT;
7977
7978 if (isTypeLegal(SVT) &&
7979 SVT.getVectorElementType() == EltVT &&
7980 SVT.getVectorNumElements() > NElts)
7981 return SVT;
7982 }
7983 return MVT::Other;
7984}