blob: 7a4d34d10a4db0654aa7696ad2c411b38f128c0e [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);
Bill Wendling14a30ef2009-01-17 03:56:04 +0000122
123 // We have faster algorithm for ui32->single only.
124 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Dale Johannesena359b8b2008-10-21 20:50:01 +0000125 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
127 }
128
129 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
130 // this operation.
131 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
132 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
133 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000134 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000136 // f32 and f64 cases are Legal, f80 case is not
137 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
138 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
140 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
141 }
142
Dale Johannesen958b08b2007-09-19 23:55:34 +0000143 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
144 // are Legal, f80 is custom lowered.
145 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
146 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147
148 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
149 // this operation.
150 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
151 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
152
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000153 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000155 // f32 and f64 cases are Legal, f80 case is not
156 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 } else {
158 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
159 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
160 }
161
162 // Handle FP_TO_UINT by promoting the destination to a larger signed
163 // conversion.
164 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
165 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
166 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
167
168 if (Subtarget->is64Bit()) {
169 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
170 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
171 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000172 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 // Expand FP_TO_UINT into a select.
174 // FIXME: We would like to use a Custom expander here eventually to do
175 // the optimal thing for SSE vs. the default expansion in the legalizer.
176 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
177 else
178 // With SSE3 we can use fisttpll to convert to a signed i64.
179 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
180 }
181
182 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000183 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
185 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
186 }
187
Dan Gohman8450d862008-02-18 19:34:53 +0000188 // Scalar integer divide and remainder are lowered to use operations that
189 // produce two results, to match the available instructions. This exposes
190 // the two-result form to trivial CSE, which is able to combine x/y and x%y
191 // into a single instruction.
192 //
193 // Scalar integer multiply-high is also lowered to use two-result
194 // operations, to match the available instructions. However, plain multiply
195 // (low) operations are left as Legal, as there are single-result
196 // instructions for this in x86. Using the two-result multiply instructions
197 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000198 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
199 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
200 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
201 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
202 setOperationAction(ISD::SREM , MVT::i8 , Expand);
203 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000204 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
205 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
206 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
207 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
208 setOperationAction(ISD::SREM , MVT::i16 , Expand);
209 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000210 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
211 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
212 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
213 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
214 setOperationAction(ISD::SREM , MVT::i32 , Expand);
215 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000216 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
217 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
218 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
219 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
220 setOperationAction(ISD::SREM , MVT::i64 , Expand);
221 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000222
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
224 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
225 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
226 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000228 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
229 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
230 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
232 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000233 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000235 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000236 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000237
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000239 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
240 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000242 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
243 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000245 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
246 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 if (Subtarget->is64Bit()) {
248 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000249 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
250 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 }
252
253 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
254 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
255
256 // These should be promoted to a larger select which is supported.
257 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
258 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
259 // X86 wants to expand cmov itself.
260 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
261 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
262 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
263 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000264 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
266 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
267 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
268 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
269 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000270 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 if (Subtarget->is64Bit()) {
272 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
273 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
274 }
275 // X86 ret instruction may pop stack.
276 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000277 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278
279 // Darwin ABI issue.
280 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
281 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
282 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
283 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000284 if (Subtarget->is64Bit())
285 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000286 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 if (Subtarget->is64Bit()) {
288 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
289 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
290 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000291 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 }
293 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
294 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
295 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
296 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000297 if (Subtarget->is64Bit()) {
298 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
299 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
300 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
301 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302
Evan Cheng8d51ab32008-03-10 19:38:10 +0000303 if (Subtarget->hasSSE1())
304 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000305
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000306 if (!Subtarget->hasSSE2())
307 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
308
Mon P Wang078a62d2008-05-05 19:05:59 +0000309 // Expand certain atomics
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000310 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
311 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
312 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
313 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000314
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000315 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
316 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
317 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
318 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000319
Dale Johannesenf160d802008-10-02 18:53:47 +0000320 if (!Subtarget->is64Bit()) {
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000321 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
322 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
323 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
324 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
325 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
326 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
327 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesenf160d802008-10-02 18:53:47 +0000328 }
329
Dan Gohman472d12c2008-06-30 20:59:49 +0000330 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
331 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 // FIXME - use subtarget debug flags
333 if (!Subtarget->isTargetDarwin() &&
334 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000335 !Subtarget->isTargetCygMing()) {
336 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
337 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
338 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339
340 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
341 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
342 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
343 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
344 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 setExceptionPointerRegister(X86::RAX);
346 setExceptionSelectorRegister(X86::RDX);
347 } else {
348 setExceptionPointerRegister(X86::EAX);
349 setExceptionSelectorRegister(X86::EDX);
350 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000351 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000352 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
353
Duncan Sands7407a9f2007-09-11 14:10:23 +0000354 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000355
Chris Lattner56b941f2008-01-15 21:58:22 +0000356 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000357
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
359 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000361 if (Subtarget->is64Bit()) {
362 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000364 } else {
365 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000367 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368
369 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
370 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
371 if (Subtarget->is64Bit())
372 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
373 if (Subtarget->isTargetCygMing())
374 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
375 else
376 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
377
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000378 if (X86ScalarSSEf64) {
379 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 // Set up the FP register classes.
381 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
382 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
383
384 // Use ANDPD to simulate FABS.
385 setOperationAction(ISD::FABS , MVT::f64, Custom);
386 setOperationAction(ISD::FABS , MVT::f32, Custom);
387
388 // Use XORP to simulate FNEG.
389 setOperationAction(ISD::FNEG , MVT::f64, Custom);
390 setOperationAction(ISD::FNEG , MVT::f32, Custom);
391
392 // Use ANDPD and ORPD to simulate FCOPYSIGN.
393 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
394 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
395
396 // We don't support sin/cos/fmod
397 setOperationAction(ISD::FSIN , MVT::f64, Expand);
398 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 setOperationAction(ISD::FSIN , MVT::f32, Expand);
400 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401
402 // Expand FP immediates into loads from the stack, except for the special
403 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000404 addLegalFPImmediate(APFloat(+0.0)); // xorpd
405 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000406
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000407 // Floating truncations from f80 and extensions to f80 go through memory.
408 // If optimizing, we lie about this though and handle it in
409 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
410 if (Fast) {
411 setConvertAction(MVT::f32, MVT::f80, Expand);
412 setConvertAction(MVT::f64, MVT::f80, Expand);
413 setConvertAction(MVT::f80, MVT::f32, Expand);
414 setConvertAction(MVT::f80, MVT::f64, Expand);
415 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000416 } else if (X86ScalarSSEf32) {
417 // Use SSE for f32, x87 for f64.
418 // Set up the FP register classes.
419 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
420 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
421
422 // Use ANDPS to simulate FABS.
423 setOperationAction(ISD::FABS , MVT::f32, Custom);
424
425 // Use XORP to simulate FNEG.
426 setOperationAction(ISD::FNEG , MVT::f32, Custom);
427
428 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
429
430 // Use ANDPS and ORPS to simulate FCOPYSIGN.
431 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
432 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
433
434 // We don't support sin/cos/fmod
435 setOperationAction(ISD::FSIN , MVT::f32, Expand);
436 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000437
Nate Begemane2ba64f2008-02-14 08:57:00 +0000438 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000439 addLegalFPImmediate(APFloat(+0.0f)); // xorps
440 addLegalFPImmediate(APFloat(+0.0)); // FLD0
441 addLegalFPImmediate(APFloat(+1.0)); // FLD1
442 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
443 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
444
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000445 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
446 // this though and handle it in InstructionSelectPreprocess so that
447 // dagcombine2 can hack on these.
448 if (Fast) {
449 setConvertAction(MVT::f32, MVT::f64, Expand);
450 setConvertAction(MVT::f32, MVT::f80, Expand);
451 setConvertAction(MVT::f80, MVT::f32, Expand);
452 setConvertAction(MVT::f64, MVT::f32, Expand);
453 // And x87->x87 truncations also.
454 setConvertAction(MVT::f80, MVT::f64, Expand);
455 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000456
457 if (!UnsafeFPMath) {
458 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
459 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
460 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000462 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 // Set up the FP register classes.
464 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
465 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
466
467 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
468 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
469 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
470 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000471
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000472 // Floating truncations go through memory. If optimizing, we lie about
473 // this though and handle it in InstructionSelectPreprocess so that
474 // dagcombine2 can hack on these.
475 if (Fast) {
476 setConvertAction(MVT::f80, MVT::f32, Expand);
477 setConvertAction(MVT::f64, MVT::f32, Expand);
478 setConvertAction(MVT::f80, MVT::f64, Expand);
479 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480
481 if (!UnsafeFPMath) {
482 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
483 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
484 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000485 addLegalFPImmediate(APFloat(+0.0)); // FLD0
486 addLegalFPImmediate(APFloat(+1.0)); // FLD1
487 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
488 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000489 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
490 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
491 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
492 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 }
494
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000495 // Long double always uses X87.
496 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000497 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
498 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000499 {
Dale Johannesen6e547b42008-10-09 23:00:39 +0000500 bool ignored;
Chris Lattnerdd867392008-01-27 06:19:31 +0000501 APFloat TmpFlt(+0.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000502 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
503 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000504 addLegalFPImmediate(TmpFlt); // FLD0
505 TmpFlt.changeSign();
506 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
507 APFloat TmpFlt2(+1.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000508 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
509 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000510 addLegalFPImmediate(TmpFlt2); // FLD1
511 TmpFlt2.changeSign();
512 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
513 }
514
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000515 if (!UnsafeFPMath) {
516 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
517 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
518 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000519
Dan Gohman2f7b1982007-10-11 23:21:31 +0000520 // Always use a library call for pow.
521 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
522 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
523 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
524
Dale Johannesen92b33082008-09-04 00:47:13 +0000525 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000526 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000527 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000528 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000529 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
530
Mon P Wanga5a239f2008-11-06 05:31:54 +0000531 // First set operation action for all vector types to either promote
Mon P Wang1448aad2008-10-30 08:01:45 +0000532 // (for widening) or expand (for scalarization). Then we will selectively
533 // turn on ones that can be effectively codegen'd.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
535 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000536 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif825aa892008-08-28 23:19:51 +0000549 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
551 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands92c43912008-06-06 12:08:01 +0000552 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesen177edff2008-09-10 17:31:40 +0000574 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 }
580
Mon P Wang1f292322008-11-23 04:37:22 +0000581 if (!DisableMMX && Subtarget->hasMMX()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
583 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
584 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000585 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
587
588 // FIXME: add MMX packed arithmetics
589
590 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
591 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
592 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
593 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
594
595 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
596 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
597 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000598 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599
600 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
601 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
602
603 setOperationAction(ISD::AND, MVT::v8i8, Promote);
604 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
605 setOperationAction(ISD::AND, MVT::v4i16, Promote);
606 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
607 setOperationAction(ISD::AND, MVT::v2i32, Promote);
608 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
609 setOperationAction(ISD::AND, MVT::v1i64, Legal);
610
611 setOperationAction(ISD::OR, MVT::v8i8, Promote);
612 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
613 setOperationAction(ISD::OR, MVT::v4i16, Promote);
614 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
615 setOperationAction(ISD::OR, MVT::v2i32, Promote);
616 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
617 setOperationAction(ISD::OR, MVT::v1i64, Legal);
618
619 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
620 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
621 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
622 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
623 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
624 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
625 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
626
627 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
628 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
629 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
630 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
631 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
632 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000633 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
634 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
636
637 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
638 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
639 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000640 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
642
643 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
644 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
645 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
646 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
647
Evan Cheng759fe022008-07-22 18:39:19 +0000648 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
650 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000652
653 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang83edba52008-12-12 01:25:51 +0000654
655 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
656 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
657 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
658 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
659 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
660 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661 }
662
663 if (Subtarget->hasSSE1()) {
664 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
665
666 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
667 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
668 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
669 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
670 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
671 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
673 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
674 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
675 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
676 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000677 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 }
679
680 if (Subtarget->hasSSE2()) {
681 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
682 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
683 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
684 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
685 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
686
687 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
688 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
689 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
690 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wang14edb092008-12-18 21:42:19 +0000691 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
693 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
694 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
695 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
696 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
697 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
698 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
699 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
700 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
701 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
702 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703
Nate Begeman03605a02008-07-17 16:51:19 +0000704 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
705 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
706 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
707 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000708
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
710 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
711 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
712 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
714
715 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000716 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
717 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000718 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000719 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000720 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000721 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
722 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
723 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 }
725 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
726 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
727 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
728 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000729 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000731 if (Subtarget->is64Bit()) {
732 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000733 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000734 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735
736 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
737 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000738 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
739 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
740 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
741 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
742 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
743 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
744 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
745 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
746 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
747 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748 }
749
Chris Lattner3bc08502008-01-17 19:59:44 +0000750 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000751
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 // Custom lower v2i64 and v2f64 selects.
753 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
754 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
755 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
756 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000757
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000759
760 if (Subtarget->hasSSE41()) {
761 // FIXME: Do we need to handle scalar-to-vector here?
762 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
763
764 // i8 and i16 vectors are custom , because the source register and source
765 // source memory operand types are not the same width. f32 vectors are
766 // custom since the immediate controlling the insert encodes additional
767 // information.
768 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
769 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangac2a3c52009-01-15 21:10:20 +0000770 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000771 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
772
773 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
774 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangac2a3c52009-01-15 21:10:20 +0000775 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng6c249332008-03-24 21:52:23 +0000776 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000777
778 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000779 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
780 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000781 }
782 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000783
Nate Begeman03605a02008-07-17 16:51:19 +0000784 if (Subtarget->hasSSE42()) {
785 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
786 }
787
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788 // We want to custom lower some of our intrinsics.
789 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
790
Bill Wendling7e04be62008-12-09 22:08:41 +0000791 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling4c134df2008-11-24 19:21:46 +0000792 setOperationAction(ISD::SADDO, MVT::i32, Custom);
793 setOperationAction(ISD::SADDO, MVT::i64, Custom);
794 setOperationAction(ISD::UADDO, MVT::i32, Custom);
795 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling7e04be62008-12-09 22:08:41 +0000796 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
797 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
798 setOperationAction(ISD::USUBO, MVT::i32, Custom);
799 setOperationAction(ISD::USUBO, MVT::i64, Custom);
800 setOperationAction(ISD::SMULO, MVT::i32, Custom);
801 setOperationAction(ISD::SMULO, MVT::i64, Custom);
802 setOperationAction(ISD::UMULO, MVT::i32, Custom);
803 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Bill Wendling4c134df2008-11-24 19:21:46 +0000804
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 // We have target-specific dag combine patterns for the following nodes:
806 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000807 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 setTargetDAGCombine(ISD::SELECT);
sampo025b75c2009-01-26 00:52:55 +0000809 setTargetDAGCombine(ISD::SHL);
810 setTargetDAGCombine(ISD::SRA);
811 setTargetDAGCombine(ISD::SRL);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000812 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000813
814 computeRegisterProperties();
815
816 // FIXME: These should be based on subtarget info. Plus, the values should
817 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000818 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
819 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
820 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000822 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823}
824
Scott Michel502151f2008-03-10 15:42:14 +0000825
Duncan Sands4a361272009-01-01 15:52:00 +0000826MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel502151f2008-03-10 15:42:14 +0000827 return MVT::i8;
828}
829
830
Evan Cheng5a67b812008-01-23 23:17:41 +0000831/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
832/// the desired ByVal argument alignment.
833static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
834 if (MaxAlign == 16)
835 return;
836 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
837 if (VTy->getBitWidth() == 128)
838 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000839 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
840 unsigned EltAlign = 0;
841 getMaxByValAlign(ATy->getElementType(), EltAlign);
842 if (EltAlign > MaxAlign)
843 MaxAlign = EltAlign;
844 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
845 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
846 unsigned EltAlign = 0;
847 getMaxByValAlign(STy->getElementType(i), EltAlign);
848 if (EltAlign > MaxAlign)
849 MaxAlign = EltAlign;
850 if (MaxAlign == 16)
851 break;
852 }
853 }
854 return;
855}
856
857/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
858/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000859/// that contain SSE vectors are placed at 16-byte boundaries while the rest
860/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000861unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000862 if (Subtarget->is64Bit()) {
863 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000864 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000865 if (TyAlign > 8)
866 return TyAlign;
867 return 8;
868 }
869
Evan Cheng5a67b812008-01-23 23:17:41 +0000870 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000871 if (Subtarget->hasSSE1())
872 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000873 return Align;
874}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875
Evan Cheng8c590372008-05-15 08:39:06 +0000876/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000877/// and store operations as a result of memset, memcpy, and memmove
878/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000879/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000880MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000881X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
882 bool isSrcConst, bool isSrcStr) const {
Chris Lattnerf0bf1062008-10-28 05:49:35 +0000883 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
884 // linux. This is because the stack realignment code can't handle certain
885 // cases like PR2962. This should be removed when PR2962 is fixed.
886 if (Subtarget->getStackAlignment() >= 16) {
887 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
888 return MVT::v4i32;
889 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
890 return MVT::v4f32;
891 }
Evan Cheng8c590372008-05-15 08:39:06 +0000892 if (Subtarget->is64Bit() && Size >= 8)
893 return MVT::i64;
894 return MVT::i32;
895}
896
897
Evan Cheng6fb06762007-11-09 01:32:10 +0000898/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
899/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000900SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000901 SelectionDAG &DAG) const {
902 if (usesGlobalOffsetTable())
903 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
904 if (!Subtarget->isPICStyleRIPRel())
905 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
906 return Table;
907}
908
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909//===----------------------------------------------------------------------===//
910// Return Value Calling Convention Implementation
911//===----------------------------------------------------------------------===//
912
913#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000914
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000916SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
918
919 SmallVector<CCValAssign, 16> RVLocs;
920 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
921 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
922 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000923 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000924
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000925 // If this is the first return lowered for this function, add the regs to the
926 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000927 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000928 for (unsigned i = 0; i != RVLocs.size(); ++i)
929 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000930 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000931 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000932 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000933
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000934 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000935 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000936 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000937 SDValue TailCall = Chain;
938 SDValue TargetAddress = TailCall.getOperand(1);
939 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000940 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000941 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000942 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000943 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000944 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
945 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000946 assert(StackAdjustment.getOpcode() == ISD::Constant &&
947 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000948
Dan Gohman8181bd12008-07-27 21:46:04 +0000949 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000950 Operands.push_back(Chain.getOperand(0));
951 Operands.push_back(TargetAddress);
952 Operands.push_back(StackAdjustment);
953 // Copy registers used by the call. Last operand is a flag so it is not
954 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000955 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000956 Operands.push_back(Chain.getOperand(i));
957 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000958 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
959 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000960 }
961
962 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000963 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000964
Dan Gohman8181bd12008-07-27 21:46:04 +0000965 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000966 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
967 // Operand #1 = Bytes To Pop
968 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
969
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000970 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000971 for (unsigned i = 0; i != RVLocs.size(); ++i) {
972 CCValAssign &VA = RVLocs[i];
973 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000974 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000975
Chris Lattnerb56cc342008-03-11 03:23:40 +0000976 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
977 // the RET instruction and handled by the FP Stackifier.
978 if (RVLocs[i].getLocReg() == X86::ST0 ||
979 RVLocs[i].getLocReg() == X86::ST1) {
980 // If this is a copy from an xmm register to ST(0), use an FPExtend to
981 // change the value to the FP stack register class.
982 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
983 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
984 RetOps.push_back(ValToCopy);
985 // Don't emit a copytoreg.
986 continue;
987 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000988
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000989 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000990 Flag = Chain.getValue(1);
991 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000992
993 // The x86-64 ABI for returning structs by value requires that we copy
994 // the sret argument into %rax for the return. We saved the argument into
995 // a virtual register in the entry block, so now we copy the value out
996 // and into %rax.
997 if (Subtarget->is64Bit() &&
998 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
999 MachineFunction &MF = DAG.getMachineFunction();
1000 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1001 unsigned Reg = FuncInfo->getSRetReturnReg();
1002 if (!Reg) {
1003 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1004 FuncInfo->setSRetReturnReg(Reg);
1005 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001006 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001007
1008 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
1009 Flag = Chain.getValue(1);
1010 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001011
Chris Lattnerb56cc342008-03-11 03:23:40 +00001012 RetOps[0] = Chain; // Update chain.
1013
1014 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +00001015 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +00001016 RetOps.push_back(Flag);
1017
1018 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001019}
1020
1021
1022/// LowerCallResult - Lower the result values of an ISD::CALL into the
1023/// appropriate copies out of appropriate physical registers. This assumes that
1024/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1025/// being lowered. The returns a SDNode with the same number of values as the
1026/// ISD::CALL.
1027SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +00001028LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029 unsigned CallingConv, SelectionDAG &DAG) {
1030
1031 // Assign locations to each value returned by this call.
1032 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +00001033 bool isVarArg = TheCall->isVarArg();
Edwin Törökaf8e1332009-02-01 18:15:56 +00001034 bool Is64Bit = Subtarget->is64Bit();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
1036 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1037
Dan Gohman8181bd12008-07-27 21:46:04 +00001038 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039
1040 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001041 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00001042 MVT CopyVT = RVLocs[i].getValVT();
Edwin Törökaf8e1332009-02-01 18:15:56 +00001043
1044 // If this is x86-64, and we disabled SSE, we can't return FP values
1045 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
1046 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
1047 cerr << "SSE register return with SSE disabled\n";
1048 exit(1);
1049 }
1050
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001051 // If this is a call to a function that returns an fp value on the floating
1052 // point stack, but where we prefer to use the value in xmm registers, copy
1053 // 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 +00001054 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1055 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001056 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1057 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001059
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001060 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1061 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001062 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001063 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001064
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001065 if (CopyVT != RVLocs[i].getValVT()) {
1066 // Round the F80 the right size, which also moves to the appropriate xmm
1067 // register.
1068 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1069 // This truncation won't change the value.
1070 DAG.getIntPtrConstant(1));
1071 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001072
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001073 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 }
Duncan Sands698842f2008-07-02 17:40:58 +00001075
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001076 // Merge everything together with a MERGE_VALUES node.
1077 ResultVals.push_back(Chain);
Duncan Sands42d7bb82008-12-01 11:41:29 +00001078 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(), &ResultVals[0],
1079 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001080}
1081
1082
1083//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001084// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001085//===----------------------------------------------------------------------===//
1086// StdCall calling convention seems to be standard for many Windows' API
1087// routines and around. It differs from C calling convention just a little:
1088// callee should clean up the stack, not caller. Symbols should be also
1089// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001090// For info on fast calling convention see Fast Calling Convention (tail call)
1091// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001092
1093/// AddLiveIn - This helper function adds the specified physical register to the
1094/// MachineFunction as a live in value. It also creates a corresponding virtual
1095/// register for it.
1096static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1097 const TargetRegisterClass *RC) {
1098 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001099 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1100 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001101 return VReg;
1102}
1103
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001104/// CallIsStructReturn - Determines whether a CALL node uses struct return
1105/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001106static bool CallIsStructReturn(CallSDNode *TheCall) {
1107 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001108 if (!NumOps)
1109 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001110
Dan Gohman705e3f72008-09-13 01:54:27 +00001111 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001112}
1113
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001114/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1115/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001116static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001117 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001118 if (!NumArgs)
1119 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001120
1121 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001122}
1123
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001124/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1125/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001126/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001127bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001128 if (IsVarArg)
1129 return false;
1130
Dan Gohman705e3f72008-09-13 01:54:27 +00001131 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001132 default:
1133 return false;
1134 case CallingConv::X86_StdCall:
1135 return !Subtarget->is64Bit();
1136 case CallingConv::X86_FastCall:
1137 return !Subtarget->is64Bit();
1138 case CallingConv::Fast:
1139 return PerformTailCallOpt;
1140 }
1141}
1142
Dan Gohman705e3f72008-09-13 01:54:27 +00001143/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1144/// given CallingConvention value.
1145CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001146 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001147 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001148 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001149 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1150 return CC_X86_64_TailCall;
1151 else
1152 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001153 }
1154
Gordon Henriksen18ace102008-01-05 16:56:59 +00001155 if (CC == CallingConv::X86_FastCall)
1156 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001157 else if (CC == CallingConv::Fast)
1158 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001159 else
1160 return CC_X86_32_C;
1161}
1162
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001163/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1164/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001165NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001166X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001167 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001168 if (CC == CallingConv::X86_FastCall)
1169 return FastCall;
1170 else if (CC == CallingConv::X86_StdCall)
1171 return StdCall;
1172 return None;
1173}
1174
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001175
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001176/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1177/// in a register before calling.
1178bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1179 return !IsTailCall && !Is64Bit &&
1180 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1181 Subtarget->isPICStyleGOT();
1182}
1183
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001184/// CallRequiresFnAddressInReg - Check whether the call requires the function
1185/// address to be loaded in a register.
1186bool
1187X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1188 return !Is64Bit && IsTailCall &&
1189 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1190 Subtarget->isPICStyleGOT();
1191}
1192
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001193/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1194/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001195/// the specific parameter attribute. The copy will be passed as a byval
1196/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001197static SDValue
1198CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001199 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001200 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001201 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001202 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001203}
1204
Dan Gohman8181bd12008-07-27 21:46:04 +00001205SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001206 const CCValAssign &VA,
1207 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001208 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001209 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001210 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001211 ISD::ArgFlagsTy Flags =
1212 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001213 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001214 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001215
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001216 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1217 // changed with more analysis.
1218 // In case of tail call optimization mark all arguments mutable. Since they
1219 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001220 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001221 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001222 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001223 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001224 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001225 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001226 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001227}
1228
Dan Gohman8181bd12008-07-27 21:46:04 +00001229SDValue
1230X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001231 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001232 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1233
1234 const Function* Fn = MF.getFunction();
1235 if (Fn->hasExternalLinkage() &&
1236 Subtarget->isTargetCygMing() &&
1237 Fn->getName() == "main")
1238 FuncInfo->setForceFramePointer(true);
1239
1240 // Decorate the function name.
1241 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1242
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001243 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001244 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001245 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001246 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001247 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001248 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001249
1250 assert(!(isVarArg && CC == CallingConv::Fast) &&
1251 "Var args not supported with calling convention fastcc");
1252
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001253 // Assign locations to all of the incoming arguments.
1254 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001255 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001256 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001257
Dan Gohman8181bd12008-07-27 21:46:04 +00001258 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 unsigned LastVal = ~0U;
1260 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1261 CCValAssign &VA = ArgLocs[i];
1262 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1263 // places.
1264 assert(VA.getValNo() != LastVal &&
1265 "Don't support value assigned to multiple locs yet");
1266 LastVal = VA.getValNo();
1267
1268 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001269 MVT RegVT = VA.getLocVT();
Devang Patelf3707e82009-01-05 17:31:22 +00001270 TargetRegisterClass *RC = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001271 if (RegVT == MVT::i32)
1272 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001273 else if (Is64Bit && RegVT == MVT::i64)
1274 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001275 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001276 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001277 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001278 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001279 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001280 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001281 else if (RegVT.isVector()) {
1282 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001283 if (!Is64Bit)
1284 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1285 else {
1286 // Darwin calling convention passes MMX values in either GPRs or
1287 // XMMs in x86-64. Other targets pass them in memory.
1288 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1289 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1290 RegVT = MVT::v2i64;
1291 } else {
1292 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1293 RegVT = MVT::i64;
1294 }
1295 }
1296 } else {
1297 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001299
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001300 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001301 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302
1303 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1304 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1305 // right size.
1306 if (VA.getLocInfo() == CCValAssign::SExt)
1307 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1308 DAG.getValueType(VA.getValVT()));
1309 else if (VA.getLocInfo() == CCValAssign::ZExt)
1310 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1311 DAG.getValueType(VA.getValVT()));
1312
1313 if (VA.getLocInfo() != CCValAssign::Full)
1314 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1315
Gordon Henriksen18ace102008-01-05 16:56:59 +00001316 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001317 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001318 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001319 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1320 else if (RC == X86::VR128RegisterClass) {
1321 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1322 DAG.getConstant(0, MVT::i64));
1323 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1324 }
1325 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001326
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001327 ArgValues.push_back(ArgValue);
1328 } else {
1329 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001330 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001331 }
1332 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001333
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001334 // The x86-64 ABI for returning structs by value requires that we copy
1335 // the sret argument into %rax for the return. Save the argument into
1336 // a virtual register so that we can access it from the return points.
1337 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1338 MachineFunction &MF = DAG.getMachineFunction();
1339 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1340 unsigned Reg = FuncInfo->getSRetReturnReg();
1341 if (!Reg) {
1342 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1343 FuncInfo->setSRetReturnReg(Reg);
1344 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001345 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001346 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1347 }
1348
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001349 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001350 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001351 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001352 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353
1354 // If the function takes variable number of arguments, make a frame index for
1355 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001356 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001357 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1358 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1359 }
1360 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001361 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1362
1363 // FIXME: We should really autogenerate these arrays
1364 static const unsigned GPR64ArgRegsWin64[] = {
1365 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001366 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001367 static const unsigned XMMArgRegsWin64[] = {
1368 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1369 };
1370 static const unsigned GPR64ArgRegs64Bit[] = {
1371 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1372 };
1373 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001374 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1375 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1376 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001377 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1378
1379 if (IsWin64) {
1380 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1381 GPR64ArgRegs = GPR64ArgRegsWin64;
1382 XMMArgRegs = XMMArgRegsWin64;
1383 } else {
1384 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1385 GPR64ArgRegs = GPR64ArgRegs64Bit;
1386 XMMArgRegs = XMMArgRegs64Bit;
1387 }
1388 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1389 TotalNumIntRegs);
1390 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1391 TotalNumXMMRegs);
1392
Edwin Törökaf8e1332009-02-01 18:15:56 +00001393 assert((Subtarget->hasSSE1() || !NumXMMRegs) &&
1394 "SSE register cannot be used when SSE is disabled!");
1395 if (!Subtarget->hasSSE1()) {
1396 // Kernel mode asks for SSE to be disabled, so don't push them
1397 // on the stack.
1398 TotalNumXMMRegs = 0;
1399 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001400 // For X86-64, if there are vararg parameters that are passed via
1401 // registers, then we must store them to their spots on the stack so they
1402 // may be loaded by deferencing the result of va_next.
1403 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001404 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1405 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1406 TotalNumXMMRegs * 16, 16);
1407
Gordon Henriksen18ace102008-01-05 16:56:59 +00001408 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001409 SmallVector<SDValue, 8> MemOps;
1410 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1411 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001412 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001413 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001414 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1415 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001416 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1417 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001418 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001419 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001420 MemOps.push_back(Store);
1421 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001422 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001423 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001424
Gordon Henriksen18ace102008-01-05 16:56:59 +00001425 // Now store the XMM (fp + vector) parameter registers.
1426 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001427 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001428 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001429 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1430 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001431 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1432 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001433 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001434 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001435 MemOps.push_back(Store);
1436 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001437 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001438 }
1439 if (!MemOps.empty())
1440 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1441 &MemOps[0], MemOps.size());
1442 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001443 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001444
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001445 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001446
Gordon Henriksen18ace102008-01-05 16:56:59 +00001447 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001448 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001449 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001450 BytesCallerReserves = 0;
1451 } else {
1452 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001453 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001454 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001455 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001456 BytesCallerReserves = StackSize;
1457 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001458
Gordon Henriksen18ace102008-01-05 16:56:59 +00001459 if (!Is64Bit) {
1460 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1461 if (CC == CallingConv::X86_FastCall)
1462 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1463 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001464
Anton Korobeynikove844e472007-08-15 17:12:32 +00001465 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001466
1467 // Return the new list of results.
Duncan Sands42d7bb82008-12-01 11:41:29 +00001468 return DAG.getNode(ISD::MERGE_VALUES, Op.getNode()->getVTList(),
1469 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001470}
1471
Dan Gohman8181bd12008-07-27 21:46:04 +00001472SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001473X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001474 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001475 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001476 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001477 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001478 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001479 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001480 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001481 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001482 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001483 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001484 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001485 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001486}
1487
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001488/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001489/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001490SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001491X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001492 SDValue &OutRetAddr,
1493 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001494 bool IsTailCall,
1495 bool Is64Bit,
1496 int FPDiff) {
1497 if (!IsTailCall || FPDiff==0) return Chain;
1498
1499 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001500 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001501 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001502
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001503 // Load the "old" Return address.
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001504 OutRetAddr = DAG.getLoad(VT, Chain, OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001505 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001506}
1507
1508/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1509/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001510static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001511EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001512 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001513 bool Is64Bit, int FPDiff) {
1514 // Store the return address to the appropriate stack slot.
1515 if (!FPDiff) return Chain;
1516 // Calculate the new stack slot for the return address.
1517 int SlotSize = Is64Bit ? 8 : 4;
1518 int NewReturnAddrFI =
1519 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001520 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001521 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001522 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001523 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001524 return Chain;
1525}
1526
Dan Gohman8181bd12008-07-27 21:46:04 +00001527SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001528 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001529 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1530 SDValue Chain = TheCall->getChain();
1531 unsigned CC = TheCall->getCallingConv();
1532 bool isVarArg = TheCall->isVarArg();
1533 bool IsTailCall = TheCall->isTailCall() &&
1534 CC == CallingConv::Fast && PerformTailCallOpt;
1535 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001536 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001537 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001538
1539 assert(!(isVarArg && CC == CallingConv::Fast) &&
1540 "Var args not supported with calling convention fastcc");
1541
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001542 // Analyze operands of the call, assigning locations to each operand.
1543 SmallVector<CCValAssign, 16> ArgLocs;
1544 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001545 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001546
1547 // Get a count of how many bytes are to be pushed on the stack.
1548 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001549 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001550 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001551
Gordon Henriksen18ace102008-01-05 16:56:59 +00001552 int FPDiff = 0;
1553 if (IsTailCall) {
1554 // Lower arguments at fp - stackoffset + fpdiff.
1555 unsigned NumBytesCallerPushed =
1556 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1557 FPDiff = NumBytesCallerPushed - NumBytes;
1558
1559 // Set the delta of movement of the returnaddr stackslot.
1560 // But only set if delta is greater than previous delta.
1561 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1562 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1563 }
1564
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001565 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001566
Dan Gohman8181bd12008-07-27 21:46:04 +00001567 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001568 // Load return adress for tail calls.
1569 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1570 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001571
Dan Gohman8181bd12008-07-27 21:46:04 +00001572 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1573 SmallVector<SDValue, 8> MemOpChains;
1574 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001575
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001576 // Walk the register/memloc assignments, inserting copies/loads. In the case
1577 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001578 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1579 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001580 SDValue Arg = TheCall->getArg(i);
1581 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1582 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001583
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001584 // Promote the value if needed.
1585 switch (VA.getLocInfo()) {
1586 default: assert(0 && "Unknown loc info!");
1587 case CCValAssign::Full: break;
1588 case CCValAssign::SExt:
1589 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1590 break;
1591 case CCValAssign::ZExt:
1592 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1593 break;
1594 case CCValAssign::AExt:
1595 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1596 break;
1597 }
1598
1599 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001600 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001601 MVT RegVT = VA.getLocVT();
1602 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001603 switch (VA.getLocReg()) {
1604 default:
1605 break;
1606 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1607 case X86::R8: {
1608 // Special case: passing MMX values in GPR registers.
1609 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1610 break;
1611 }
1612 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1613 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1614 // Special case: passing MMX values in XMM registers.
1615 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1616 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1617 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1618 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1619 getMOVLMask(2, DAG));
1620 break;
1621 }
1622 }
1623 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001624 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1625 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001626 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001627 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001628 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001629 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1630
Dan Gohman705e3f72008-09-13 01:54:27 +00001631 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1632 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001633 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001634 }
1635 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001636
1637 if (!MemOpChains.empty())
1638 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1639 &MemOpChains[0], MemOpChains.size());
1640
1641 // Build a sequence of copy-to-reg nodes chained together with token chain
1642 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001643 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001644 // Tail call byval lowering might overwrite argument registers so in case of
1645 // tail call optimization the copies to registers are lowered later.
1646 if (!IsTailCall)
1647 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1648 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1649 InFlag);
1650 InFlag = Chain.getValue(1);
1651 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001652
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001653 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001654 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001655 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1656 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1657 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1658 InFlag);
1659 InFlag = Chain.getValue(1);
1660 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001661 // If we are tail calling and generating PIC/GOT style code load the address
1662 // of the callee into ecx. The value in ecx is used as target of the tail
1663 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1664 // calls on PIC/GOT architectures. Normally we would just put the address of
1665 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1666 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001667 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001668 // Note: The actual moving to ecx is done further down.
1669 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001670 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001671 !G->getGlobal()->hasProtectedVisibility())
1672 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001673 else if (isa<ExternalSymbolSDNode>(Callee))
1674 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001675 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001676
Gordon Henriksen18ace102008-01-05 16:56:59 +00001677 if (Is64Bit && isVarArg) {
1678 // From AMD64 ABI document:
1679 // For calls that may call functions that use varargs or stdargs
1680 // (prototype-less calls or calls to functions containing ellipsis (...) in
1681 // the declaration) %al is used as hidden argument to specify the number
1682 // of SSE registers used. The contents of %al do not need to match exactly
1683 // the number of registers, but must be an ubound on the number of SSE
1684 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001685
1686 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001687 // Count the number of XMM registers allocated.
1688 static const unsigned XMMArgRegs[] = {
1689 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1690 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1691 };
1692 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Edwin Törökaf8e1332009-02-01 18:15:56 +00001693 assert((Subtarget->hasSSE1() || !NumXMMRegs)
1694 && "SSE registers cannot be used when SSE is disabled");
Gordon Henriksen18ace102008-01-05 16:56:59 +00001695
1696 Chain = DAG.getCopyToReg(Chain, X86::AL,
1697 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1698 InFlag = Chain.getValue(1);
1699 }
1700
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001701
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001702 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001703 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001704 SmallVector<SDValue, 8> MemOpChains2;
1705 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001706 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001707 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001708 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001709 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1710 CCValAssign &VA = ArgLocs[i];
1711 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001712 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001713 SDValue Arg = TheCall->getArg(i);
1714 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001715 // Create frame index.
1716 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001717 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001718 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001719 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001720
Duncan Sandsc93fae32008-03-21 09:14:45 +00001721 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001722 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001723 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001724 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001725 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1726 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1727
1728 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001729 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001730 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001731 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001732 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001733 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001734 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001735 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001736 }
1737 }
1738
1739 if (!MemOpChains2.empty())
1740 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001741 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001742
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001743 // Copy arguments to their registers.
1744 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1745 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1746 InFlag);
1747 InFlag = Chain.getValue(1);
1748 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001749 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001750
Gordon Henriksen18ace102008-01-05 16:56:59 +00001751 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001752 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1753 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001754 }
1755
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001756 // If the callee is a GlobalAddress node (quite common, every direct call is)
1757 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1758 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1759 // We should use extra load for direct calls to dllimported functions in
1760 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001761 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1762 getTargetMachine(), true))
Dan Gohman36322c72008-10-18 02:06:02 +00001763 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1764 G->getOffset());
Bill Wendlingfef06052008-09-16 21:48:12 +00001765 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1766 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001767 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001768 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001769
1770 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001771 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001772 Callee,InFlag);
1773 Callee = DAG.getRegister(Opc, getPointerTy());
1774 // Add register as live out.
1775 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001776 }
1777
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001778 // Returns a chain & a flag for retval copy to use.
1779 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001780 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001781
1782 if (IsTailCall) {
1783 Ops.push_back(Chain);
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001784 Ops.push_back(DAG.getIntPtrConstant(NumBytes, true));
1785 Ops.push_back(DAG.getIntPtrConstant(0, true));
Gabor Greif1c80d112008-08-28 21:40:38 +00001786 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001787 Ops.push_back(InFlag);
1788 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1789 InFlag = Chain.getValue(1);
1790
1791 // Returns a chain & a flag for retval copy to use.
1792 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1793 Ops.clear();
1794 }
1795
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001796 Ops.push_back(Chain);
1797 Ops.push_back(Callee);
1798
Gordon Henriksen18ace102008-01-05 16:56:59 +00001799 if (IsTailCall)
1800 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001801
Gordon Henriksen18ace102008-01-05 16:56:59 +00001802 // Add argument registers to the end of the list so that they are known live
1803 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001804 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1805 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1806 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001807
Evan Cheng8ba45e62008-03-18 23:36:35 +00001808 // Add an implicit use GOT pointer in EBX.
1809 if (!IsTailCall && !Is64Bit &&
1810 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1811 Subtarget->isPICStyleGOT())
1812 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1813
1814 // Add an implicit use of AL for x86 vararg functions.
1815 if (Is64Bit && isVarArg)
1816 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1817
Gabor Greif1c80d112008-08-28 21:40:38 +00001818 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001819 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001820
Gordon Henriksen18ace102008-01-05 16:56:59 +00001821 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001822 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001823 "Flag must be set. Depend on flag being set in LowerRET");
1824 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman705e3f72008-09-13 01:54:27 +00001825 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001826
Gabor Greif1c80d112008-08-28 21:40:38 +00001827 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001828 }
1829
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001830 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001831 InFlag = Chain.getValue(1);
1832
1833 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001834 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001835 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001836 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001837 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001838 // If this is is a call to a struct-return function, the callee
1839 // pops the hidden struct pointer, so we have to push it back.
1840 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001841 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001842 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001843 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001844
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001845 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001846 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001847 DAG.getIntPtrConstant(NumBytes, true),
1848 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1849 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001850 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001851 InFlag = Chain.getValue(1);
1852
1853 // Handle result values, copying them out of physregs into vregs that we
1854 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001855 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001856 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001857}
1858
1859
1860//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001861// Fast Calling Convention (tail call) implementation
1862//===----------------------------------------------------------------------===//
1863
1864// Like std call, callee cleans arguments, convention except that ECX is
1865// reserved for storing the tail called function address. Only 2 registers are
1866// free for argument passing (inreg). Tail call optimization is performed
1867// provided:
1868// * tailcallopt is enabled
1869// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001870// On X86_64 architecture with GOT-style position independent code only local
1871// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001872// To keep the stack aligned according to platform abi the function
1873// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1874// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001875// If a tail called function callee has more arguments than the caller the
1876// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001877// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001878// original REtADDR, but before the saved framepointer or the spilled registers
1879// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1880// stack layout:
1881// arg1
1882// arg2
1883// RETADDR
1884// [ new RETADDR
1885// move area ]
1886// (possible EBP)
1887// ESI
1888// EDI
1889// local1 ..
1890
1891/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1892/// for a 16 byte align requirement.
1893unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1894 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001895 MachineFunction &MF = DAG.getMachineFunction();
1896 const TargetMachine &TM = MF.getTarget();
1897 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1898 unsigned StackAlignment = TFI.getStackAlignment();
1899 uint64_t AlignMask = StackAlignment - 1;
1900 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001901 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001902 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1903 // Number smaller than 12 so just add the difference.
1904 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1905 } else {
1906 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1907 Offset = ((~AlignMask) & Offset) + StackAlignment +
1908 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001909 }
Evan Chengded8f902008-09-07 09:07:23 +00001910 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001911}
1912
1913/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001914/// following the call is a return. A function is eligible if caller/callee
1915/// calling conventions match, currently only fastcc supports tail calls, and
1916/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001917bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001918 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001919 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001920 if (!PerformTailCallOpt)
1921 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001922
Dan Gohman705e3f72008-09-13 01:54:27 +00001923 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001924 MachineFunction &MF = DAG.getMachineFunction();
1925 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001926 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001927 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001928 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001929 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001930 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001931 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001932 return true;
1933
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001934 // Can only do local tail calls (in same module, hidden or protected) on
1935 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001936 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1937 return G->getGlobal()->hasHiddenVisibility()
1938 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001939 }
1940 }
Evan Chenge7a87392007-11-02 01:26:22 +00001941
1942 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001943}
1944
Dan Gohmanca4857a2008-09-03 23:12:08 +00001945FastISel *
1946X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001947 MachineModuleInfo *mmo,
Devang Patelfcf1c752009-01-13 00:35:13 +00001948 DwarfWriter *dw,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001949 DenseMap<const Value *, unsigned> &vm,
1950 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001951 MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00001952 DenseMap<const AllocaInst *, int> &am
1953#ifndef NDEBUG
1954 , SmallSet<Instruction*, 8> &cil
1955#endif
1956 ) {
Devang Patelfcf1c752009-01-13 00:35:13 +00001957 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohman9dd43582008-10-14 23:54:11 +00001958#ifndef NDEBUG
1959 , cil
1960#endif
1961 );
Dan Gohman97805ee2008-08-19 21:32:53 +00001962}
1963
1964
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001965//===----------------------------------------------------------------------===//
1966// Other Lowering Hooks
1967//===----------------------------------------------------------------------===//
1968
1969
Dan Gohman8181bd12008-07-27 21:46:04 +00001970SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001971 MachineFunction &MF = DAG.getMachineFunction();
1972 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1973 int ReturnAddrIndex = FuncInfo->getRAIndex();
1974
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001975 if (ReturnAddrIndex == 0) {
1976 // Set up a frame object for the return address.
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001977 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001978 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001979 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001980 }
1981
1982 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1983}
1984
1985
Chris Lattnerebb91142008-12-24 23:53:05 +00001986/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
1987/// specific condition code, returning the condition code and the LHS/RHS of the
1988/// comparison to make.
1989static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1990 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001991 if (!isFP) {
1992 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1993 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1994 // X > -1 -> X == 0, jump !sign.
1995 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00001996 return X86::COND_NS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001997 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1998 // X < 0 -> X == 0, jump on sign.
Chris Lattnerebb91142008-12-24 23:53:05 +00001999 return X86::COND_S;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002000 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00002001 // X < 1 -> X <= 0
2002 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00002003 return X86::COND_LE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002004 }
2005 }
2006
2007 switch (SetCCOpcode) {
Chris Lattnerb8397512008-12-23 23:42:27 +00002008 default: assert(0 && "Invalid integer condition!");
Chris Lattnerebb91142008-12-24 23:53:05 +00002009 case ISD::SETEQ: return X86::COND_E;
2010 case ISD::SETGT: return X86::COND_G;
2011 case ISD::SETGE: return X86::COND_GE;
2012 case ISD::SETLT: return X86::COND_L;
2013 case ISD::SETLE: return X86::COND_LE;
2014 case ISD::SETNE: return X86::COND_NE;
2015 case ISD::SETULT: return X86::COND_B;
2016 case ISD::SETUGT: return X86::COND_A;
2017 case ISD::SETULE: return X86::COND_BE;
2018 case ISD::SETUGE: return X86::COND_AE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002019 }
Chris Lattnerb8397512008-12-23 23:42:27 +00002020 }
2021
2022 // First determine if it is required or is profitable to flip the operands.
Duncan Sandsc2a04622008-10-24 13:03:10 +00002023
Chris Lattnerb8397512008-12-23 23:42:27 +00002024 // If LHS is a foldable load, but RHS is not, flip the condition.
2025 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2026 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2027 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2028 std::swap(LHS, RHS);
Evan Chengfc937c92008-08-28 23:48:31 +00002029 }
2030
Chris Lattnerb8397512008-12-23 23:42:27 +00002031 switch (SetCCOpcode) {
2032 default: break;
2033 case ISD::SETOLT:
2034 case ISD::SETOLE:
2035 case ISD::SETUGT:
2036 case ISD::SETUGE:
2037 std::swap(LHS, RHS);
2038 break;
2039 }
2040
2041 // On a floating point condition, the flags are set as follows:
2042 // ZF PF CF op
2043 // 0 | 0 | 0 | X > Y
2044 // 0 | 0 | 1 | X < Y
2045 // 1 | 0 | 0 | X == Y
2046 // 1 | 1 | 1 | unordered
2047 switch (SetCCOpcode) {
Chris Lattnerebb91142008-12-24 23:53:05 +00002048 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattnerb8397512008-12-23 23:42:27 +00002049 case ISD::SETUEQ:
Chris Lattnerebb91142008-12-24 23:53:05 +00002050 case ISD::SETEQ: return X86::COND_E;
Chris Lattnerb8397512008-12-23 23:42:27 +00002051 case ISD::SETOLT: // flipped
2052 case ISD::SETOGT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002053 case ISD::SETGT: return X86::COND_A;
Chris Lattnerb8397512008-12-23 23:42:27 +00002054 case ISD::SETOLE: // flipped
2055 case ISD::SETOGE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002056 case ISD::SETGE: return X86::COND_AE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002057 case ISD::SETUGT: // flipped
2058 case ISD::SETULT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002059 case ISD::SETLT: return X86::COND_B;
Chris Lattnerb8397512008-12-23 23:42:27 +00002060 case ISD::SETUGE: // flipped
2061 case ISD::SETULE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002062 case ISD::SETLE: return X86::COND_BE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002063 case ISD::SETONE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002064 case ISD::SETNE: return X86::COND_NE;
2065 case ISD::SETUO: return X86::COND_P;
2066 case ISD::SETO: return X86::COND_NP;
Chris Lattnerb8397512008-12-23 23:42:27 +00002067 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002068}
2069
2070/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2071/// code. Current x86 isa includes the following FP cmov instructions:
2072/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2073static bool hasFPCMov(unsigned X86CC) {
2074 switch (X86CC) {
2075 default:
2076 return false;
2077 case X86::COND_B:
2078 case X86::COND_BE:
2079 case X86::COND_E:
2080 case X86::COND_P:
2081 case X86::COND_A:
2082 case X86::COND_AE:
2083 case X86::COND_NE:
2084 case X86::COND_NP:
2085 return true;
2086 }
2087}
2088
2089/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2090/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002091static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002092 if (Op.getOpcode() == ISD::UNDEF)
2093 return true;
2094
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002095 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002096 return (Val >= Low && Val < Hi);
2097}
2098
2099/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2100/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002101static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002102 if (Op.getOpcode() == ISD::UNDEF)
2103 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002104 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002105}
2106
2107/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2108/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2109bool X86::isPSHUFDMask(SDNode *N) {
2110 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2111
Dan Gohman7dc19012007-08-02 21:17:01 +00002112 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002113 return false;
2114
2115 // Check if the value doesn't reference the second vector.
2116 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002117 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002118 if (Arg.getOpcode() == ISD::UNDEF) continue;
2119 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002120 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002121 return false;
2122 }
2123
2124 return true;
2125}
2126
2127/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2128/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2129bool X86::isPSHUFHWMask(SDNode *N) {
2130 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2131
2132 if (N->getNumOperands() != 8)
2133 return false;
2134
2135 // Lower quadword copied in order.
2136 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002137 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002138 if (Arg.getOpcode() == ISD::UNDEF) continue;
2139 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002140 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002141 return false;
2142 }
2143
2144 // Upper quadword shuffled.
2145 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002146 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002147 if (Arg.getOpcode() == ISD::UNDEF) continue;
2148 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002149 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002150 if (Val < 4 || Val > 7)
2151 return false;
2152 }
2153
2154 return true;
2155}
2156
2157/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2158/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2159bool X86::isPSHUFLWMask(SDNode *N) {
2160 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2161
2162 if (N->getNumOperands() != 8)
2163 return false;
2164
2165 // Upper quadword copied in order.
2166 for (unsigned i = 4; i != 8; ++i)
2167 if (!isUndefOrEqual(N->getOperand(i), i))
2168 return false;
2169
2170 // Lower quadword shuffled.
2171 for (unsigned i = 0; i != 4; ++i)
2172 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2173 return false;
2174
2175 return true;
2176}
2177
2178/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2179/// specifies a shuffle of elements that is suitable for input to SHUFP*.
djgc2517d32009-01-26 04:35:06 +00002180template<class SDOperand>
2181static bool isSHUFPMask(SDOperand *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.
djgc2517d32009-01-26 04:35:06 +00002204template<class SDOperand>
2205static bool isCommutedSHUFP(SDOperand *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002206 if (NumOps != 2 && NumOps != 4) return false;
2207
2208 unsigned Half = NumOps / 2;
2209 for (unsigned i = 0; i < Half; ++i)
2210 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2211 return false;
2212 for (unsigned i = Half; i < NumOps; ++i)
2213 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2214 return false;
2215 return true;
2216}
2217
2218static bool isCommutedSHUFP(SDNode *N) {
2219 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2220 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2221}
2222
2223/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2224/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2225bool X86::isMOVHLPSMask(SDNode *N) {
2226 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2227
2228 if (N->getNumOperands() != 4)
2229 return false;
2230
2231 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2232 return isUndefOrEqual(N->getOperand(0), 6) &&
2233 isUndefOrEqual(N->getOperand(1), 7) &&
2234 isUndefOrEqual(N->getOperand(2), 2) &&
2235 isUndefOrEqual(N->getOperand(3), 3);
2236}
2237
2238/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2239/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2240/// <2, 3, 2, 3>
2241bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2242 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2243
2244 if (N->getNumOperands() != 4)
2245 return false;
2246
2247 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2248 return isUndefOrEqual(N->getOperand(0), 2) &&
2249 isUndefOrEqual(N->getOperand(1), 3) &&
2250 isUndefOrEqual(N->getOperand(2), 2) &&
2251 isUndefOrEqual(N->getOperand(3), 3);
2252}
2253
2254/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2255/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2256bool X86::isMOVLPMask(SDNode *N) {
2257 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2258
2259 unsigned NumElems = N->getNumOperands();
2260 if (NumElems != 2 && NumElems != 4)
2261 return false;
2262
2263 for (unsigned i = 0; i < NumElems/2; ++i)
2264 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2265 return false;
2266
2267 for (unsigned i = NumElems/2; i < NumElems; ++i)
2268 if (!isUndefOrEqual(N->getOperand(i), i))
2269 return false;
2270
2271 return true;
2272}
2273
2274/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2275/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2276/// and MOVLHPS.
2277bool X86::isMOVHPMask(SDNode *N) {
2278 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2279
2280 unsigned NumElems = N->getNumOperands();
2281 if (NumElems != 2 && NumElems != 4)
2282 return false;
2283
2284 for (unsigned i = 0; i < NumElems/2; ++i)
2285 if (!isUndefOrEqual(N->getOperand(i), i))
2286 return false;
2287
2288 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002289 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002290 if (!isUndefOrEqual(Arg, i + NumElems))
2291 return false;
2292 }
2293
2294 return true;
2295}
2296
2297/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2298/// specifies a shuffle of elements that is suitable for input to UNPCKL.
djgc2517d32009-01-26 04:35:06 +00002299template<class SDOperand>
2300bool static isUNPCKLMask(SDOperand *Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002301 bool V2IsSplat = false) {
2302 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2303 return false;
2304
2305 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002306 SDValue BitI = Elts[i];
2307 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002308 if (!isUndefOrEqual(BitI, j))
2309 return false;
2310 if (V2IsSplat) {
2311 if (isUndefOrEqual(BitI1, NumElts))
2312 return false;
2313 } else {
2314 if (!isUndefOrEqual(BitI1, j + NumElts))
2315 return false;
2316 }
2317 }
2318
2319 return true;
2320}
2321
2322bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2323 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2324 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2325}
2326
2327/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2328/// specifies a shuffle of elements that is suitable for input to UNPCKH.
djgc2517d32009-01-26 04:35:06 +00002329template<class SDOperand>
2330bool static isUNPCKHMask(SDOperand *Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002331 bool V2IsSplat = false) {
2332 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2333 return false;
2334
2335 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002336 SDValue BitI = Elts[i];
2337 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002338 if (!isUndefOrEqual(BitI, j + NumElts/2))
2339 return false;
2340 if (V2IsSplat) {
2341 if (isUndefOrEqual(BitI1, NumElts))
2342 return false;
2343 } else {
2344 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2345 return false;
2346 }
2347 }
2348
2349 return true;
2350}
2351
2352bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2353 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2354 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2355}
2356
2357/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2358/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2359/// <0, 0, 1, 1>
2360bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2361 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2362
2363 unsigned NumElems = N->getNumOperands();
2364 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2365 return false;
2366
2367 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002368 SDValue BitI = N->getOperand(i);
2369 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002370
2371 if (!isUndefOrEqual(BitI, j))
2372 return false;
2373 if (!isUndefOrEqual(BitI1, j))
2374 return false;
2375 }
2376
2377 return true;
2378}
2379
2380/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2381/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2382/// <2, 2, 3, 3>
2383bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2384 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2385
2386 unsigned NumElems = N->getNumOperands();
2387 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2388 return false;
2389
2390 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002391 SDValue BitI = N->getOperand(i);
2392 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002393
2394 if (!isUndefOrEqual(BitI, j))
2395 return false;
2396 if (!isUndefOrEqual(BitI1, j))
2397 return false;
2398 }
2399
2400 return true;
2401}
2402
2403/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2404/// specifies a shuffle of elements that is suitable for input to MOVSS,
2405/// MOVSD, and MOVD, i.e. setting the lowest element.
djgc2517d32009-01-26 04:35:06 +00002406template<class SDOperand>
2407static bool isMOVLMask(SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002408 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002409 return false;
2410
2411 if (!isUndefOrEqual(Elts[0], NumElts))
2412 return false;
2413
2414 for (unsigned i = 1; i < NumElts; ++i) {
2415 if (!isUndefOrEqual(Elts[i], i))
2416 return false;
2417 }
2418
2419 return true;
2420}
2421
2422bool X86::isMOVLMask(SDNode *N) {
2423 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2424 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2425}
2426
2427/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2428/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2429/// element of vector 2 and the other elements to come from vector 1 in order.
djgc2517d32009-01-26 04:35:06 +00002430template<class SDOperand>
2431static bool isCommutedMOVL(SDOperand *Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002432 bool V2IsSplat = false,
2433 bool V2IsUndef = false) {
2434 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2435 return false;
2436
2437 if (!isUndefOrEqual(Ops[0], 0))
2438 return false;
2439
2440 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002441 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002442 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2443 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2444 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2445 return false;
2446 }
2447
2448 return true;
2449}
2450
2451static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2452 bool V2IsUndef = false) {
2453 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2454 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2455 V2IsSplat, V2IsUndef);
2456}
2457
2458/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2459/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2460bool X86::isMOVSHDUPMask(SDNode *N) {
2461 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2462
2463 if (N->getNumOperands() != 4)
2464 return false;
2465
2466 // Expect 1, 1, 3, 3
2467 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002468 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002469 if (Arg.getOpcode() == ISD::UNDEF) continue;
2470 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002471 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002472 if (Val != 1) return false;
2473 }
2474
2475 bool HasHi = false;
2476 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002477 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002478 if (Arg.getOpcode() == ISD::UNDEF) continue;
2479 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002480 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002481 if (Val != 3) return false;
2482 HasHi = true;
2483 }
2484
2485 // Don't use movshdup if it can be done with a shufps.
2486 return HasHi;
2487}
2488
2489/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2490/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2491bool X86::isMOVSLDUPMask(SDNode *N) {
2492 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2493
2494 if (N->getNumOperands() != 4)
2495 return false;
2496
2497 // Expect 0, 0, 2, 2
2498 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002499 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002500 if (Arg.getOpcode() == ISD::UNDEF) continue;
2501 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002502 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002503 if (Val != 0) return false;
2504 }
2505
2506 bool HasHi = false;
2507 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002508 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002509 if (Arg.getOpcode() == ISD::UNDEF) continue;
2510 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002511 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002512 if (Val != 2) return false;
2513 HasHi = true;
2514 }
2515
2516 // Don't use movshdup if it can be done with a shufps.
2517 return HasHi;
2518}
2519
2520/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2521/// specifies a identity operation on the LHS or RHS.
2522static bool isIdentityMask(SDNode *N, bool RHS = false) {
2523 unsigned NumElems = N->getNumOperands();
2524 for (unsigned i = 0; i < NumElems; ++i)
2525 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2526 return false;
2527 return true;
2528}
2529
2530/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2531/// a splat of a single element.
2532static bool isSplatMask(SDNode *N) {
2533 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2534
2535 // This is a splat operation if each element of the permute is the same, and
2536 // if the value doesn't reference the second vector.
2537 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002538 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002539 unsigned i = 0;
2540 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002541 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002542 if (isa<ConstantSDNode>(Elt)) {
2543 ElementBase = Elt;
2544 break;
2545 }
2546 }
2547
Gabor Greif1c80d112008-08-28 21:40:38 +00002548 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002549 return false;
2550
2551 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002552 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002553 if (Arg.getOpcode() == ISD::UNDEF) continue;
2554 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2555 if (Arg != ElementBase) return false;
2556 }
2557
2558 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002559 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002560}
2561
Mon P Wang532c9632008-12-23 04:03:27 +00002562/// getSplatMaskEltNo - Given a splat mask, return the index to the element
2563/// we want to splat.
2564static SDValue getSplatMaskEltNo(SDNode *N) {
2565 assert(isSplatMask(N) && "Not a splat mask");
2566 unsigned NumElems = N->getNumOperands();
2567 SDValue ElementBase;
2568 unsigned i = 0;
2569 for (; i != NumElems; ++i) {
2570 SDValue Elt = N->getOperand(i);
2571 if (isa<ConstantSDNode>(Elt))
2572 return Elt;
2573 }
2574 assert(0 && " No splat value found!");
2575 return SDValue();
2576}
2577
2578
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002579/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2580/// a splat of a single element and it's a 2 or 4 element mask.
2581bool X86::isSplatMask(SDNode *N) {
2582 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2583
2584 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2585 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2586 return false;
2587 return ::isSplatMask(N);
2588}
2589
2590/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2591/// specifies a splat of zero element.
2592bool X86::isSplatLoMask(SDNode *N) {
2593 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2594
2595 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2596 if (!isUndefOrEqual(N->getOperand(i), 0))
2597 return false;
2598 return true;
2599}
2600
Evan Chenga2497eb2008-09-25 20:50:48 +00002601/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2602/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2603bool X86::isMOVDDUPMask(SDNode *N) {
2604 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2605
2606 unsigned e = N->getNumOperands() / 2;
2607 for (unsigned i = 0; i < e; ++i)
2608 if (!isUndefOrEqual(N->getOperand(i), i))
2609 return false;
2610 for (unsigned i = 0; i < e; ++i)
2611 if (!isUndefOrEqual(N->getOperand(e+i), i))
2612 return false;
2613 return true;
2614}
2615
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002616/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2617/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2618/// instructions.
2619unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2620 unsigned NumOperands = N->getNumOperands();
2621 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2622 unsigned Mask = 0;
2623 for (unsigned i = 0; i < NumOperands; ++i) {
2624 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002625 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002626 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002627 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002628 if (Val >= NumOperands) Val -= NumOperands;
2629 Mask |= Val;
2630 if (i != NumOperands - 1)
2631 Mask <<= Shift;
2632 }
2633
2634 return Mask;
2635}
2636
2637/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2638/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2639/// instructions.
2640unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2641 unsigned Mask = 0;
2642 // 8 nodes, but we only care about the last 4.
2643 for (unsigned i = 7; i >= 4; --i) {
2644 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002645 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002646 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002647 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002648 Mask |= (Val - 4);
2649 if (i != 4)
2650 Mask <<= 2;
2651 }
2652
2653 return Mask;
2654}
2655
2656/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2657/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2658/// instructions.
2659unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2660 unsigned Mask = 0;
2661 // 8 nodes, but we only care about the first 4.
2662 for (int i = 3; i >= 0; --i) {
2663 unsigned Val = 0;
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)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002666 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002667 Mask |= Val;
2668 if (i != 0)
2669 Mask <<= 2;
2670 }
2671
2672 return Mask;
2673}
2674
2675/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2676/// specifies a 8 element shuffle that can be broken into a pair of
2677/// PSHUFHW and PSHUFLW.
2678static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2679 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2680
2681 if (N->getNumOperands() != 8)
2682 return false;
2683
2684 // Lower quadword shuffled.
2685 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002686 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002687 if (Arg.getOpcode() == ISD::UNDEF) continue;
2688 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002689 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002690 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002691 return false;
2692 }
2693
2694 // Upper quadword shuffled.
2695 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002696 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002697 if (Arg.getOpcode() == ISD::UNDEF) continue;
2698 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002699 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002700 if (Val < 4 || Val > 7)
2701 return false;
2702 }
2703
2704 return true;
2705}
2706
Chris Lattnere6aa3862007-11-25 00:24:49 +00002707/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002708/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002709static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2710 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002711 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002712 MVT VT = Op.getValueType();
2713 MVT MaskVT = Mask.getValueType();
2714 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002715 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002716 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002717
2718 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002719 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002720 if (Arg.getOpcode() == ISD::UNDEF) {
2721 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2722 continue;
2723 }
2724 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002725 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002726 if (Val < NumElems)
2727 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2728 else
2729 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2730 }
2731
2732 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002733 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002734 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2735}
2736
Evan Chenga6769df2007-12-07 21:30:01 +00002737/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2738/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002739static
Dan Gohman8181bd12008-07-27 21:46:04 +00002740SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002741 MVT MaskVT = Mask.getValueType();
2742 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002743 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002744 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002745 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002746 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002747 if (Arg.getOpcode() == ISD::UNDEF) {
2748 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2749 continue;
2750 }
2751 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002752 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002753 if (Val < NumElems)
2754 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2755 else
2756 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2757 }
2758 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2759}
2760
2761
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002762/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2763/// match movhlps. The lower half elements should come from upper half of
2764/// V1 (and in order), and the upper half elements should come from the upper
2765/// half of V2 (and in order).
2766static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2767 unsigned NumElems = Mask->getNumOperands();
2768 if (NumElems != 4)
2769 return false;
2770 for (unsigned i = 0, e = 2; i != e; ++i)
2771 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2772 return false;
2773 for (unsigned i = 2; i != 4; ++i)
2774 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2775 return false;
2776 return true;
2777}
2778
2779/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002780/// is promoted to a vector. It also returns the LoadSDNode by reference if
2781/// required.
2782static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002783 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2784 return false;
2785 N = N->getOperand(0).getNode();
2786 if (!ISD::isNON_EXTLoad(N))
2787 return false;
2788 if (LD)
2789 *LD = cast<LoadSDNode>(N);
2790 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002791}
2792
2793/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2794/// match movlp{s|d}. The lower half elements should come from lower half of
2795/// V1 (and in order), and the upper half elements should come from the upper
2796/// half of V2 (and in order). And since V1 will become the source of the
2797/// MOVLP, it must be either a vector load or a scalar load to vector.
2798static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2799 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2800 return false;
2801 // Is V2 is a vector load, don't do this transformation. We will try to use
2802 // load folding shufps op.
2803 if (ISD::isNON_EXTLoad(V2))
2804 return false;
2805
2806 unsigned NumElems = Mask->getNumOperands();
2807 if (NumElems != 2 && NumElems != 4)
2808 return false;
2809 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2810 if (!isUndefOrEqual(Mask->getOperand(i), i))
2811 return false;
2812 for (unsigned i = NumElems/2; i != NumElems; ++i)
2813 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2814 return false;
2815 return true;
2816}
2817
2818/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2819/// all the same.
2820static bool isSplatVector(SDNode *N) {
2821 if (N->getOpcode() != ISD::BUILD_VECTOR)
2822 return false;
2823
Dan Gohman8181bd12008-07-27 21:46:04 +00002824 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002825 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2826 if (N->getOperand(i) != SplatValue)
2827 return false;
2828 return true;
2829}
2830
2831/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2832/// to an undef.
2833static bool isUndefShuffle(SDNode *N) {
2834 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2835 return false;
2836
Dan Gohman8181bd12008-07-27 21:46:04 +00002837 SDValue V1 = N->getOperand(0);
2838 SDValue V2 = N->getOperand(1);
2839 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002840 unsigned NumElems = Mask.getNumOperands();
2841 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002842 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002843 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002844 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002845 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2846 return false;
2847 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2848 return false;
2849 }
2850 }
2851 return true;
2852}
2853
2854/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2855/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002856static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002857 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002858 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002859 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002860 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002861}
2862
2863/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2864/// to an zero vector.
2865static bool isZeroShuffle(SDNode *N) {
2866 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2867 return false;
2868
Dan Gohman8181bd12008-07-27 21:46:04 +00002869 SDValue V1 = N->getOperand(0);
2870 SDValue V2 = N->getOperand(1);
2871 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002872 unsigned NumElems = Mask.getNumOperands();
2873 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002874 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002875 if (Arg.getOpcode() == ISD::UNDEF)
2876 continue;
2877
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002878 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002879 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002880 unsigned Opc = V1.getNode()->getOpcode();
2881 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002882 continue;
2883 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002884 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002885 return false;
2886 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002887 unsigned Opc = V2.getNode()->getOpcode();
2888 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002889 continue;
2890 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002891 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002892 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002893 }
2894 }
2895 return true;
2896}
2897
2898/// getZeroVector - Returns a vector of specified type with all zero elements.
2899///
Dan Gohman8181bd12008-07-27 21:46:04 +00002900static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002901 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002902
2903 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2904 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002905 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002906 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002907 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002908 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002909 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002910 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002911 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002912 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002913 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002914 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2915 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002916 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002917}
2918
Chris Lattnere6aa3862007-11-25 00:24:49 +00002919/// getOnesVector - Returns a vector of specified type with all bits set.
2920///
Dan Gohman8181bd12008-07-27 21:46:04 +00002921static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002922 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002923
2924 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2925 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002926 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2927 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002928 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002929 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2930 else // SSE
2931 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2932 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2933}
2934
2935
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002936/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2937/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002938static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002939 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2940
2941 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002942 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943 unsigned NumElems = Mask.getNumOperands();
2944 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002945 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002946 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002947 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002948 if (Val > NumElems) {
2949 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2950 Changed = true;
2951 }
2952 }
2953 MaskVec.push_back(Arg);
2954 }
2955
2956 if (Changed)
2957 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2958 &MaskVec[0], MaskVec.size());
2959 return Mask;
2960}
2961
2962/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2963/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002964static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002965 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2966 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002967
Dan Gohman8181bd12008-07-27 21:46:04 +00002968 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002969 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2970 for (unsigned i = 1; i != NumElems; ++i)
2971 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2972 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2973}
2974
2975/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2976/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002977static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002978 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2979 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002980 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002981 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2982 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2983 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2984 }
2985 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2986}
2987
2988/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2989/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002990static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002991 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2992 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002993 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002994 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002995 for (unsigned i = 0; i != Half; ++i) {
2996 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2997 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2998 }
2999 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3000}
3001
Chris Lattner2d91b962008-03-09 01:05:04 +00003002/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
3003/// element #0 of a vector with the specified index, leaving the rest of the
3004/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00003005static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00003006 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003007 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3008 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003009 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00003010 // Element #0 of the result gets the elt we are replacing.
3011 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
3012 for (unsigned i = 1; i != NumElems; ++i)
3013 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
3014 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3015}
3016
Evan Chengbf8b2c52008-04-05 00:30:36 +00003017/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00003018static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003019 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
3020 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003021 if (PVT == VT)
3022 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00003023 SDValue V1 = Op.getOperand(0);
3024 SDValue Mask = Op.getOperand(2);
Mon P Wang532c9632008-12-23 04:03:27 +00003025 unsigned MaskNumElems = Mask.getNumOperands();
3026 unsigned NumElems = MaskNumElems;
Evan Chengbf8b2c52008-04-05 00:30:36 +00003027 // Special handling of v4f32 -> v4i32.
3028 if (VT != MVT::v4f32) {
Mon P Wang532c9632008-12-23 04:03:27 +00003029 // Find which element we want to splat.
3030 SDNode* EltNoNode = getSplatMaskEltNo(Mask.getNode()).getNode();
3031 unsigned EltNo = cast<ConstantSDNode>(EltNoNode)->getZExtValue();
3032 // unpack elements to the correct location
Evan Chengbf8b2c52008-04-05 00:30:36 +00003033 while (NumElems > 4) {
Mon P Wang532c9632008-12-23 04:03:27 +00003034 if (EltNo < NumElems/2) {
3035 Mask = getUnpacklMask(MaskNumElems, DAG);
3036 } else {
3037 Mask = getUnpackhMask(MaskNumElems, DAG);
3038 EltNo -= NumElems/2;
3039 }
Evan Chengbf8b2c52008-04-05 00:30:36 +00003040 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
3041 NumElems >>= 1;
3042 }
Mon P Wang532c9632008-12-23 04:03:27 +00003043 SDValue Cst = DAG.getConstant(EltNo, MVT::i32);
3044 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003045 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003046
Evan Chengbf8b2c52008-04-05 00:30:36 +00003047 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00003048 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00003049 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003050 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3051}
3052
Evan Chenga2497eb2008-09-25 20:50:48 +00003053/// isVectorLoad - Returns true if the node is a vector load, a scalar
3054/// load that's promoted to vector, or a load bitcasted.
3055static bool isVectorLoad(SDValue Op) {
3056 assert(Op.getValueType().isVector() && "Expected a vector type");
3057 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
3058 Op.getOpcode() == ISD::BIT_CONVERT) {
3059 return isa<LoadSDNode>(Op.getOperand(0));
3060 }
3061 return isa<LoadSDNode>(Op);
3062}
3063
3064
3065/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3066///
3067static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3068 SelectionDAG &DAG, bool HasSSE3) {
3069 // If we have sse3 and shuffle has more than one use or input is a load, then
3070 // use movddup. Otherwise, use movlhps.
3071 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3072 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3073 MVT VT = Op.getValueType();
3074 if (VT == PVT)
3075 return Op;
3076 unsigned NumElems = PVT.getVectorNumElements();
3077 if (NumElems == 2) {
3078 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3079 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
3080 } else {
3081 assert(NumElems == 4);
3082 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3083 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
3084 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst0, Cst1, Cst0, Cst1);
3085 }
3086
3087 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
3088 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
3089 DAG.getNode(ISD::UNDEF, PVT), Mask);
3090 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3091}
3092
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003093/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003094/// vector of zero or undef vector. This produces a shuffle where the low
3095/// element of V2 is swizzled into the zero/undef vector, landing at element
3096/// 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 +00003097static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003098 bool isZero, bool HasSSE2,
3099 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003100 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003101 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00003102 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003103 unsigned NumElems = V2.getValueType().getVectorNumElements();
3104 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3105 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003106 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003107 for (unsigned i = 0; i != NumElems; ++i)
3108 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3109 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3110 else
3111 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003112 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003113 &MaskVec[0], MaskVec.size());
3114 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3115}
3116
Evan Chengdea99362008-05-29 08:22:04 +00003117/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3118/// a shuffle that is zero.
3119static
Dan Gohman8181bd12008-07-27 21:46:04 +00003120unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003121 unsigned NumElems, bool Low,
3122 SelectionDAG &DAG) {
3123 unsigned NumZeros = 0;
3124 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003125 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003126 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003127 if (Idx.getOpcode() == ISD::UNDEF) {
3128 ++NumZeros;
3129 continue;
3130 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003131 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3132 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003133 ++NumZeros;
3134 else
3135 break;
3136 }
3137 return NumZeros;
3138}
3139
3140/// isVectorShift - Returns true if the shuffle can be implemented as a
3141/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003142static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3143 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003144 unsigned NumElems = Mask.getNumOperands();
3145
3146 isLeft = true;
3147 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3148 if (!NumZeros) {
3149 isLeft = false;
3150 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3151 if (!NumZeros)
3152 return false;
3153 }
3154
3155 bool SeenV1 = false;
3156 bool SeenV2 = false;
3157 for (unsigned i = NumZeros; i < NumElems; ++i) {
3158 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003159 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003160 if (Idx.getOpcode() == ISD::UNDEF)
3161 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003162 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003163 if (Index < NumElems)
3164 SeenV1 = true;
3165 else {
3166 Index -= NumElems;
3167 SeenV2 = true;
3168 }
3169 if (Index != Val)
3170 return false;
3171 }
3172 if (SeenV1 && SeenV2)
3173 return false;
3174
3175 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3176 ShAmt = NumZeros;
3177 return true;
3178}
3179
3180
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003181/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3182///
Dan Gohman8181bd12008-07-27 21:46:04 +00003183static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003184 unsigned NumNonZero, unsigned NumZero,
3185 SelectionDAG &DAG, TargetLowering &TLI) {
3186 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003187 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003188
Dan Gohman8181bd12008-07-27 21:46:04 +00003189 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003190 bool First = true;
3191 for (unsigned i = 0; i < 16; ++i) {
3192 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3193 if (ThisIsNonZero && First) {
3194 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003195 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003196 else
3197 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3198 First = false;
3199 }
3200
3201 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003202 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003203 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3204 if (LastIsNonZero) {
3205 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3206 }
3207 if (ThisIsNonZero) {
3208 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3209 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3210 ThisElt, DAG.getConstant(8, MVT::i8));
3211 if (LastIsNonZero)
3212 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3213 } else
3214 ThisElt = LastElt;
3215
Gabor Greif1c80d112008-08-28 21:40:38 +00003216 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003217 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003218 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003219 }
3220 }
3221
3222 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3223}
3224
3225/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3226///
Dan Gohman8181bd12008-07-27 21:46:04 +00003227static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003228 unsigned NumNonZero, unsigned NumZero,
3229 SelectionDAG &DAG, TargetLowering &TLI) {
3230 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003231 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003232
Dan Gohman8181bd12008-07-27 21:46:04 +00003233 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003234 bool First = true;
3235 for (unsigned i = 0; i < 8; ++i) {
3236 bool isNonZero = (NonZeros & (1 << i)) != 0;
3237 if (isNonZero) {
3238 if (First) {
3239 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003240 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003241 else
3242 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3243 First = false;
3244 }
3245 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003246 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003247 }
3248 }
3249
3250 return V;
3251}
3252
Evan Chengdea99362008-05-29 08:22:04 +00003253/// getVShift - Return a vector logical shift node.
3254///
Dan Gohman8181bd12008-07-27 21:46:04 +00003255static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003256 unsigned NumBits, SelectionDAG &DAG,
3257 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003258 bool isMMX = VT.getSizeInBits() == 64;
3259 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003260 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3261 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3262 return DAG.getNode(ISD::BIT_CONVERT, VT,
3263 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003264 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003265}
3266
Dan Gohman8181bd12008-07-27 21:46:04 +00003267SDValue
3268X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003269 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003270 if (ISD::isBuildVectorAllZeros(Op.getNode())
3271 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003272 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3273 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3274 // eliminated on x86-32 hosts.
3275 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3276 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003277
Gabor Greif1c80d112008-08-28 21:40:38 +00003278 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003279 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003280 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003281 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003282
Duncan Sands92c43912008-06-06 12:08:01 +00003283 MVT VT = Op.getValueType();
3284 MVT EVT = VT.getVectorElementType();
3285 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286
3287 unsigned NumElems = Op.getNumOperands();
3288 unsigned NumZero = 0;
3289 unsigned NumNonZero = 0;
3290 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003291 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003292 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003293 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003294 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003295 if (Elt.getOpcode() == ISD::UNDEF)
3296 continue;
3297 Values.insert(Elt);
3298 if (Elt.getOpcode() != ISD::Constant &&
3299 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003300 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003301 if (isZeroNode(Elt))
3302 NumZero++;
3303 else {
3304 NonZeros |= (1 << i);
3305 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003306 }
3307 }
3308
3309 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003310 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3311 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003312 }
3313
Chris Lattner66a4dda2008-03-09 05:42:06 +00003314 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003315 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003316 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003317 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003318
Chris Lattner2d91b962008-03-09 01:05:04 +00003319 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3320 // the value are obviously zero, truncate the value to i32 and do the
3321 // insertion that way. Only do this if the value is non-constant or if the
3322 // value is a constant being inserted into element 0. It is cheaper to do
3323 // a constant pool load than it is to do a movd + shuffle.
3324 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3325 (!IsAllConstants || Idx == 0)) {
3326 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3327 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003328 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3329 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003330
3331 // Truncate the value (which may itself be a constant) to i32, and
3332 // convert it to a vector with movd (S2V+shuffle to zero extend).
3333 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3334 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003335 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3336 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003337
3338 // Now we have our 32-bit value zero extended in the low element of
3339 // a vector. If Idx != 0, swizzle it into place.
3340 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003341 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003342 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3343 getSwapEltZeroMask(VecElts, Idx, DAG)
3344 };
3345 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3346 }
3347 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3348 }
3349 }
3350
Chris Lattnerac914892008-03-08 22:59:52 +00003351 // If we have a constant or non-constant insertion into the low element of
3352 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3353 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3354 // depending on what the source datatype is. Because we can only get here
3355 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3356 if (Idx == 0 &&
3357 // Don't do this for i64 values on x86-32.
3358 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003359 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003360 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003361 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3362 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003363 }
Evan Chengdea99362008-05-29 08:22:04 +00003364
3365 // Is it a vector logical left shift?
3366 if (NumElems == 2 && Idx == 1 &&
3367 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003368 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003369 return getVShift(true, VT,
3370 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3371 NumBits/2, DAG, *this);
3372 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003373
3374 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003375 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003376
Chris Lattnerac914892008-03-08 22:59:52 +00003377 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3378 // is a non-constant being inserted into an element other than the low one,
3379 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3380 // movd/movss) to move this into the low element, then shuffle it into
3381 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003382 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003383 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3384
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003385 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003386 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3387 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003388 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3389 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003390 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003391 for (unsigned i = 0; i < NumElems; i++)
3392 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003393 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003394 &MaskVec[0], MaskVec.size());
3395 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3396 DAG.getNode(ISD::UNDEF, VT), Mask);
3397 }
3398 }
3399
Chris Lattner66a4dda2008-03-09 05:42:06 +00003400 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3401 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003402 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003403
Dan Gohman21463242007-07-24 22:55:08 +00003404 // A vector full of immediates; various special cases are already
3405 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003406 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003407 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003408
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003409 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003410 if (EVTBits == 64) {
3411 if (NumNonZero == 1) {
3412 // One half is zero or undef.
3413 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003414 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003415 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003416 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3417 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003418 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003419 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003420 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003421
3422 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3423 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003424 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003425 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003426 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003427 }
3428
3429 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003430 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003431 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003432 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003433 }
3434
3435 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003436 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003437 V.resize(NumElems);
3438 if (NumElems == 4 && NumZero > 0) {
3439 for (unsigned i = 0; i < 4; ++i) {
3440 bool isZero = !(NonZeros & (1 << i));
3441 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003442 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003443 else
3444 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3445 }
3446
3447 for (unsigned i = 0; i < 2; ++i) {
3448 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3449 default: break;
3450 case 0:
3451 V[i] = V[i*2]; // Must be a zero vector.
3452 break;
3453 case 1:
3454 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3455 getMOVLMask(NumElems, DAG));
3456 break;
3457 case 2:
3458 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3459 getMOVLMask(NumElems, DAG));
3460 break;
3461 case 3:
3462 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3463 getUnpacklMask(NumElems, DAG));
3464 break;
3465 }
3466 }
3467
Duncan Sands92c43912008-06-06 12:08:01 +00003468 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3469 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003470 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003471 bool Reverse = (NonZeros & 0x3) == 2;
3472 for (unsigned i = 0; i < 2; ++i)
3473 if (Reverse)
3474 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3475 else
3476 MaskVec.push_back(DAG.getConstant(i, EVT));
3477 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3478 for (unsigned i = 0; i < 2; ++i)
3479 if (Reverse)
3480 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3481 else
3482 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003483 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003484 &MaskVec[0], MaskVec.size());
3485 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3486 }
3487
3488 if (Values.size() > 2) {
3489 // Expand into a number of unpckl*.
3490 // e.g. for v4f32
3491 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3492 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3493 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003494 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003495 for (unsigned i = 0; i < NumElems; ++i)
3496 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3497 NumElems >>= 1;
3498 while (NumElems != 0) {
3499 for (unsigned i = 0; i < NumElems; ++i)
3500 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3501 UnpckMask);
3502 NumElems >>= 1;
3503 }
3504 return V[0];
3505 }
3506
Dan Gohman8181bd12008-07-27 21:46:04 +00003507 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003508}
3509
Evan Chengfca29242007-12-07 08:07:39 +00003510static
Dan Gohman8181bd12008-07-27 21:46:04 +00003511SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003512 SDValue PermMask, SelectionDAG &DAG,
3513 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003514 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003515 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3516 MVT MaskEVT = MaskVT.getVectorElementType();
3517 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003518 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3519 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003520
3521 // First record which half of which vector the low elements come from.
3522 SmallVector<unsigned, 4> LowQuad(4);
3523 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003524 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003525 if (Elt.getOpcode() == ISD::UNDEF)
3526 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003527 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003528 int QuadIdx = EltIdx / 4;
3529 ++LowQuad[QuadIdx];
3530 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003531
Evan Cheng75184a92007-12-11 01:46:18 +00003532 int BestLowQuad = -1;
3533 unsigned MaxQuad = 1;
3534 for (unsigned i = 0; i < 4; ++i) {
3535 if (LowQuad[i] > MaxQuad) {
3536 BestLowQuad = i;
3537 MaxQuad = LowQuad[i];
3538 }
Evan Chengfca29242007-12-07 08:07:39 +00003539 }
3540
Evan Cheng75184a92007-12-11 01:46:18 +00003541 // Record which half of which vector the high elements come from.
3542 SmallVector<unsigned, 4> HighQuad(4);
3543 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003544 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003545 if (Elt.getOpcode() == ISD::UNDEF)
3546 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003547 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003548 int QuadIdx = EltIdx / 4;
3549 ++HighQuad[QuadIdx];
3550 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003551
Evan Cheng75184a92007-12-11 01:46:18 +00003552 int BestHighQuad = -1;
3553 MaxQuad = 1;
3554 for (unsigned i = 0; i < 4; ++i) {
3555 if (HighQuad[i] > MaxQuad) {
3556 BestHighQuad = i;
3557 MaxQuad = HighQuad[i];
3558 }
3559 }
3560
3561 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3562 if (BestLowQuad != -1 || BestHighQuad != -1) {
3563 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003564 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003565
Evan Cheng75184a92007-12-11 01:46:18 +00003566 if (BestLowQuad != -1)
3567 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3568 else
3569 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003570
Evan Cheng75184a92007-12-11 01:46:18 +00003571 if (BestHighQuad != -1)
3572 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3573 else
3574 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003575
Dan Gohman8181bd12008-07-27 21:46:04 +00003576 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003577 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3578 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3579 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3580 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3581
3582 // Now sort high and low parts separately.
3583 BitVector InOrder(8);
3584 if (BestLowQuad != -1) {
3585 // Sort lower half in order using PSHUFLW.
3586 MaskVec.clear();
3587 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003588
Evan Cheng75184a92007-12-11 01:46:18 +00003589 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003590 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003591 if (Elt.getOpcode() == ISD::UNDEF) {
3592 MaskVec.push_back(Elt);
3593 InOrder.set(i);
3594 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003595 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003596 if (EltIdx != i)
3597 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003598
Evan Cheng75184a92007-12-11 01:46:18 +00003599 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003600
Evan Cheng75184a92007-12-11 01:46:18 +00003601 // If this element is in the right place after this shuffle, then
3602 // remember it.
3603 if ((int)(EltIdx / 4) == BestLowQuad)
3604 InOrder.set(i);
3605 }
3606 }
3607 if (AnyOutOrder) {
3608 for (unsigned i = 4; i != 8; ++i)
3609 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
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 if (BestHighQuad != -1) {
3616 // Sort high half in order using PSHUFHW if possible.
3617 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003618
Evan Cheng75184a92007-12-11 01:46:18 +00003619 for (unsigned i = 0; i != 4; ++i)
3620 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003621
Evan Cheng75184a92007-12-11 01:46:18 +00003622 bool AnyOutOrder = false;
3623 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003624 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003625 if (Elt.getOpcode() == ISD::UNDEF) {
3626 MaskVec.push_back(Elt);
3627 InOrder.set(i);
3628 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003629 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003630 if (EltIdx != i)
3631 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003632
Evan Cheng75184a92007-12-11 01:46:18 +00003633 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003634
Evan Cheng75184a92007-12-11 01:46:18 +00003635 // If this element is in the right place after this shuffle, then
3636 // remember it.
3637 if ((int)(EltIdx / 4) == BestHighQuad)
3638 InOrder.set(i);
3639 }
3640 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003641
Evan Cheng75184a92007-12-11 01:46:18 +00003642 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003643 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003644 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3645 }
3646 }
3647
3648 // The other elements are put in the right place using pextrw and pinsrw.
3649 for (unsigned i = 0; i != 8; ++i) {
3650 if (InOrder[i])
3651 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003652 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003653 if (Elt.getOpcode() == ISD::UNDEF)
3654 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003655 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003656 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003657 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3658 DAG.getConstant(EltIdx, PtrVT))
3659 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3660 DAG.getConstant(EltIdx - 8, PtrVT));
3661 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3662 DAG.getConstant(i, PtrVT));
3663 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003664
Evan Cheng75184a92007-12-11 01:46:18 +00003665 return NewV;
3666 }
3667
Bill Wendling2c7cd592008-08-21 22:35:37 +00003668 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3669 // few as possible. First, let's find out how many elements are already in the
3670 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003671 unsigned V1InOrder = 0;
3672 unsigned V1FromV1 = 0;
3673 unsigned V2InOrder = 0;
3674 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003675 SmallVector<SDValue, 8> V1Elts;
3676 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003677 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003678 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003679 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003680 V1Elts.push_back(Elt);
3681 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003682 ++V1InOrder;
3683 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003684 continue;
3685 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003686 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003687 if (EltIdx == i) {
3688 V1Elts.push_back(Elt);
3689 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3690 ++V1InOrder;
3691 } else if (EltIdx == i+8) {
3692 V1Elts.push_back(Elt);
3693 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3694 ++V2InOrder;
3695 } else if (EltIdx < 8) {
3696 V1Elts.push_back(Elt);
Mon P Wang75498182009-01-28 23:11:14 +00003697 V2Elts.push_back(DAG.getConstant(EltIdx+8, MaskEVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003698 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003699 } else {
Mon P Wang532c9632008-12-23 04:03:27 +00003700 V1Elts.push_back(Elt);
Evan Cheng75184a92007-12-11 01:46:18 +00003701 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3702 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003703 }
3704 }
3705
3706 if (V2InOrder > V1InOrder) {
3707 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3708 std::swap(V1, V2);
3709 std::swap(V1Elts, V2Elts);
3710 std::swap(V1FromV1, V2FromV2);
3711 }
3712
Evan Cheng75184a92007-12-11 01:46:18 +00003713 if ((V1FromV1 + V1InOrder) != 8) {
3714 // Some elements are from V2.
3715 if (V1FromV1) {
3716 // If there are elements that are from V1 but out of place,
3717 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003718 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003719 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003720 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003721 if (Elt.getOpcode() == ISD::UNDEF) {
3722 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3723 continue;
3724 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003725 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003726 if (EltIdx >= 8)
3727 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3728 else
3729 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3730 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003731 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003732 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003733 }
Evan Cheng75184a92007-12-11 01:46:18 +00003734
3735 NewV = V1;
3736 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003737 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003738 if (Elt.getOpcode() == ISD::UNDEF)
3739 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003740 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003741 if (EltIdx < 8)
3742 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003743 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003744 DAG.getConstant(EltIdx - 8, PtrVT));
3745 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3746 DAG.getConstant(i, PtrVT));
3747 }
3748 return NewV;
3749 } else {
3750 // All elements are from V1.
3751 NewV = V1;
3752 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003753 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003754 if (Elt.getOpcode() == ISD::UNDEF)
3755 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003756 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003757 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003758 DAG.getConstant(EltIdx, PtrVT));
3759 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3760 DAG.getConstant(i, PtrVT));
3761 }
3762 return NewV;
3763 }
3764}
3765
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003766/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3767/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3768/// done when every pair / quad of shuffle mask elements point to elements in
3769/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003770/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3771static
Dan Gohman8181bd12008-07-27 21:46:04 +00003772SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003773 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003774 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003775 TargetLowering &TLI) {
3776 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003777 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003778 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003779 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003780 MVT NewVT = MaskVT;
3781 switch (VT.getSimpleVT()) {
3782 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003783 case MVT::v4f32: NewVT = MVT::v2f64; break;
3784 case MVT::v4i32: NewVT = MVT::v2i64; break;
3785 case MVT::v8i16: NewVT = MVT::v4i32; break;
3786 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003787 }
3788
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003789 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003790 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003791 NewVT = MVT::v2i64;
3792 else
3793 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003794 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003795 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003796 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003797 for (unsigned i = 0; i < NumElems; i += Scale) {
3798 unsigned StartIdx = ~0U;
3799 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003800 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003801 if (Elt.getOpcode() == ISD::UNDEF)
3802 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003803 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003804 if (StartIdx == ~0U)
3805 StartIdx = EltIdx - (EltIdx % Scale);
3806 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003807 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003808 }
3809 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003810 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003811 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003812 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003813 }
3814
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003815 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3816 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3817 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3818 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3819 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003820}
3821
Evan Chenge9b9c672008-05-09 21:53:03 +00003822/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003823///
Dan Gohman8181bd12008-07-27 21:46:04 +00003824static SDValue getVZextMovL(MVT VT, MVT OpVT,
3825 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003826 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003827 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3828 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003829 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003830 LD = dyn_cast<LoadSDNode>(SrcOp);
3831 if (!LD) {
3832 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3833 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003834 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003835 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3836 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3837 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3838 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3839 // PR2108
3840 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3841 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003842 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003843 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003844 SrcOp.getOperand(0)
3845 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003846 }
3847 }
3848 }
3849
3850 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003851 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003852 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3853}
3854
Evan Chengf50554e2008-07-22 21:13:36 +00003855/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3856/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003857static SDValue
3858LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3859 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003860 MVT MaskVT = PermMask.getValueType();
3861 MVT MaskEVT = MaskVT.getVectorElementType();
3862 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003863 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003864 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003865 unsigned NumHi = 0;
3866 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003867 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003868 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003869 if (Elt.getOpcode() == ISD::UNDEF) {
3870 Locs[i] = std::make_pair(-1, -1);
3871 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003872 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003873 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003874 if (Val < 4) {
3875 Locs[i] = std::make_pair(0, NumLo);
3876 Mask1[NumLo] = Elt;
3877 NumLo++;
3878 } else {
3879 Locs[i] = std::make_pair(1, NumHi);
3880 if (2+NumHi < 4)
3881 Mask1[2+NumHi] = Elt;
3882 NumHi++;
3883 }
3884 }
3885 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003886
Evan Chengf50554e2008-07-22 21:13:36 +00003887 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003888 // If no more than two elements come from either vector. This can be
3889 // implemented with two shuffles. First shuffle gather the elements.
3890 // The second shuffle, which takes the first shuffle as both of its
3891 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003892 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3893 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3894 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003895
Dan Gohman8181bd12008-07-27 21:46:04 +00003896 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003897 for (unsigned i = 0; i != 4; ++i) {
3898 if (Locs[i].first == -1)
3899 continue;
3900 else {
3901 unsigned Idx = (i < 2) ? 0 : 4;
3902 Idx += Locs[i].first * 2 + Locs[i].second;
3903 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3904 }
3905 }
3906
3907 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3908 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3909 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003910 } else if (NumLo == 3 || NumHi == 3) {
3911 // Otherwise, we must have three elements from one vector, call it X, and
3912 // one element from the other, call it Y. First, use a shufps to build an
3913 // intermediate vector with the one element from Y and the element from X
3914 // that will be in the same half in the final destination (the indexes don't
3915 // matter). Then, use a shufps to build the final vector, taking the half
3916 // containing the element from Y from the intermediate, and the other half
3917 // from X.
3918 if (NumHi == 3) {
3919 // Normalize it so the 3 elements come from V1.
3920 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3921 std::swap(V1, V2);
3922 }
3923
3924 // Find the element from V2.
3925 unsigned HiIndex;
3926 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003927 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003928 if (Elt.getOpcode() == ISD::UNDEF)
3929 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003930 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003931 if (Val >= 4)
3932 break;
3933 }
3934
3935 Mask1[0] = PermMask.getOperand(HiIndex);
3936 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3937 Mask1[2] = PermMask.getOperand(HiIndex^1);
3938 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3939 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3940 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3941
3942 if (HiIndex >= 2) {
3943 Mask1[0] = PermMask.getOperand(0);
3944 Mask1[1] = PermMask.getOperand(1);
3945 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3946 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3947 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3948 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3949 } else {
3950 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3951 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3952 Mask1[2] = PermMask.getOperand(2);
3953 Mask1[3] = PermMask.getOperand(3);
3954 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003955 Mask1[2] =
3956 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3957 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003958 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003959 Mask1[3] =
3960 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3961 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003962 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3963 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3964 }
Evan Chengf50554e2008-07-22 21:13:36 +00003965 }
3966
3967 // Break it into (shuffle shuffle_hi, shuffle_lo).
3968 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003969 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3970 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3971 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003972 unsigned MaskIdx = 0;
3973 unsigned LoIdx = 0;
3974 unsigned HiIdx = 2;
3975 for (unsigned i = 0; i != 4; ++i) {
3976 if (i == 2) {
3977 MaskPtr = &HiMask;
3978 MaskIdx = 1;
3979 LoIdx = 0;
3980 HiIdx = 2;
3981 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003982 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003983 if (Elt.getOpcode() == ISD::UNDEF) {
3984 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003985 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003986 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3987 (*MaskPtr)[LoIdx] = Elt;
3988 LoIdx++;
3989 } else {
3990 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3991 (*MaskPtr)[HiIdx] = Elt;
3992 HiIdx++;
3993 }
3994 }
3995
Dan Gohman8181bd12008-07-27 21:46:04 +00003996 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003997 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3998 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003999 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00004000 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4001 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004002 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00004003 for (unsigned i = 0; i != 4; ++i) {
4004 if (Locs[i].first == -1) {
4005 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
4006 } else {
4007 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
4008 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
4009 }
4010 }
4011 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
4012 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4013 &MaskOps[0], MaskOps.size()));
4014}
4015
Dan Gohman8181bd12008-07-27 21:46:04 +00004016SDValue
4017X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
4018 SDValue V1 = Op.getOperand(0);
4019 SDValue V2 = Op.getOperand(1);
4020 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00004021 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004022 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00004023 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004024 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4025 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
4026 bool V1IsSplat = false;
4027 bool V2IsSplat = false;
4028
Gabor Greif1c80d112008-08-28 21:40:38 +00004029 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004030 return DAG.getNode(ISD::UNDEF, VT);
4031
Gabor Greif1c80d112008-08-28 21:40:38 +00004032 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00004033 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004034
Gabor Greif1c80d112008-08-28 21:40:38 +00004035 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004036 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00004037 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004038 return V2;
4039
Evan Chengae6c9212008-09-25 23:35:16 +00004040 // Canonicalize movddup shuffles.
4041 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Chengbdd9d9f2008-10-06 21:13:08 +00004042 VT.getSizeInBits() == 128 &&
Evan Chengae6c9212008-09-25 23:35:16 +00004043 X86::isMOVDDUPMask(PermMask.getNode()))
4044 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
4045
Gabor Greif1c80d112008-08-28 21:40:38 +00004046 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004047 if (isMMX || NumElems < 4) return Op;
4048 // Promote it to a v4{if}32 splat.
4049 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004050 }
4051
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004052 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4053 // do it!
4054 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004055 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004056 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004057 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
4058 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4059 // FIXME: Figure out a cleaner way to do this.
4060 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004061 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004062 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004063 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004064 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004065 SDValue NewV1 = NewOp.getOperand(0);
4066 SDValue NewV2 = NewOp.getOperand(1);
4067 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00004068 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004069 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00004070 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004071 }
4072 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004073 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004074 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004075 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004076 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004077 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00004078 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004079 }
4080 }
4081
Evan Chengdea99362008-05-29 08:22:04 +00004082 // Check if this can be converted into a logical shift.
4083 bool isLeft = false;
4084 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004085 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00004086 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4087 if (isShift && ShVal.hasOneUse()) {
4088 // If the shifted value has multiple uses, it may be cheaper to use
4089 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004090 MVT EVT = VT.getVectorElementType();
4091 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004092 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4093 }
4094
Gabor Greif1c80d112008-08-28 21:40:38 +00004095 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004096 if (V1IsUndef)
4097 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004098 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004099 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004100 if (!isMMX)
4101 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004102 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004103
Gabor Greif1c80d112008-08-28 21:40:38 +00004104 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4105 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4106 X86::isMOVHLPSMask(PermMask.getNode()) ||
4107 X86::isMOVHPMask(PermMask.getNode()) ||
4108 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004109 return Op;
4110
Gabor Greif1c80d112008-08-28 21:40:38 +00004111 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4112 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004113 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4114
Evan Chengdea99362008-05-29 08:22:04 +00004115 if (isShift) {
4116 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004117 MVT EVT = VT.getVectorElementType();
4118 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004119 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4120 }
4121
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004122 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004123 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4124 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004125 V1IsSplat = isSplatVector(V1.getNode());
4126 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004127
4128 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004129 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4130 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4131 std::swap(V1IsSplat, V2IsSplat);
4132 std::swap(V1IsUndef, V2IsUndef);
4133 Commuted = true;
4134 }
4135
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004136 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004137 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004138 if (V2IsUndef) return V1;
4139 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4140 if (V2IsSplat) {
4141 // V2 is a splat, so the mask may be malformed. That is, it may point
4142 // to any V2 element. The instruction selectior won't like this. Get
4143 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004144 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004145 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004146 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4147 }
4148 return Op;
4149 }
4150
Gabor Greif1c80d112008-08-28 21:40:38 +00004151 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4152 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4153 X86::isUNPCKLMask(PermMask.getNode()) ||
4154 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004155 return Op;
4156
4157 if (V2IsSplat) {
4158 // Normalize mask so all entries that point to V2 points to its first
4159 // element then try to match unpck{h|l} again. If match, return a
4160 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004161 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004162 if (NewMask.getNode() != PermMask.getNode()) {
4163 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004164 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004165 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004166 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004167 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004168 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4169 }
4170 }
4171 }
4172
4173 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004174 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004175 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4176
4177 if (Commuted) {
4178 // Commute is back and try unpck* again.
4179 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004180 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4181 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4182 X86::isUNPCKLMask(PermMask.getNode()) ||
4183 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004184 return Op;
4185 }
4186
Evan Chengbf8b2c52008-04-05 00:30:36 +00004187 // Try PSHUF* first, then SHUFP*.
4188 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4189 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004190 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004191 if (V2.getOpcode() != ISD::UNDEF)
4192 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4193 DAG.getNode(ISD::UNDEF, VT), PermMask);
4194 return Op;
4195 }
4196
4197 if (!isMMX) {
4198 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004199 (X86::isPSHUFDMask(PermMask.getNode()) ||
4200 X86::isPSHUFHWMask(PermMask.getNode()) ||
4201 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004202 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004203 if (VT == MVT::v4f32) {
4204 RVT = MVT::v4i32;
4205 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4206 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4207 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4208 } else if (V2.getOpcode() != ISD::UNDEF)
4209 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4210 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4211 if (RVT != VT)
4212 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004213 return Op;
4214 }
4215
Evan Chengbf8b2c52008-04-05 00:30:36 +00004216 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004217 if (X86::isSHUFPMask(PermMask.getNode()) ||
4218 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004219 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004220 }
4221
Evan Cheng75184a92007-12-11 01:46:18 +00004222 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4223 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004224 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004225 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004226 return NewOp;
4227 }
4228
Evan Chengf50554e2008-07-22 21:13:36 +00004229 // Handle all 4 wide cases with a number of shuffles except for MMX.
4230 if (NumElems == 4 && !isMMX)
4231 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004232
Dan Gohman8181bd12008-07-27 21:46:04 +00004233 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004234}
4235
Dan Gohman8181bd12008-07-27 21:46:04 +00004236SDValue
4237X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004238 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004239 MVT VT = Op.getValueType();
4240 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004241 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004242 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004243 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004244 DAG.getValueType(VT));
4245 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004246 } else if (VT.getSizeInBits() == 16) {
Evan Chengf9393b32009-01-02 05:29:08 +00004247 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4248 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4249 if (Idx == 0)
4250 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4251 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4252 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32,
4253 Op.getOperand(0)),
4254 Op.getOperand(1)));
Dan Gohman8181bd12008-07-27 21:46:04 +00004255 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004256 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004257 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004258 DAG.getValueType(VT));
4259 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004260 } else if (VT == MVT::f32) {
4261 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4262 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004263 // result has a single use which is a store or a bitcast to i32. And in
4264 // the case of a store, it's not worth it if the index is a constant 0,
4265 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004266 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004267 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004268 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004269 if ((User->getOpcode() != ISD::STORE ||
4270 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4271 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004272 (User->getOpcode() != ISD::BIT_CONVERT ||
4273 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004274 return SDValue();
4275 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004276 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4277 Op.getOperand(1));
4278 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004279 } else if (VT == MVT::i32) {
4280 // ExtractPS works with constant index.
4281 if (isa<ConstantSDNode>(Op.getOperand(1)))
4282 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004283 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004284 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004285}
4286
4287
Dan Gohman8181bd12008-07-27 21:46:04 +00004288SDValue
4289X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004290 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004291 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004292
Evan Cheng6c249332008-03-24 21:52:23 +00004293 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004294 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004295 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004296 return Res;
4297 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004298
Duncan Sands92c43912008-06-06 12:08:01 +00004299 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004300 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004301 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004302 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004303 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004304 if (Idx == 0)
4305 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4306 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4307 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4308 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004309 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004310 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004311 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004312 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004313 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004314 DAG.getValueType(VT));
4315 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004316 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004317 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004318 if (Idx == 0)
4319 return Op;
4320 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004321 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004322 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004323 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004324 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004325 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004326 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004327 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004328 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004329 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004330 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004331 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004332 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004333 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004334 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4335 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4336 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004337 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004338 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004339 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4340 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4341 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004342 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343 if (Idx == 0)
4344 return Op;
4345
4346 // UNPCKHPD the element to the lowest double word, then movsd.
4347 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4348 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004349 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004350 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004351 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004352 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004353 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004354 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004355 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004356 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004357 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4358 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4359 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004360 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004361 }
4362
Dan Gohman8181bd12008-07-27 21:46:04 +00004363 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004364}
4365
Dan Gohman8181bd12008-07-27 21:46:04 +00004366SDValue
4367X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004368 MVT VT = Op.getValueType();
4369 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004370
Dan Gohman8181bd12008-07-27 21:46:04 +00004371 SDValue N0 = Op.getOperand(0);
4372 SDValue N1 = Op.getOperand(1);
4373 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004374
Dan Gohman5a7af042008-08-14 22:53:18 +00004375 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4376 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004377 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004378 : X86ISD::PINSRW;
4379 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4380 // argument.
4381 if (N1.getValueType() != MVT::i32)
4382 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4383 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004384 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begemand77e59e2008-02-11 04:19:36 +00004385 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004386 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004387 // Bits [7:6] of the constant are the source select. This will always be
4388 // zero here. The DAG Combiner may combine an extract_elt index into these
4389 // bits. For example (insert (extract, 3), 2) could be matched by putting
4390 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4391 // Bits [5:4] of the constant are the destination select. This is the
4392 // value of the incoming immediate.
4393 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4394 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004395 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begemand77e59e2008-02-11 04:19:36 +00004396 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004397 } else if (EVT == MVT::i32) {
4398 // InsertPS works with constant index.
4399 if (isa<ConstantSDNode>(N2))
4400 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004401 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004402 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004403}
4404
Dan Gohman8181bd12008-07-27 21:46:04 +00004405SDValue
4406X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004407 MVT VT = Op.getValueType();
4408 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004409
4410 if (Subtarget->hasSSE41())
4411 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4412
Evan Chenge12a7eb2007-12-12 07:55:34 +00004413 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004414 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004415
Dan Gohman8181bd12008-07-27 21:46:04 +00004416 SDValue N0 = Op.getOperand(0);
4417 SDValue N1 = Op.getOperand(1);
4418 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004419
Duncan Sands92c43912008-06-06 12:08:01 +00004420 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004421 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4422 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423 if (N1.getValueType() != MVT::i32)
4424 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4425 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004426 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004427 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004428 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004429 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004430}
4431
Dan Gohman8181bd12008-07-27 21:46:04 +00004432SDValue
4433X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004434 if (Op.getValueType() == MVT::v2f32)
4435 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4436 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4437 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4438 Op.getOperand(0))));
4439
Dan Gohman8181bd12008-07-27 21:46:04 +00004440 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004441 MVT VT = MVT::v2i32;
4442 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004443 default: break;
4444 case MVT::v16i8:
4445 case MVT::v8i16:
4446 VT = MVT::v4i32;
4447 break;
4448 }
4449 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4450 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004451}
4452
Bill Wendlingfef06052008-09-16 21:48:12 +00004453// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4454// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4455// one of the above mentioned nodes. It has to be wrapped because otherwise
4456// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4457// be used to form addressing mode. These wrapped nodes will be selected
4458// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004459SDValue
4460X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004461 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004462 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004463 getPointerTy(),
4464 CP->getAlignment());
4465 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4466 // With PIC, the address is actually $g + Offset.
4467 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4468 !Subtarget->isPICStyleRIPRel()) {
4469 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4470 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4471 Result);
4472 }
4473
4474 return Result;
4475}
4476
Dan Gohman8181bd12008-07-27 21:46:04 +00004477SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004478X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
Dan Gohman36322c72008-10-18 02:06:02 +00004479 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00004480 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00004481 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4482 bool ExtraLoadRequired =
4483 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4484
4485 // Create the TargetGlobalAddress node, folding in the constant
4486 // offset if it is legal.
4487 SDValue Result;
Dan Gohman3d5257c2008-10-21 03:38:42 +00004488 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +00004489 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4490 Offset = 0;
4491 } else
4492 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004493 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00004494
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004495 // With PIC, the address is actually $g + Offset.
Dan Gohman36322c72008-10-18 02:06:02 +00004496 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004497 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4498 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4499 Result);
4500 }
4501
4502 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4503 // load the value at address GV, not the value of GV itself. This means that
4504 // the GlobalAddress must be in the base or index register of the address, not
4505 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4506 // The same applies for external symbols during PIC codegen
Dan Gohman36322c72008-10-18 02:06:02 +00004507 if (ExtraLoadRequired)
Dan Gohman12a9c082008-02-06 22:27:42 +00004508 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004509 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004510
Dan Gohman36322c72008-10-18 02:06:02 +00004511 // If there was a non-zero offset that we didn't fold, create an explicit
4512 // addition for it.
4513 if (Offset != 0)
4514 Result = DAG.getNode(ISD::ADD, getPointerTy(), Result,
4515 DAG.getConstant(Offset, getPointerTy()));
4516
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004517 return Result;
4518}
4519
Evan Cheng7f250d62008-09-24 00:05:32 +00004520SDValue
4521X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4522 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00004523 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
4524 return LowerGlobalAddress(GV, Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00004525}
4526
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004527// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004528static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004529LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004530 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004531 SDValue InFlag;
4532 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004533 DAG.getNode(X86ISD::GlobalBaseReg,
4534 PtrVT), InFlag);
4535 InFlag = Chain.getValue(1);
4536
4537 // emit leal symbol@TLSGD(,%ebx,1), %eax
4538 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004539 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004540 GA->getValueType(0),
4541 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004542 SDValue Ops[] = { Chain, TGA, InFlag };
4543 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004544 InFlag = Result.getValue(2);
4545 Chain = Result.getValue(1);
4546
4547 // call ___tls_get_addr. This function receives its argument in
4548 // the register EAX.
4549 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4550 InFlag = Chain.getValue(1);
4551
4552 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004553 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004554 DAG.getTargetExternalSymbol("___tls_get_addr",
4555 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004556 DAG.getRegister(X86::EAX, PtrVT),
4557 DAG.getRegister(X86::EBX, PtrVT),
4558 InFlag };
4559 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4560 InFlag = Chain.getValue(1);
4561
4562 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4563}
4564
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004565// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004566static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004567LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004568 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004569 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004570
4571 // emit leaq symbol@TLSGD(%rip), %rdi
4572 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004573 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004574 GA->getValueType(0),
4575 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004576 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4577 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004578 Chain = Result.getValue(1);
4579 InFlag = Result.getValue(2);
4580
aslb204cd52008-08-16 12:58:29 +00004581 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004582 // the register RDI.
4583 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4584 InFlag = Chain.getValue(1);
4585
4586 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004587 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004588 DAG.getTargetExternalSymbol("__tls_get_addr",
4589 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004590 DAG.getRegister(X86::RDI, PtrVT),
4591 InFlag };
4592 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4593 InFlag = Chain.getValue(1);
4594
4595 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4596}
4597
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004598// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4599// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004600static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004601 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004602 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004603 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004604 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4605 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004606 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004607 GA->getValueType(0),
4608 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004609 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004610
4611 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004612 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004613 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004614
4615 // The address of the thread local variable is the add of the thread
4616 // pointer with the offset of the variable.
4617 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4618}
4619
Dan Gohman8181bd12008-07-27 21:46:04 +00004620SDValue
4621X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004622 // TODO: implement the "local dynamic" model
4623 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004624 assert(Subtarget->isTargetELF() &&
4625 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004626 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4627 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4628 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004629 if (Subtarget->is64Bit()) {
4630 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4631 } else {
4632 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4633 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4634 else
4635 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4636 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004637}
4638
Dan Gohman8181bd12008-07-27 21:46:04 +00004639SDValue
4640X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004641 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4642 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004643 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4644 // With PIC, the address is actually $g + Offset.
4645 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4646 !Subtarget->isPICStyleRIPRel()) {
4647 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4648 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4649 Result);
4650 }
4651
4652 return Result;
4653}
4654
Dan Gohman8181bd12008-07-27 21:46:04 +00004655SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004656 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004657 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004658 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4659 // With PIC, the address is actually $g + Offset.
4660 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4661 !Subtarget->isPICStyleRIPRel()) {
4662 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4663 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4664 Result);
4665 }
4666
4667 return Result;
4668}
4669
Chris Lattner62814a32007-10-17 06:02:13 +00004670/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4671/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004672SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004673 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004674 MVT VT = Op.getValueType();
4675 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004676 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004677 SDValue ShOpLo = Op.getOperand(0);
4678 SDValue ShOpHi = Op.getOperand(1);
4679 SDValue ShAmt = Op.getOperand(2);
4680 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004681 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4682 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004683
Dan Gohman8181bd12008-07-27 21:46:04 +00004684 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004685 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004686 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4687 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004688 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004689 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4690 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004691 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004692
Dan Gohman8181bd12008-07-27 21:46:04 +00004693 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004694 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004695 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004696 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004697
Dan Gohman8181bd12008-07-27 21:46:04 +00004698 SDValue Hi, Lo;
4699 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4700 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4701 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004702
Chris Lattner62814a32007-10-17 06:02:13 +00004703 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004704 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4705 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004706 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004707 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4708 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004709 }
4710
Dan Gohman8181bd12008-07-27 21:46:04 +00004711 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004712 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004713}
4714
Dan Gohman8181bd12008-07-27 21:46:04 +00004715SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004716 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004717 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004718 "Unknown SINT_TO_FP to lower!");
4719
4720 // These are really Legal; caller falls through into that case.
4721 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004722 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004723 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4724 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004725 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004726
Duncan Sands92c43912008-06-06 12:08:01 +00004727 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004728 MachineFunction &MF = DAG.getMachineFunction();
4729 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004730 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4731 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004732 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004733 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004734
4735 // Build the FILD
4736 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004737 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004738 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004739 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4740 else
4741 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004742 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004743 Ops.push_back(Chain);
4744 Ops.push_back(StackSlot);
4745 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004746 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004747 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004748
Dale Johannesen2fc20782007-09-14 22:26:36 +00004749 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004750 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004751 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004752
4753 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4754 // shouldn't be necessary except that RFP cannot be live across
4755 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4756 MachineFunction &MF = DAG.getMachineFunction();
4757 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004758 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004759 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004760 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004761 Ops.push_back(Chain);
4762 Ops.push_back(Result);
4763 Ops.push_back(StackSlot);
4764 Ops.push_back(DAG.getValueType(Op.getValueType()));
4765 Ops.push_back(InFlag);
4766 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004767 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004768 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004769 }
4770
4771 return Result;
4772}
4773
Bill Wendling14a30ef2009-01-17 03:56:04 +00004774// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4775SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4776 // This algorithm is not obvious. Here it is in C code, more or less:
4777 /*
4778 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4779 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4780 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesenfb019af2008-10-21 23:07:49 +00004781
Bill Wendling14a30ef2009-01-17 03:56:04 +00004782 // Copy ints to xmm registers.
4783 __m128i xh = _mm_cvtsi32_si128( hi );
4784 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004785
Bill Wendling14a30ef2009-01-17 03:56:04 +00004786 // Combine into low half of a single xmm register.
4787 __m128i x = _mm_unpacklo_epi32( xh, xl );
4788 __m128d d;
4789 double sd;
Dale Johannesenfb019af2008-10-21 23:07:49 +00004790
Bill Wendling14a30ef2009-01-17 03:56:04 +00004791 // Merge in appropriate exponents to give the integer bits the right
4792 // magnitude.
4793 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004794
Bill Wendling14a30ef2009-01-17 03:56:04 +00004795 // Subtract away the biases to deal with the IEEE-754 double precision
4796 // implicit 1.
4797 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004798
Bill Wendling14a30ef2009-01-17 03:56:04 +00004799 // All conversions up to here are exact. The correctly rounded result is
4800 // calculated using the current rounding mode using the following
4801 // horizontal add.
4802 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4803 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4804 // store doesn't really need to be here (except
4805 // maybe to zero the other double)
4806 return sd;
4807 }
4808 */
Dale Johannesenfb019af2008-10-21 23:07:49 +00004809
Dale Johannesena359b8b2008-10-21 20:50:01 +00004810 // Build some magic constants.
Bill Wendling14a30ef2009-01-17 03:56:04 +00004811 std::vector<Constant*> CV0;
Dale Johannesena359b8b2008-10-21 20:50:01 +00004812 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4813 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4814 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4815 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4816 Constant *C0 = ConstantVector::get(CV0);
4817 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 4);
4818
Bill Wendling14a30ef2009-01-17 03:56:04 +00004819 std::vector<Constant*> CV1;
Dale Johannesena359b8b2008-10-21 20:50:01 +00004820 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4821 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4822 Constant *C1 = ConstantVector::get(CV1);
4823 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 4);
4824
4825 SmallVector<SDValue, 4> MaskVec;
4826 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
4827 MaskVec.push_back(DAG.getConstant(4, MVT::i32));
4828 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
4829 MaskVec.push_back(DAG.getConstant(5, MVT::i32));
4830 SDValue UnpcklMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, &MaskVec[0],
4831 MaskVec.size());
4832 SmallVector<SDValue, 4> MaskVec2;
Duncan Sandsca872ca2008-10-22 11:24:12 +00004833 MaskVec2.push_back(DAG.getConstant(1, MVT::i32));
4834 MaskVec2.push_back(DAG.getConstant(0, MVT::i32));
4835 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec2[0],
Dale Johannesena359b8b2008-10-21 20:50:01 +00004836 MaskVec2.size());
4837
4838 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004839 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4840 Op.getOperand(0),
4841 DAG.getIntPtrConstant(1)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004842 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004843 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4844 Op.getOperand(0),
4845 DAG.getIntPtrConstant(0)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004846 SDValue Unpck1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4847 XR1, XR2, UnpcklMask);
4848 SDValue CLod0 = DAG.getLoad(MVT::v4i32, DAG.getEntryNode(), CPIdx0,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004849 PseudoSourceValue::getConstantPool(), 0,
4850 false, 16);
Dale Johannesena359b8b2008-10-21 20:50:01 +00004851 SDValue Unpck2 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004852 Unpck1, CLod0, UnpcklMask);
Dale Johannesena359b8b2008-10-21 20:50:01 +00004853 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Unpck2);
4854 SDValue CLod1 = DAG.getLoad(MVT::v2f64, CLod0.getValue(1), CPIdx1,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004855 PseudoSourceValue::getConstantPool(), 0,
4856 false, 16);
Dale Johannesena359b8b2008-10-21 20:50:01 +00004857 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::v2f64, XR2F, CLod1);
Bill Wendling14a30ef2009-01-17 03:56:04 +00004858
Dale Johannesena359b8b2008-10-21 20:50:01 +00004859 // Add the halves; easiest way is to swap them into another reg first.
4860 SDValue Shuf = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2f64,
4861 Sub, Sub, ShufMask);
4862 SDValue Add = DAG.getNode(ISD::FADD, MVT::v2f64, Shuf, Sub);
4863 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64, Add,
4864 DAG.getIntPtrConstant(0));
4865}
4866
Bill Wendling14a30ef2009-01-17 03:56:04 +00004867// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4868SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
4869 // FP constant to bias correct the final result.
4870 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4871 MVT::f64);
4872
4873 // Load the 32-bit value into an XMM register.
4874 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
4875 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4876 Op.getOperand(0),
4877 DAG.getIntPtrConstant(0)));
4878
4879 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64,
4880 DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Load),
4881 DAG.getIntPtrConstant(0));
4882
4883 // Or the load with the bias.
4884 SDValue Or = DAG.getNode(ISD::OR, MVT::v2i64,
4885 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64,
4886 DAG.getNode(ISD::SCALAR_TO_VECTOR,
Evan Chengc0ab5e52009-01-19 08:19:57 +00004887 MVT::v2f64, Load)),
Bill Wendling14a30ef2009-01-17 03:56:04 +00004888 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64,
4889 DAG.getNode(ISD::SCALAR_TO_VECTOR,
Evan Chengc0ab5e52009-01-19 08:19:57 +00004890 MVT::v2f64, Bias)));
Bill Wendling14a30ef2009-01-17 03:56:04 +00004891 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64,
4892 DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Or),
4893 DAG.getIntPtrConstant(0));
4894
4895 // Subtract the bias.
4896 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Or, Bias);
4897
4898 // Handle final rounding.
Bill Wendlingdb547de2009-01-17 07:40:19 +00004899 MVT DestVT = Op.getValueType();
4900
4901 if (DestVT.bitsLT(MVT::f64)) {
4902 return DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
4903 DAG.getIntPtrConstant(0));
4904 } else if (DestVT.bitsGT(MVT::f64)) {
4905 return DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
4906 }
4907
4908 // Handle final rounding.
4909 return Sub;
Bill Wendling14a30ef2009-01-17 03:56:04 +00004910}
4911
4912SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Cheng44fd2392009-01-19 08:08:22 +00004913 SDValue N0 = Op.getOperand(0);
Bill Wendling14a30ef2009-01-17 03:56:04 +00004914
Evan Cheng44fd2392009-01-19 08:08:22 +00004915 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4916 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4917 // the optimization here.
4918 if (DAG.SignBitIsZero(N0))
4919 return DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), N0);
4920
4921 MVT SrcVT = N0.getValueType();
Bill Wendling14a30ef2009-01-17 03:56:04 +00004922 if (SrcVT == MVT::i64) {
4923 // We only handle SSE2 f64 target here; caller can handle the rest.
4924 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4925 return SDValue();
Bill Wendlingdb547de2009-01-17 07:40:19 +00004926
Bill Wendling14a30ef2009-01-17 03:56:04 +00004927 return LowerUINT_TO_FP_i64(Op, DAG);
4928 } else if (SrcVT == MVT::i32) {
Bill Wendling14a30ef2009-01-17 03:56:04 +00004929 return LowerUINT_TO_FP_i32(Op, DAG);
4930 }
4931
4932 assert(0 && "Unknown UINT_TO_FP to lower!");
4933 return SDValue();
4934}
4935
Dan Gohman8181bd12008-07-27 21:46:04 +00004936std::pair<SDValue,SDValue> X86TargetLowering::
4937FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004938 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4939 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004940 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004941
Dale Johannesen2fc20782007-09-14 22:26:36 +00004942 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004943 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004944 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004945 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004946 if (Subtarget->is64Bit() &&
4947 Op.getValueType() == MVT::i64 &&
4948 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004949 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004950
Evan Cheng05441e62007-10-15 20:11:21 +00004951 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4952 // stack slot.
4953 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004954 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004955 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004956 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004957 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004958 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004959 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4960 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4961 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4962 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004963 }
4964
Dan Gohman8181bd12008-07-27 21:46:04 +00004965 SDValue Chain = DAG.getEntryNode();
4966 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004967 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004968 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004969 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004970 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004971 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004972 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004973 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4974 };
4975 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4976 Chain = Value.getValue(1);
4977 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4978 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4979 }
4980
4981 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004982 SDValue Ops[] = { Chain, Value, StackSlot };
4983 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004984
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004985 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004986}
4987
Dan Gohman8181bd12008-07-27 21:46:04 +00004988SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4989 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4990 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004991 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004992
4993 // Load the result.
4994 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4995}
4996
Dan Gohman8181bd12008-07-27 21:46:04 +00004997SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004998 MVT VT = Op.getValueType();
4999 MVT EltVT = VT;
5000 if (VT.isVector())
5001 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002 std::vector<Constant*> CV;
5003 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005004 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005005 CV.push_back(C);
5006 CV.push_back(C);
5007 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005008 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005009 CV.push_back(C);
5010 CV.push_back(C);
5011 CV.push_back(C);
5012 CV.push_back(C);
5013 }
Dan Gohman11821702007-07-27 17:16:43 +00005014 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005015 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
5016 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005017 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005018 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005019 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
5020}
5021
Dan Gohman8181bd12008-07-27 21:46:04 +00005022SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005023 MVT VT = Op.getValueType();
5024 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00005025 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00005026 if (VT.isVector()) {
5027 EltVT = VT.getVectorElementType();
5028 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00005029 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005030 std::vector<Constant*> CV;
5031 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005032 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005033 CV.push_back(C);
5034 CV.push_back(C);
5035 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005036 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005037 CV.push_back(C);
5038 CV.push_back(C);
5039 CV.push_back(C);
5040 CV.push_back(C);
5041 }
Dan Gohman11821702007-07-27 17:16:43 +00005042 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005043 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
5044 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005045 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005046 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00005047 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00005048 return DAG.getNode(ISD::BIT_CONVERT, VT,
5049 DAG.getNode(ISD::XOR, MVT::v2i64,
5050 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
5051 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
5052 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00005053 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
5054 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005055}
5056
Dan Gohman8181bd12008-07-27 21:46:04 +00005057SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
5058 SDValue Op0 = Op.getOperand(0);
5059 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00005060 MVT VT = Op.getValueType();
5061 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005062
5063 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005064 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005065 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
5066 SrcVT = VT;
5067 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005068 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005069 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00005070 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005071 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005072 }
5073
5074 // At this point the operands and the result should have the same
5075 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005076
5077 // First get the sign bit of second operand.
5078 std::vector<Constant*> CV;
5079 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005080 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
5081 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005082 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005083 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
5084 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5085 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5086 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005087 }
Dan Gohman11821702007-07-27 17:16:43 +00005088 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005089 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
5090 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005091 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005092 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00005093 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005094
5095 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005096 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005097 // Op0 is MVT::f32, Op1 is MVT::f64.
5098 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
5099 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
5100 DAG.getConstant(32, MVT::i32));
5101 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
5102 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00005103 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005104 }
5105
5106 // Clear first operand sign bit.
5107 CV.clear();
5108 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005109 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
5110 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005111 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005112 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
5113 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5114 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5115 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005116 }
Dan Gohman11821702007-07-27 17:16:43 +00005117 C = ConstantVector::get(CV);
5118 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00005119 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005120 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005121 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00005122 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005123
5124 // Or the value with the sign bit.
5125 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
5126}
5127
Dan Gohman8181bd12008-07-27 21:46:04 +00005128SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00005129 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00005130 SDValue Op0 = Op.getOperand(0);
5131 SDValue Op1 = Op.getOperand(1);
Chris Lattner77a62312008-12-25 05:34:37 +00005132 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5133
Dan Gohman22cefb02009-01-29 01:59:02 +00005134 // Lower (X & (1 << N)) == 0 to BT(X, N).
5135 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5136 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman13dd9522009-01-13 23:25:30 +00005137 if (Op0.getOpcode() == ISD::AND &&
5138 Op0.hasOneUse() &&
5139 Op1.getOpcode() == ISD::Constant &&
Dan Gohman22cefb02009-01-29 01:59:02 +00005140 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattner77a62312008-12-25 05:34:37 +00005141 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohman22cefb02009-01-29 01:59:02 +00005142 SDValue LHS, RHS;
5143 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5144 if (ConstantSDNode *Op010C =
5145 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5146 if (Op010C->getZExtValue() == 1) {
5147 LHS = Op0.getOperand(0);
5148 RHS = Op0.getOperand(1).getOperand(1);
5149 }
5150 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5151 if (ConstantSDNode *Op000C =
5152 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5153 if (Op000C->getZExtValue() == 1) {
5154 LHS = Op0.getOperand(1);
5155 RHS = Op0.getOperand(0).getOperand(1);
5156 }
5157 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5158 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5159 SDValue AndLHS = Op0.getOperand(0);
5160 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5161 LHS = AndLHS.getOperand(0);
5162 RHS = AndLHS.getOperand(1);
5163 }
5164 }
Evan Cheng950aac02007-09-25 01:57:46 +00005165
Dan Gohman22cefb02009-01-29 01:59:02 +00005166 if (LHS.getNode()) {
Chris Lattner77a62312008-12-25 05:34:37 +00005167 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5168 // instruction. Since the shift amount is in-range-or-undefined, we know
5169 // that doing a bittest on the i16 value is ok. We extend to i32 because
5170 // the encoding for the i16 version is larger than the i32 version.
5171 if (LHS.getValueType() == MVT::i8)
5172 LHS = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, LHS);
5173
5174 // If the operand types disagree, extend the shift amount to match. Since
5175 // BT ignores high bits (like shifts) we can use anyextend.
5176 if (LHS.getValueType() != RHS.getValueType())
5177 RHS = DAG.getNode(ISD::ANY_EXTEND, LHS.getValueType(), RHS);
Dan Gohman22cefb02009-01-29 01:59:02 +00005178
Chris Lattner77a62312008-12-25 05:34:37 +00005179 SDValue BT = DAG.getNode(X86ISD::BT, MVT::i32, LHS, RHS);
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005180 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dan Gohman22cefb02009-01-29 01:59:02 +00005181 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Chris Lattner77a62312008-12-25 05:34:37 +00005182 DAG.getConstant(Cond, MVT::i8), BT);
5183 }
5184 }
5185
5186 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5187 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Chris Lattner60435922008-12-24 00:11:37 +00005188
Chris Lattner77a62312008-12-25 05:34:37 +00005189 SDValue Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Chris Lattner60435922008-12-24 00:11:37 +00005190 return DAG.getNode(X86ISD::SETCC, MVT::i8,
5191 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005192}
5193
Dan Gohman8181bd12008-07-27 21:46:04 +00005194SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5195 SDValue Cond;
5196 SDValue Op0 = Op.getOperand(0);
5197 SDValue Op1 = Op.getOperand(1);
5198 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00005199 MVT VT = Op.getValueType();
5200 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5201 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5202
5203 if (isFP) {
5204 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00005205 MVT VT0 = Op0.getValueType();
5206 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5207 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00005208 bool Swap = false;
5209
5210 switch (SetCCOpcode) {
5211 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005212 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00005213 case ISD::SETEQ: SSECC = 0; break;
5214 case ISD::SETOGT:
5215 case ISD::SETGT: Swap = true; // Fallthrough
5216 case ISD::SETLT:
5217 case ISD::SETOLT: SSECC = 1; break;
5218 case ISD::SETOGE:
5219 case ISD::SETGE: Swap = true; // Fallthrough
5220 case ISD::SETLE:
5221 case ISD::SETOLE: SSECC = 2; break;
5222 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005223 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00005224 case ISD::SETNE: SSECC = 4; break;
5225 case ISD::SETULE: Swap = true;
5226 case ISD::SETUGE: SSECC = 5; break;
5227 case ISD::SETULT: Swap = true;
5228 case ISD::SETUGT: SSECC = 6; break;
5229 case ISD::SETO: SSECC = 7; break;
5230 }
5231 if (Swap)
5232 std::swap(Op0, Op1);
5233
Nate Begeman6357f9d2008-07-25 19:05:58 +00005234 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00005235 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00005236 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005237 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005238 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5239 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5240 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
5241 }
5242 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005243 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005244 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5245 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5246 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
5247 }
5248 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00005249 }
5250 // Handle all other FP comparisons here.
5251 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
5252 }
5253
5254 // We are handling one of the integer comparisons here. Since SSE only has
5255 // GT and EQ comparisons for integer, swapping operands and multiple
5256 // operations may be required for some comparisons.
5257 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5258 bool Swap = false, Invert = false, FlipSigns = false;
5259
5260 switch (VT.getSimpleVT()) {
5261 default: break;
5262 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5263 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5264 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5265 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5266 }
5267
5268 switch (SetCCOpcode) {
5269 default: break;
5270 case ISD::SETNE: Invert = true;
5271 case ISD::SETEQ: Opc = EQOpc; break;
5272 case ISD::SETLT: Swap = true;
5273 case ISD::SETGT: Opc = GTOpc; break;
5274 case ISD::SETGE: Swap = true;
5275 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5276 case ISD::SETULT: Swap = true;
5277 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5278 case ISD::SETUGE: Swap = true;
5279 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5280 }
5281 if (Swap)
5282 std::swap(Op0, Op1);
5283
5284 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5285 // bits of the inputs before performing those operations.
5286 if (FlipSigns) {
5287 MVT EltVT = VT.getVectorElementType();
Duncan Sands505ba942009-02-01 18:06:53 +00005288 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5289 EltVT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005290 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
5291 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005292 SignBits.size());
5293 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
5294 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
5295 }
5296
Dan Gohman8181bd12008-07-27 21:46:04 +00005297 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005298
5299 // If the logical-not of the result is required, perform that now.
Bob Wilson81a42cf2009-01-22 17:39:32 +00005300 if (Invert)
Bill Wendlingfcfb47d2009-01-30 23:03:19 +00005301 Result = DAG.getNOT(Op.getDebugLoc(), Result, VT);
Bob Wilson81a42cf2009-01-22 17:39:32 +00005302
Nate Begeman03605a02008-07-17 16:51:19 +00005303 return Result;
5304}
Evan Cheng950aac02007-09-25 01:57:46 +00005305
Evan Chengd580f022008-12-03 08:38:43 +00005306// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
5307static bool isX86LogicalCmp(unsigned Opc) {
5308 return Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI;
5309}
5310
Dan Gohman8181bd12008-07-27 21:46:04 +00005311SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005312 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005313 SDValue Cond = Op.getOperand(0);
5314 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005315
5316 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005317 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005318
Evan Cheng50d37ab2007-10-08 22:16:29 +00005319 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5320 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005321 if (Cond.getOpcode() == X86ISD::SETCC) {
5322 CC = Cond.getOperand(0);
5323
Dan Gohman8181bd12008-07-27 21:46:04 +00005324 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005326 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005327
Evan Cheng50d37ab2007-10-08 22:16:29 +00005328 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005329 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005330 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005331 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005332
Dan Gohman22cefb02009-01-29 01:59:02 +00005333 if ((isX86LogicalCmp(Opc) && !IllegalFPCMov) || Opc == X86ISD::BT) { // FIXME
Evan Cheng50d37ab2007-10-08 22:16:29 +00005334 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005335 addTest = false;
5336 }
5337 }
5338
5339 if (addTest) {
5340 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00005341 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005342 }
5343
Duncan Sands92c43912008-06-06 12:08:01 +00005344 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005345 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005346 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005347 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5348 // condition is true.
5349 Ops.push_back(Op.getOperand(2));
5350 Ops.push_back(Op.getOperand(1));
5351 Ops.push_back(CC);
5352 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00005353 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005354}
5355
Evan Chengd580f022008-12-03 08:38:43 +00005356// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5357// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5358// from the AND / OR.
5359static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5360 Opc = Op.getOpcode();
5361 if (Opc != ISD::OR && Opc != ISD::AND)
5362 return false;
5363 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5364 Op.getOperand(0).hasOneUse() &&
5365 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5366 Op.getOperand(1).hasOneUse());
5367}
5368
Evan Cheng67f98b12009-02-02 08:19:07 +00005369// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5370// 1 and that the SETCC node has a single use.
Evan Cheng8c3af2c2009-02-02 08:07:36 +00005371static bool isXor1OfSetCC(SDValue Op) {
5372 if (Op.getOpcode() != ISD::XOR)
5373 return false;
5374 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5375 if (N1C && N1C->getAPIntValue() == 1) {
5376 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5377 Op.getOperand(0).hasOneUse();
5378 }
5379 return false;
5380}
5381
Dan Gohman8181bd12008-07-27 21:46:04 +00005382SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005383 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005384 SDValue Chain = Op.getOperand(0);
5385 SDValue Cond = Op.getOperand(1);
5386 SDValue Dest = Op.getOperand(2);
5387 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005388
5389 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005390 Cond = LowerSETCC(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005391#if 0
5392 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingf5399032008-12-12 21:15:41 +00005393 else if (Cond.getOpcode() == X86ISD::ADD ||
5394 Cond.getOpcode() == X86ISD::SUB ||
5395 Cond.getOpcode() == X86ISD::SMUL ||
5396 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling7e04be62008-12-09 22:08:41 +00005397 Cond = LowerXALUO(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005398#endif
5399
Evan Cheng50d37ab2007-10-08 22:16:29 +00005400 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5401 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005402 if (Cond.getOpcode() == X86ISD::SETCC) {
5403 CC = Cond.getOperand(0);
5404
Dan Gohman8181bd12008-07-27 21:46:04 +00005405 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005406 unsigned Opc = Cmp.getOpcode();
Chris Lattner77a62312008-12-25 05:34:37 +00005407 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
5408 if (isX86LogicalCmp(Opc) || Opc == X86ISD::BT) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005409 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005410 addTest = false;
Bill Wendlingd3511522008-12-02 01:06:39 +00005411 } else {
Evan Chengd580f022008-12-03 08:38:43 +00005412 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling809e7bd2008-12-03 08:32:02 +00005413 default: break;
5414 case X86::COND_O:
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005415 case X86::COND_B:
Chris Lattner77a62312008-12-25 05:34:37 +00005416 // These can only come from an arithmetic instruction with overflow,
5417 // e.g. SADDO, UADDO.
Bill Wendling809e7bd2008-12-03 08:32:02 +00005418 Cond = Cond.getNode()->getOperand(1);
5419 addTest = false;
5420 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00005421 }
Evan Cheng950aac02007-09-25 01:57:46 +00005422 }
Evan Chengd580f022008-12-03 08:38:43 +00005423 } else {
5424 unsigned CondOpc;
5425 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5426 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5427 unsigned Opc = Cmp.getOpcode();
5428 if (CondOpc == ISD::OR) {
5429 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5430 // two branches instead of an explicit OR instruction with a
5431 // separate test.
5432 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5433 isX86LogicalCmp(Opc)) {
5434 CC = Cond.getOperand(0).getOperand(0);
5435 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5436 Chain, Dest, CC, Cmp);
5437 CC = Cond.getOperand(1).getOperand(0);
5438 Cond = Cmp;
5439 addTest = false;
5440 }
5441 } else { // ISD::AND
5442 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5443 // two branches instead of an explicit AND instruction with a
5444 // separate test. However, we only do this if this block doesn't
5445 // have a fall-through edge, because this requires an explicit
5446 // jmp when the condition is false.
5447 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5448 isX86LogicalCmp(Opc) &&
5449 Op.getNode()->hasOneUse()) {
5450 X86::CondCode CCode =
5451 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5452 CCode = X86::GetOppositeBranchCondition(CCode);
5453 CC = DAG.getConstant(CCode, MVT::i8);
5454 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5455 // Look for an unconditional branch following this conditional branch.
5456 // We need this because we need to reverse the successors in order
5457 // to implement FCMP_OEQ.
5458 if (User.getOpcode() == ISD::BR) {
5459 SDValue FalseBB = User.getOperand(1);
5460 SDValue NewBR =
5461 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5462 assert(NewBR == User);
5463 Dest = FalseBB;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005464
Evan Chengd580f022008-12-03 08:38:43 +00005465 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5466 Chain, Dest, CC, Cmp);
5467 X86::CondCode CCode =
5468 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5469 CCode = X86::GetOppositeBranchCondition(CCode);
5470 CC = DAG.getConstant(CCode, MVT::i8);
5471 Cond = Cmp;
5472 addTest = false;
5473 }
5474 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005475 }
Evan Cheng8c3af2c2009-02-02 08:07:36 +00005476 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5477 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5478 // It should be transformed during dag combiner except when the condition
5479 // is set by a arithmetics with overflow node.
5480 X86::CondCode CCode =
5481 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5482 CCode = X86::GetOppositeBranchCondition(CCode);
5483 CC = DAG.getConstant(CCode, MVT::i8);
5484 Cond = Cond.getOperand(0).getOperand(1);
5485 addTest = false;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005486 }
Evan Cheng950aac02007-09-25 01:57:46 +00005487 }
5488
5489 if (addTest) {
5490 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005491 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005492 }
Evan Cheng621216e2007-09-29 00:00:36 +00005493 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005494 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005495}
5496
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005497
5498// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5499// Calls to _alloca is needed to probe the stack when allocating more than 4k
5500// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5501// that the guard pages used by the OS virtual memory manager are allocated in
5502// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005503SDValue
5504X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005505 SelectionDAG &DAG) {
5506 assert(Subtarget->isTargetCygMing() &&
5507 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005508
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005509 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005510 SDValue Chain = Op.getOperand(0);
5511 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005512 // FIXME: Ensure alignment here
5513
Dan Gohman8181bd12008-07-27 21:46:04 +00005514 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005515
Duncan Sands92c43912008-06-06 12:08:01 +00005516 MVT IntPtr = getPointerTy();
5517 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005518
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005519 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005520
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005521 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5522 Flag = Chain.getValue(1);
5523
5524 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005525 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005526 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005527 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005528 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005529 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005530 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005531 Flag = Chain.getValue(1);
5532
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005533 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005534 DAG.getIntPtrConstant(0, true),
5535 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005536 Flag);
5537
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005538 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005539
Dan Gohman8181bd12008-07-27 21:46:04 +00005540 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005541 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005542}
5543
Dan Gohman8181bd12008-07-27 21:46:04 +00005544SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005545X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005546 SDValue Chain,
5547 SDValue Dst, SDValue Src,
5548 SDValue Size, unsigned Align,
5549 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005550 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005551 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005552
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005553 // If not DWORD aligned or size is more than the threshold, call the library.
5554 // The libc version is likely to be faster for these cases. It can use the
5555 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005556 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005557 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005558 ConstantSize->getZExtValue() >
5559 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005560 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005561
5562 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005563 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005564
Bill Wendling4b2e3782008-10-01 00:59:58 +00005565 if (const char *bzeroEntry = V &&
5566 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5567 MVT IntPtr = getPointerTy();
5568 const Type *IntPtrTy = TD->getIntPtrType();
5569 TargetLowering::ArgListTy Args;
5570 TargetLowering::ArgListEntry Entry;
5571 Entry.Node = Dst;
5572 Entry.Ty = IntPtrTy;
5573 Args.push_back(Entry);
5574 Entry.Node = Size;
5575 Args.push_back(Entry);
Dale Johannesenca6237b2009-01-30 23:10:59 +00005576 // FIXME provide DebugLoc info
Bill Wendling4b2e3782008-10-01 00:59:58 +00005577 std::pair<SDValue,SDValue> CallResult =
5578 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5579 CallingConv::C, false,
Dale Johannesenca6237b2009-01-30 23:10:59 +00005580 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG,
5581 DebugLoc::getUnknownLoc());
Bill Wendling4b2e3782008-10-01 00:59:58 +00005582 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005583 }
5584
Dan Gohmane8b391e2008-04-12 04:36:06 +00005585 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005586 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005587 }
5588
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005589 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005590 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005591 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005592 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005593 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005594 unsigned BytesLeft = 0;
5595 bool TwoRepStos = false;
5596 if (ValC) {
5597 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005598 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005599
5600 // If the value is a constant, then we can potentially use larger sets.
5601 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005602 case 2: // WORD aligned
5603 AVT = MVT::i16;
5604 ValReg = X86::AX;
5605 Val = (Val << 8) | Val;
5606 break;
5607 case 0: // DWORD aligned
5608 AVT = MVT::i32;
5609 ValReg = X86::EAX;
5610 Val = (Val << 8) | Val;
5611 Val = (Val << 16) | Val;
5612 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5613 AVT = MVT::i64;
5614 ValReg = X86::RAX;
5615 Val = (Val << 32) | Val;
5616 }
5617 break;
5618 default: // Byte aligned
5619 AVT = MVT::i8;
5620 ValReg = X86::AL;
5621 Count = DAG.getIntPtrConstant(SizeVal);
5622 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005623 }
5624
Duncan Sandsec142ee2008-06-08 20:54:56 +00005625 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005626 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005627 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5628 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005629 }
5630
5631 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5632 InFlag);
5633 InFlag = Chain.getValue(1);
5634 } else {
5635 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005636 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005637 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005638 InFlag = Chain.getValue(1);
5639 }
5640
5641 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5642 Count, InFlag);
5643 InFlag = Chain.getValue(1);
5644 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005645 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005646 InFlag = Chain.getValue(1);
5647
5648 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005649 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005650 Ops.push_back(Chain);
5651 Ops.push_back(DAG.getValueType(AVT));
5652 Ops.push_back(InFlag);
5653 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5654
5655 if (TwoRepStos) {
5656 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005657 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005658 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005659 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005660 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5661 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5662 Left, InFlag);
5663 InFlag = Chain.getValue(1);
5664 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5665 Ops.clear();
5666 Ops.push_back(Chain);
5667 Ops.push_back(DAG.getValueType(MVT::i8));
5668 Ops.push_back(InFlag);
5669 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5670 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005671 // Handle the last 1 - 7 bytes.
5672 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005673 MVT AddrVT = Dst.getValueType();
5674 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005675
5676 Chain = DAG.getMemset(Chain,
5677 DAG.getNode(ISD::ADD, AddrVT, Dst,
5678 DAG.getConstant(Offset, AddrVT)),
5679 Src,
5680 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005681 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005682 }
5683
Dan Gohmane8b391e2008-04-12 04:36:06 +00005684 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005685 return Chain;
5686}
5687
Dan Gohman8181bd12008-07-27 21:46:04 +00005688SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005689X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005690 SDValue Chain, SDValue Dst, SDValue Src,
5691 SDValue Size, unsigned Align,
5692 bool AlwaysInline,
5693 const Value *DstSV, uint64_t DstSVOff,
5694 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005695 // This requires the copy size to be a constant, preferrably
5696 // within a subtarget-specific limit.
5697 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5698 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005699 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005700 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005701 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005702 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005703
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005704 /// If not DWORD aligned, call the library.
5705 if ((Align & 3) != 0)
5706 return SDValue();
5707
5708 // DWORD aligned
5709 MVT AVT = MVT::i32;
5710 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005711 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005712
Duncan Sands92c43912008-06-06 12:08:01 +00005713 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005714 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005715 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005716 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005717
Dan Gohman8181bd12008-07-27 21:46:04 +00005718 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005719 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5720 Count, InFlag);
5721 InFlag = Chain.getValue(1);
5722 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005723 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005724 InFlag = Chain.getValue(1);
5725 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005726 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005727 InFlag = Chain.getValue(1);
5728
5729 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005730 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005731 Ops.push_back(Chain);
5732 Ops.push_back(DAG.getValueType(AVT));
5733 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005734 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005735
Dan Gohman8181bd12008-07-27 21:46:04 +00005736 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005737 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005738 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005739 // Handle the last 1 - 7 bytes.
5740 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005741 MVT DstVT = Dst.getValueType();
5742 MVT SrcVT = Src.getValueType();
5743 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005744 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005745 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005746 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005747 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005748 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005749 DAG.getConstant(BytesLeft, SizeVT),
5750 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005751 DstSV, DstSVOff + Offset,
5752 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005753 }
5754
Dan Gohmane8b391e2008-04-12 04:36:06 +00005755 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005756}
5757
Dan Gohman8181bd12008-07-27 21:46:04 +00005758SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005759 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005760
5761 if (!Subtarget->is64Bit()) {
5762 // vastart just stores the address of the VarArgsFrameIndex slot into the
5763 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005764 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005765 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005766 }
5767
5768 // __va_list_tag:
5769 // gp_offset (0 - 6 * 8)
5770 // fp_offset (48 - 48 + 8 * 16)
5771 // overflow_arg_area (point to parameters coming in memory).
5772 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005773 SmallVector<SDValue, 8> MemOps;
5774 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005775 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005776 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005777 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005778 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005779 MemOps.push_back(Store);
5780
5781 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005782 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005783 Store = DAG.getStore(Op.getOperand(0),
5784 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005785 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005786 MemOps.push_back(Store);
5787
5788 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005789 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005790 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005791 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005792 MemOps.push_back(Store);
5793
5794 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005795 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005796 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005797 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005798 MemOps.push_back(Store);
5799 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5800}
5801
Dan Gohman8181bd12008-07-27 21:46:04 +00005802SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005803 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5804 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005805 SDValue Chain = Op.getOperand(0);
5806 SDValue SrcPtr = Op.getOperand(1);
5807 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005808
5809 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5810 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005811 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005812}
5813
Dan Gohman8181bd12008-07-27 21:46:04 +00005814SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005815 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005816 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005817 SDValue Chain = Op.getOperand(0);
5818 SDValue DstPtr = Op.getOperand(1);
5819 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005820 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5821 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005822
Dan Gohman840ff5c2008-04-18 20:55:41 +00005823 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5824 DAG.getIntPtrConstant(24), 8, false,
5825 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005826}
5827
Dan Gohman8181bd12008-07-27 21:46:04 +00005828SDValue
5829X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005830 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005831 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005832 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005833 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005834 case Intrinsic::x86_sse_comieq_ss:
5835 case Intrinsic::x86_sse_comilt_ss:
5836 case Intrinsic::x86_sse_comile_ss:
5837 case Intrinsic::x86_sse_comigt_ss:
5838 case Intrinsic::x86_sse_comige_ss:
5839 case Intrinsic::x86_sse_comineq_ss:
5840 case Intrinsic::x86_sse_ucomieq_ss:
5841 case Intrinsic::x86_sse_ucomilt_ss:
5842 case Intrinsic::x86_sse_ucomile_ss:
5843 case Intrinsic::x86_sse_ucomigt_ss:
5844 case Intrinsic::x86_sse_ucomige_ss:
5845 case Intrinsic::x86_sse_ucomineq_ss:
5846 case Intrinsic::x86_sse2_comieq_sd:
5847 case Intrinsic::x86_sse2_comilt_sd:
5848 case Intrinsic::x86_sse2_comile_sd:
5849 case Intrinsic::x86_sse2_comigt_sd:
5850 case Intrinsic::x86_sse2_comige_sd:
5851 case Intrinsic::x86_sse2_comineq_sd:
5852 case Intrinsic::x86_sse2_ucomieq_sd:
5853 case Intrinsic::x86_sse2_ucomilt_sd:
5854 case Intrinsic::x86_sse2_ucomile_sd:
5855 case Intrinsic::x86_sse2_ucomigt_sd:
5856 case Intrinsic::x86_sse2_ucomige_sd:
5857 case Intrinsic::x86_sse2_ucomineq_sd: {
5858 unsigned Opc = 0;
5859 ISD::CondCode CC = ISD::SETCC_INVALID;
5860 switch (IntNo) {
5861 default: break;
5862 case Intrinsic::x86_sse_comieq_ss:
5863 case Intrinsic::x86_sse2_comieq_sd:
5864 Opc = X86ISD::COMI;
5865 CC = ISD::SETEQ;
5866 break;
5867 case Intrinsic::x86_sse_comilt_ss:
5868 case Intrinsic::x86_sse2_comilt_sd:
5869 Opc = X86ISD::COMI;
5870 CC = ISD::SETLT;
5871 break;
5872 case Intrinsic::x86_sse_comile_ss:
5873 case Intrinsic::x86_sse2_comile_sd:
5874 Opc = X86ISD::COMI;
5875 CC = ISD::SETLE;
5876 break;
5877 case Intrinsic::x86_sse_comigt_ss:
5878 case Intrinsic::x86_sse2_comigt_sd:
5879 Opc = X86ISD::COMI;
5880 CC = ISD::SETGT;
5881 break;
5882 case Intrinsic::x86_sse_comige_ss:
5883 case Intrinsic::x86_sse2_comige_sd:
5884 Opc = X86ISD::COMI;
5885 CC = ISD::SETGE;
5886 break;
5887 case Intrinsic::x86_sse_comineq_ss:
5888 case Intrinsic::x86_sse2_comineq_sd:
5889 Opc = X86ISD::COMI;
5890 CC = ISD::SETNE;
5891 break;
5892 case Intrinsic::x86_sse_ucomieq_ss:
5893 case Intrinsic::x86_sse2_ucomieq_sd:
5894 Opc = X86ISD::UCOMI;
5895 CC = ISD::SETEQ;
5896 break;
5897 case Intrinsic::x86_sse_ucomilt_ss:
5898 case Intrinsic::x86_sse2_ucomilt_sd:
5899 Opc = X86ISD::UCOMI;
5900 CC = ISD::SETLT;
5901 break;
5902 case Intrinsic::x86_sse_ucomile_ss:
5903 case Intrinsic::x86_sse2_ucomile_sd:
5904 Opc = X86ISD::UCOMI;
5905 CC = ISD::SETLE;
5906 break;
5907 case Intrinsic::x86_sse_ucomigt_ss:
5908 case Intrinsic::x86_sse2_ucomigt_sd:
5909 Opc = X86ISD::UCOMI;
5910 CC = ISD::SETGT;
5911 break;
5912 case Intrinsic::x86_sse_ucomige_ss:
5913 case Intrinsic::x86_sse2_ucomige_sd:
5914 Opc = X86ISD::UCOMI;
5915 CC = ISD::SETGE;
5916 break;
5917 case Intrinsic::x86_sse_ucomineq_ss:
5918 case Intrinsic::x86_sse2_ucomineq_sd:
5919 Opc = X86ISD::UCOMI;
5920 CC = ISD::SETNE;
5921 break;
5922 }
5923
Dan Gohman8181bd12008-07-27 21:46:04 +00005924 SDValue LHS = Op.getOperand(1);
5925 SDValue RHS = Op.getOperand(2);
Chris Lattnerebb91142008-12-24 23:53:05 +00005926 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman8181bd12008-07-27 21:46:04 +00005927 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5928 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005929 DAG.getConstant(X86CC, MVT::i8), Cond);
5930 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005931 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005932
5933 // Fix vector shift instructions where the last operand is a non-immediate
5934 // i32 value.
5935 case Intrinsic::x86_sse2_pslli_w:
5936 case Intrinsic::x86_sse2_pslli_d:
5937 case Intrinsic::x86_sse2_pslli_q:
5938 case Intrinsic::x86_sse2_psrli_w:
5939 case Intrinsic::x86_sse2_psrli_d:
5940 case Intrinsic::x86_sse2_psrli_q:
5941 case Intrinsic::x86_sse2_psrai_w:
5942 case Intrinsic::x86_sse2_psrai_d:
5943 case Intrinsic::x86_mmx_pslli_w:
5944 case Intrinsic::x86_mmx_pslli_d:
5945 case Intrinsic::x86_mmx_pslli_q:
5946 case Intrinsic::x86_mmx_psrli_w:
5947 case Intrinsic::x86_mmx_psrli_d:
5948 case Intrinsic::x86_mmx_psrli_q:
5949 case Intrinsic::x86_mmx_psrai_w:
5950 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005951 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005952 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005953 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005954
5955 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005956 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005957 switch (IntNo) {
5958 case Intrinsic::x86_sse2_pslli_w:
5959 NewIntNo = Intrinsic::x86_sse2_psll_w;
5960 break;
5961 case Intrinsic::x86_sse2_pslli_d:
5962 NewIntNo = Intrinsic::x86_sse2_psll_d;
5963 break;
5964 case Intrinsic::x86_sse2_pslli_q:
5965 NewIntNo = Intrinsic::x86_sse2_psll_q;
5966 break;
5967 case Intrinsic::x86_sse2_psrli_w:
5968 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5969 break;
5970 case Intrinsic::x86_sse2_psrli_d:
5971 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5972 break;
5973 case Intrinsic::x86_sse2_psrli_q:
5974 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5975 break;
5976 case Intrinsic::x86_sse2_psrai_w:
5977 NewIntNo = Intrinsic::x86_sse2_psra_w;
5978 break;
5979 case Intrinsic::x86_sse2_psrai_d:
5980 NewIntNo = Intrinsic::x86_sse2_psra_d;
5981 break;
5982 default: {
5983 ShAmtVT = MVT::v2i32;
5984 switch (IntNo) {
5985 case Intrinsic::x86_mmx_pslli_w:
5986 NewIntNo = Intrinsic::x86_mmx_psll_w;
5987 break;
5988 case Intrinsic::x86_mmx_pslli_d:
5989 NewIntNo = Intrinsic::x86_mmx_psll_d;
5990 break;
5991 case Intrinsic::x86_mmx_pslli_q:
5992 NewIntNo = Intrinsic::x86_mmx_psll_q;
5993 break;
5994 case Intrinsic::x86_mmx_psrli_w:
5995 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5996 break;
5997 case Intrinsic::x86_mmx_psrli_d:
5998 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5999 break;
6000 case Intrinsic::x86_mmx_psrli_q:
6001 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6002 break;
6003 case Intrinsic::x86_mmx_psrai_w:
6004 NewIntNo = Intrinsic::x86_mmx_psra_w;
6005 break;
6006 case Intrinsic::x86_mmx_psrai_d:
6007 NewIntNo = Intrinsic::x86_mmx_psra_d;
6008 break;
6009 default: abort(); // Can't reach here.
6010 }
6011 break;
6012 }
6013 }
Duncan Sands92c43912008-06-06 12:08:01 +00006014 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006015 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
6016 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
6017 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6018 DAG.getConstant(NewIntNo, MVT::i32),
6019 Op.getOperand(1), ShAmt);
6020 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006021 }
6022}
6023
Dan Gohman8181bd12008-07-27 21:46:04 +00006024SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006025 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6026
6027 if (Depth > 0) {
6028 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6029 SDValue Offset =
6030 DAG.getConstant(TD->getPointerSize(),
6031 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
6032 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(),
6033 DAG.getNode(ISD::ADD, getPointerTy(), FrameAddr, Offset),
6034 NULL, 0);
6035 }
6036
6037 // Just load the return address.
Dan Gohman8181bd12008-07-27 21:46:04 +00006038 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006039 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
6040}
6041
Dan Gohman8181bd12008-07-27 21:46:04 +00006042SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00006043 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6044 MFI->setFrameAddressIsTaken(true);
6045 MVT VT = Op.getValueType();
6046 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6047 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
6048 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), FrameReg, VT);
6049 while (Depth--)
6050 FrameAddr = DAG.getLoad(VT, DAG.getEntryNode(), FrameAddr, NULL, 0);
6051 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006052}
6053
Dan Gohman8181bd12008-07-27 21:46:04 +00006054SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00006055 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006056 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006057}
6058
Dan Gohman8181bd12008-07-27 21:46:04 +00006059SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006060{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006061 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00006062 SDValue Chain = Op.getOperand(0);
6063 SDValue Offset = Op.getOperand(1);
6064 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006065
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006066 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6067 getPointerTy());
6068 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006069
Dan Gohman8181bd12008-07-27 21:46:04 +00006070 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006071 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006072 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
6073 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006074 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
6075 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006076
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006077 return DAG.getNode(X86ISD::EH_RETURN,
6078 MVT::Other,
6079 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006080}
6081
Dan Gohman8181bd12008-07-27 21:46:04 +00006082SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006083 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006084 SDValue Root = Op.getOperand(0);
6085 SDValue Trmp = Op.getOperand(1); // trampoline
6086 SDValue FPtr = Op.getOperand(2); // nested function
6087 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006088
Dan Gohman12a9c082008-02-06 22:27:42 +00006089 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006090
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006091 const X86InstrInfo *TII =
6092 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6093
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006094 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006095 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006096
6097 // Large code-model.
6098
6099 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6100 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6101
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006102 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6103 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006104
6105 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6106
6107 // Load the pointer to the nested function into R11.
6108 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00006109 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006110 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006111 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006112
6113 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00006114 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006115
6116 // Load the 'nest' parameter value into R10.
6117 // R10 is specified in X86CallingConv.td
6118 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
6119 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
6120 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006121 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006122
6123 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00006124 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006125
6126 // Jump to the nested function.
6127 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
6128 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
6129 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006130 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006131
6132 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
6133 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
6134 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006135 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006136
Dan Gohman8181bd12008-07-27 21:46:04 +00006137 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006138 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00006139 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006140 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00006141 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006142 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6143 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00006144 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006145
6146 switch (CC) {
6147 default:
6148 assert(0 && "Unsupported calling convention");
6149 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006150 case CallingConv::X86_StdCall: {
6151 // Pass 'nest' parameter in ECX.
6152 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006153 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006154
6155 // Check that ECX wasn't needed by an 'inreg' parameter.
6156 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00006157 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006158
Chris Lattner1c8733e2008-03-12 17:45:29 +00006159 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006160 unsigned InRegCount = 0;
6161 unsigned Idx = 1;
6162
6163 for (FunctionType::param_iterator I = FTy->param_begin(),
6164 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00006165 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006166 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006167 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006168
6169 if (InRegCount > 2) {
6170 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6171 abort();
6172 }
6173 }
6174 break;
6175 }
6176 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00006177 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006178 // Pass 'nest' parameter in EAX.
6179 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006180 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006181 break;
6182 }
6183
Dan Gohman8181bd12008-07-27 21:46:04 +00006184 SDValue OutChains[4];
6185 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006186
6187 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
6188 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
6189
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006190 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006191 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00006192 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00006193 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006194
6195 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006196 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006197
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006198 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006199 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
6200 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006201 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006202
6203 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006204 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006205
Dan Gohman8181bd12008-07-27 21:46:04 +00006206 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00006207 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00006208 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006209 }
6210}
6211
Dan Gohman8181bd12008-07-27 21:46:04 +00006212SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006213 /*
6214 The rounding mode is in bits 11:10 of FPSR, and has the following
6215 settings:
6216 00 Round to nearest
6217 01 Round to -inf
6218 10 Round to +inf
6219 11 Round to 0
6220
6221 FLT_ROUNDS, on the other hand, expects the following:
6222 -1 Undefined
6223 0 Round to 0
6224 1 Round to nearest
6225 2 Round to +inf
6226 3 Round to -inf
6227
6228 To perform the conversion, we do:
6229 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6230 */
6231
6232 MachineFunction &MF = DAG.getMachineFunction();
6233 const TargetMachine &TM = MF.getTarget();
6234 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6235 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00006236 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006237
6238 // Save FP Control Word to stack slot
6239 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00006240 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006241
Dan Gohman8181bd12008-07-27 21:46:04 +00006242 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00006243 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006244
6245 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00006246 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006247
6248 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00006249 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006250 DAG.getNode(ISD::SRL, MVT::i16,
6251 DAG.getNode(ISD::AND, MVT::i16,
6252 CWD, DAG.getConstant(0x800, MVT::i16)),
6253 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006254 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006255 DAG.getNode(ISD::SRL, MVT::i16,
6256 DAG.getNode(ISD::AND, MVT::i16,
6257 CWD, DAG.getConstant(0x400, MVT::i16)),
6258 DAG.getConstant(9, MVT::i8));
6259
Dan Gohman8181bd12008-07-27 21:46:04 +00006260 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006261 DAG.getNode(ISD::AND, MVT::i16,
6262 DAG.getNode(ISD::ADD, MVT::i16,
6263 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
6264 DAG.getConstant(1, MVT::i16)),
6265 DAG.getConstant(3, MVT::i16));
6266
6267
Duncan Sands92c43912008-06-06 12:08:01 +00006268 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006269 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6270}
6271
Dan Gohman8181bd12008-07-27 21:46:04 +00006272SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006273 MVT VT = Op.getValueType();
6274 MVT OpVT = VT;
6275 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006276
6277 Op = Op.getOperand(0);
6278 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006279 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00006280 OpVT = MVT::i32;
6281 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6282 }
Evan Cheng48679f42007-12-14 02:13:44 +00006283
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006284 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6285 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6286 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
6287
6288 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006289 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006290 Ops.push_back(Op);
6291 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6292 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6293 Ops.push_back(Op.getValue(1));
6294 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6295
6296 // Finally xor with NumBits-1.
6297 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
6298
Evan Cheng48679f42007-12-14 02:13:44 +00006299 if (VT == MVT::i8)
6300 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6301 return Op;
6302}
6303
Dan Gohman8181bd12008-07-27 21:46:04 +00006304SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006305 MVT VT = Op.getValueType();
6306 MVT OpVT = VT;
6307 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006308
6309 Op = Op.getOperand(0);
6310 if (VT == MVT::i8) {
6311 OpVT = MVT::i32;
6312 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6313 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006314
6315 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6316 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6317 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
6318
6319 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006320 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006321 Ops.push_back(Op);
6322 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6323 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6324 Ops.push_back(Op.getValue(1));
6325 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6326
Evan Cheng48679f42007-12-14 02:13:44 +00006327 if (VT == MVT::i8)
6328 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6329 return Op;
6330}
6331
Mon P Wang14edb092008-12-18 21:42:19 +00006332SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6333 MVT VT = Op.getValueType();
6334 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
6335
6336 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6337 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6338 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6339 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6340 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6341 //
6342 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6343 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6344 // return AloBlo + AloBhi + AhiBlo;
6345
6346 SDValue A = Op.getOperand(0);
6347 SDValue B = Op.getOperand(1);
6348
6349 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6350 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6351 A, DAG.getConstant(32, MVT::i32));
6352 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6353 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6354 B, DAG.getConstant(32, MVT::i32));
6355 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6356 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6357 A, B);
6358 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6359 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6360 A, Bhi);
6361 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6362 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6363 Ahi, B);
6364 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6365 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6366 AloBhi, DAG.getConstant(32, MVT::i32));
6367 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6368 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6369 AhiBlo, DAG.getConstant(32, MVT::i32));
6370 SDValue Res = DAG.getNode(ISD::ADD, VT, AloBlo, AloBhi);
6371 Res = DAG.getNode(ISD::ADD, VT, Res, AhiBlo);
6372 return Res;
6373}
6374
6375
Bill Wendling7e04be62008-12-09 22:08:41 +00006376SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6377 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6378 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendlingd3511522008-12-02 01:06:39 +00006379 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6380 // has only one use.
Bill Wendlingd06b4202008-11-26 22:37:40 +00006381 SDNode *N = Op.getNode();
Bill Wendlingd3511522008-12-02 01:06:39 +00006382 SDValue LHS = N->getOperand(0);
6383 SDValue RHS = N->getOperand(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00006384 unsigned BaseOp = 0;
6385 unsigned Cond = 0;
6386
6387 switch (Op.getOpcode()) {
6388 default: assert(0 && "Unknown ovf instruction!");
6389 case ISD::SADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006390 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00006391 Cond = X86::COND_O;
6392 break;
6393 case ISD::UADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006394 BaseOp = X86ISD::ADD;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006395 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006396 break;
6397 case ISD::SSUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006398 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00006399 Cond = X86::COND_O;
6400 break;
6401 case ISD::USUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006402 BaseOp = X86ISD::SUB;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006403 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006404 break;
6405 case ISD::SMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006406 BaseOp = X86ISD::SMUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00006407 Cond = X86::COND_O;
6408 break;
6409 case ISD::UMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006410 BaseOp = X86ISD::UMUL;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006411 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006412 break;
6413 }
Bill Wendlingd06b4202008-11-26 22:37:40 +00006414
Bill Wendlingd3511522008-12-02 01:06:39 +00006415 // Also sets EFLAGS.
6416 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Bill Wendling7e04be62008-12-09 22:08:41 +00006417 SDValue Sum = DAG.getNode(BaseOp, VTs, LHS, RHS);
Bill Wendlingd06b4202008-11-26 22:37:40 +00006418
Bill Wendlingd3511522008-12-02 01:06:39 +00006419 SDValue SetCC =
6420 DAG.getNode(X86ISD::SETCC, N->getValueType(1),
Bill Wendling35f1a9d2008-12-10 02:01:32 +00006421 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendlingd06b4202008-11-26 22:37:40 +00006422
Bill Wendlingd3511522008-12-02 01:06:39 +00006423 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6424 return Sum;
Bill Wendling4c134df2008-11-24 19:21:46 +00006425}
6426
Dan Gohman8181bd12008-07-27 21:46:04 +00006427SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006428 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00006429 unsigned Reg = 0;
6430 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006431 switch(T.getSimpleVT()) {
6432 default:
6433 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006434 case MVT::i8: Reg = X86::AL; size = 1; break;
6435 case MVT::i16: Reg = X86::AX; size = 2; break;
6436 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00006437 case MVT::i64:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006438 assert(Subtarget->is64Bit() && "Node not type legal!");
6439 Reg = X86::RAX; size = 8;
Andrew Lenharth81580822008-03-05 01:15:49 +00006440 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00006441 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006442 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00006443 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00006444 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006445 Op.getOperand(1),
6446 Op.getOperand(3),
6447 DAG.getTargetConstant(size, MVT::i8),
6448 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006449 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006450 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
6451 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006452 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
6453 return cpOut;
6454}
6455
Duncan Sands7d9834b2008-12-01 11:39:25 +00006456SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif825aa892008-08-28 23:19:51 +00006457 SelectionDAG &DAG) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006458 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharth81580822008-03-05 01:15:49 +00006459 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006460 SDValue TheChain = Op.getOperand(0);
6461 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6462 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
6463 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX, MVT::i64,
6464 rax.getValue(2));
6465 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
6466 DAG.getConstant(32, MVT::i8));
6467 SDValue Ops[] = {
6468 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp),
6469 rdx.getValue(1)
6470 };
6471 return DAG.getMergeValues(Ops, 2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006472}
6473
Dale Johannesen9011d872008-09-29 22:25:26 +00006474SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6475 SDNode *Node = Op.getNode();
6476 MVT T = Node->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006477 SDValue negOp = DAG.getNode(ISD::SUB, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006478 DAG.getConstant(0, T), Node->getOperand(2));
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006479 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD,
6480 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen9011d872008-09-29 22:25:26 +00006481 Node->getOperand(0),
6482 Node->getOperand(1), negOp,
6483 cast<AtomicSDNode>(Node)->getSrcValue(),
6484 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006485}
6486
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006487/// LowerOperation - Provide custom lowering hooks for some operations.
6488///
Dan Gohman8181bd12008-07-27 21:46:04 +00006489SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006490 switch (Op.getOpcode()) {
6491 default: assert(0 && "Should not custom lower this!");
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006492 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6493 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006494 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6495 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6496 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6497 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6498 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6499 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6500 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6501 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006502 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006503 case ISD::SHL_PARTS:
6504 case ISD::SRA_PARTS:
6505 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6506 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00006507 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006508 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6509 case ISD::FABS: return LowerFABS(Op, DAG);
6510 case ISD::FNEG: return LowerFNEG(Op, DAG);
6511 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006512 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006513 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006514 case ISD::SELECT: return LowerSELECT(Op, DAG);
6515 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006516 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6517 case ISD::CALL: return LowerCALL(Op, DAG);
6518 case ISD::RET: return LowerRET(Op, DAG);
6519 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006520 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006521 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006522 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6523 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6524 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6525 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6526 case ISD::FRAME_TO_ARGS_OFFSET:
6527 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6528 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6529 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006530 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006531 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006532 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6533 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wang14edb092008-12-18 21:42:19 +00006534 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling7e04be62008-12-09 22:08:41 +00006535 case ISD::SADDO:
6536 case ISD::UADDO:
6537 case ISD::SSUBO:
6538 case ISD::USUBO:
6539 case ISD::SMULO:
6540 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006541 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006542 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006543}
6544
Duncan Sands7d9834b2008-12-01 11:39:25 +00006545void X86TargetLowering::
6546ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6547 SelectionDAG &DAG, unsigned NewOp) {
6548 MVT T = Node->getValueType(0);
6549 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6550
6551 SDValue Chain = Node->getOperand(0);
6552 SDValue In1 = Node->getOperand(1);
6553 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6554 Node->getOperand(2), DAG.getIntPtrConstant(0));
6555 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6556 Node->getOperand(2), DAG.getIntPtrConstant(1));
6557 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6558 // have a MemOperand. Pass the info through as a normal operand.
6559 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6560 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6561 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
6562 SDValue Result = DAG.getNode(NewOp, Tys, Ops, 5);
6563 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
6564 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6565 Results.push_back(Result.getValue(2));
6566}
6567
Duncan Sandsac496a12008-07-04 11:47:58 +00006568/// ReplaceNodeResults - Replace a node with an illegal result type
6569/// with a new node built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00006570void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6571 SmallVectorImpl<SDValue>&Results,
6572 SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006573 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006574 default:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006575 assert(false && "Do not know how to custom type legalize this operation!");
6576 return;
6577 case ISD::FP_TO_SINT: {
6578 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
6579 SDValue FIST = Vals.first, StackSlot = Vals.second;
6580 if (FIST.getNode() != 0) {
6581 MVT VT = N->getValueType(0);
6582 // Return a load from the stack slot.
6583 Results.push_back(DAG.getLoad(VT, FIST, StackSlot, NULL, 0));
6584 }
6585 return;
6586 }
6587 case ISD::READCYCLECOUNTER: {
6588 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6589 SDValue TheChain = N->getOperand(0);
6590 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6591 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
6592 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX, MVT::i32,
6593 eax.getValue(2));
6594 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6595 SDValue Ops[] = { eax, edx };
6596 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2));
6597 Results.push_back(edx.getValue(1));
6598 return;
6599 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006600 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006601 MVT T = N->getValueType(0);
6602 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6603 SDValue cpInL, cpInH;
6604 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6605 DAG.getConstant(0, MVT::i32));
6606 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6607 DAG.getConstant(1, MVT::i32));
6608 cpInL = DAG.getCopyToReg(N->getOperand(0), X86::EAX, cpInL, SDValue());
6609 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX, cpInH,
6610 cpInL.getValue(1));
6611 SDValue swapInL, swapInH;
6612 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6613 DAG.getConstant(0, MVT::i32));
6614 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6615 DAG.getConstant(1, MVT::i32));
6616 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX, swapInL,
6617 cpInH.getValue(1));
6618 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX, swapInH,
6619 swapInL.getValue(1));
6620 SDValue Ops[] = { swapInH.getValue(0),
6621 N->getOperand(1),
6622 swapInH.getValue(1) };
6623 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6624 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
6625 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
6626 Result.getValue(1));
6627 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
6628 cpOutL.getValue(2));
6629 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
6630 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6631 Results.push_back(cpOutH.getValue(1));
6632 return;
6633 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006634 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006635 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6636 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006637 case ISD::ATOMIC_LOAD_AND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006638 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6639 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006640 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006641 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6642 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006643 case ISD::ATOMIC_LOAD_OR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006644 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6645 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006646 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006647 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6648 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006649 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006650 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6651 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006652 case ISD::ATOMIC_SWAP:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006653 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6654 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006655 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006656}
6657
6658const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6659 switch (Opcode) {
6660 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006661 case X86ISD::BSF: return "X86ISD::BSF";
6662 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006663 case X86ISD::SHLD: return "X86ISD::SHLD";
6664 case X86ISD::SHRD: return "X86ISD::SHRD";
6665 case X86ISD::FAND: return "X86ISD::FAND";
6666 case X86ISD::FOR: return "X86ISD::FOR";
6667 case X86ISD::FXOR: return "X86ISD::FXOR";
6668 case X86ISD::FSRL: return "X86ISD::FSRL";
6669 case X86ISD::FILD: return "X86ISD::FILD";
6670 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6671 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6672 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6673 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6674 case X86ISD::FLD: return "X86ISD::FLD";
6675 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006676 case X86ISD::CALL: return "X86ISD::CALL";
6677 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6678 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohman7fe9b7f2008-12-23 22:45:23 +00006679 case X86ISD::BT: return "X86ISD::BT";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006680 case X86ISD::CMP: return "X86ISD::CMP";
6681 case X86ISD::COMI: return "X86ISD::COMI";
6682 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6683 case X86ISD::SETCC: return "X86ISD::SETCC";
6684 case X86ISD::CMOV: return "X86ISD::CMOV";
6685 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6686 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6687 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6688 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006689 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6690 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006691 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006692 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006693 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6694 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006695 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6696 case X86ISD::FMAX: return "X86ISD::FMAX";
6697 case X86ISD::FMIN: return "X86ISD::FMIN";
6698 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6699 case X86ISD::FRCP: return "X86ISD::FRCP";
6700 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6701 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6702 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006703 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006704 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006705 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6706 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006707 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6708 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6709 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6710 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6711 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6712 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006713 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6714 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006715 case X86ISD::VSHL: return "X86ISD::VSHL";
6716 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006717 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6718 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6719 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6720 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6721 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6722 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6723 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6724 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6725 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6726 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingae034ed2008-12-12 00:56:36 +00006727 case X86ISD::ADD: return "X86ISD::ADD";
6728 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingf5399032008-12-12 21:15:41 +00006729 case X86ISD::SMUL: return "X86ISD::SMUL";
6730 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006731 }
6732}
6733
6734// isLegalAddressingMode - Return true if the addressing mode represented
6735// by AM is legal for this target, for a load/store of the specified type.
6736bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6737 const Type *Ty) const {
6738 // X86 supports extremely general addressing modes.
6739
6740 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6741 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6742 return false;
6743
6744 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006745 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006746 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6747 return false;
Dale Johannesen64660e92008-12-05 21:47:27 +00006748 // If BaseGV requires a register, we cannot also have a BaseReg.
6749 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6750 AM.HasBaseReg)
6751 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006752
6753 // X86-64 only supports addr of globals in small code model.
6754 if (Subtarget->is64Bit()) {
6755 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6756 return false;
6757 // If lower 4G is not available, then we must use rip-relative addressing.
6758 if (AM.BaseOffs || AM.Scale > 1)
6759 return false;
6760 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006761 }
6762
6763 switch (AM.Scale) {
6764 case 0:
6765 case 1:
6766 case 2:
6767 case 4:
6768 case 8:
6769 // These scales always work.
6770 break;
6771 case 3:
6772 case 5:
6773 case 9:
6774 // These scales are formed with basereg+scalereg. Only accept if there is
6775 // no basereg yet.
6776 if (AM.HasBaseReg)
6777 return false;
6778 break;
6779 default: // Other stuff never works.
6780 return false;
6781 }
6782
6783 return true;
6784}
6785
6786
Evan Cheng27a820a2007-10-26 01:56:11 +00006787bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6788 if (!Ty1->isInteger() || !Ty2->isInteger())
6789 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006790 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6791 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006792 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006793 return false;
6794 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006795}
6796
Duncan Sands92c43912008-06-06 12:08:01 +00006797bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6798 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006799 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006800 unsigned NumBits1 = VT1.getSizeInBits();
6801 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006802 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006803 return false;
6804 return Subtarget->is64Bit() || NumBits1 < 64;
6805}
Evan Cheng27a820a2007-10-26 01:56:11 +00006806
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006807/// isShuffleMaskLegal - Targets can use this to indicate that they only
6808/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6809/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6810/// are assumed to be legal.
6811bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006812X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006813 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006814 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006815 return (Mask.getNode()->getNumOperands() <= 4 ||
6816 isIdentityMask(Mask.getNode()) ||
6817 isIdentityMask(Mask.getNode(), true) ||
6818 isSplatMask(Mask.getNode()) ||
6819 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6820 X86::isUNPCKLMask(Mask.getNode()) ||
6821 X86::isUNPCKHMask(Mask.getNode()) ||
6822 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6823 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006824}
6825
Dan Gohman48d5f062008-04-09 20:09:42 +00006826bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006827X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006828 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006829 unsigned NumElts = BVOps.size();
6830 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006831 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006832 if (NumElts == 2) return true;
6833 if (NumElts == 4) {
6834 return (isMOVLMask(&BVOps[0], 4) ||
6835 isCommutedMOVL(&BVOps[0], 4, true) ||
6836 isSHUFPMask(&BVOps[0], 4) ||
6837 isCommutedSHUFP(&BVOps[0], 4));
6838 }
6839 return false;
6840}
6841
6842//===----------------------------------------------------------------------===//
6843// X86 Scheduler Hooks
6844//===----------------------------------------------------------------------===//
6845
Mon P Wang078a62d2008-05-05 19:05:59 +00006846// private utility function
6847MachineBasicBlock *
6848X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6849 MachineBasicBlock *MBB,
6850 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006851 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006852 unsigned LoadOpc,
6853 unsigned CXchgOpc,
6854 unsigned copyOpc,
6855 unsigned notOpc,
6856 unsigned EAXreg,
6857 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006858 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006859 // For the atomic bitwise operator, we generate
6860 // thisMBB:
6861 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006862 // ld t1 = [bitinstr.addr]
6863 // op t2 = t1, [bitinstr.val]
6864 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006865 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6866 // bz newMBB
6867 // fallthrough -->nextMBB
6868 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6869 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006870 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006871 ++MBBIter;
6872
6873 /// First build the CFG
6874 MachineFunction *F = MBB->getParent();
6875 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006876 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6877 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6878 F->insert(MBBIter, newMBB);
6879 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006880
6881 // Move all successors to thisMBB to nextMBB
6882 nextMBB->transferSuccessors(thisMBB);
6883
6884 // Update thisMBB to fall through to newMBB
6885 thisMBB->addSuccessor(newMBB);
6886
6887 // newMBB jumps to itself and fall through to nextMBB
6888 newMBB->addSuccessor(nextMBB);
6889 newMBB->addSuccessor(newMBB);
6890
6891 // Insert instructions into newMBB based on incoming instruction
6892 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6893 MachineOperand& destOper = bInstr->getOperand(0);
6894 MachineOperand* argOpers[6];
6895 int numArgs = bInstr->getNumOperands() - 1;
6896 for (int i=0; i < numArgs; ++i)
6897 argOpers[i] = &bInstr->getOperand(i+1);
6898
6899 // x86 address has 4 operands: base, index, scale, and displacement
6900 int lastAddrIndx = 3; // [0,3]
6901 int valArgIndx = 4;
6902
Dale Johannesend20e4452008-08-19 18:47:28 +00006903 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6904 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006905 for (int i=0; i <= lastAddrIndx; ++i)
6906 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006907
Dale Johannesend20e4452008-08-19 18:47:28 +00006908 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006909 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006910 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006911 }
6912 else
6913 tt = t1;
6914
Dale Johannesend20e4452008-08-19 18:47:28 +00006915 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006916 assert((argOpers[valArgIndx]->isReg() ||
6917 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006918 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006919 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006920 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6921 else
6922 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006923 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006924 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006925
Dale Johannesend20e4452008-08-19 18:47:28 +00006926 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006927 MIB.addReg(t1);
6928
Dale Johannesend20e4452008-08-19 18:47:28 +00006929 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006930 for (int i=0; i <= lastAddrIndx; ++i)
6931 (*MIB).addOperand(*argOpers[i]);
6932 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006933 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6934 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6935
Dale Johannesend20e4452008-08-19 18:47:28 +00006936 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6937 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006938
6939 // insert branch
6940 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6941
Dan Gohman221a4372008-07-07 23:14:23 +00006942 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006943 return nextMBB;
6944}
6945
Dale Johannesen44eb5372008-10-03 19:41:08 +00006946// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00006947MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00006948X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
6949 MachineBasicBlock *MBB,
6950 unsigned regOpcL,
6951 unsigned regOpcH,
6952 unsigned immOpcL,
6953 unsigned immOpcH,
6954 bool invSrc) {
6955 // For the atomic bitwise operator, we generate
6956 // thisMBB (instructions are in pairs, except cmpxchg8b)
6957 // ld t1,t2 = [bitinstr.addr]
6958 // newMBB:
6959 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
6960 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006961 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00006962 // mov ECX, EBX <- t5, t6
6963 // mov EAX, EDX <- t1, t2
6964 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
6965 // mov t3, t4 <- EAX, EDX
6966 // bz newMBB
6967 // result in out1, out2
6968 // fallthrough -->nextMBB
6969
6970 const TargetRegisterClass *RC = X86::GR32RegisterClass;
6971 const unsigned LoadOpc = X86::MOV32rm;
6972 const unsigned copyOpc = X86::MOV32rr;
6973 const unsigned NotOpc = X86::NOT32r;
6974 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6975 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6976 MachineFunction::iterator MBBIter = MBB;
6977 ++MBBIter;
6978
6979 /// First build the CFG
6980 MachineFunction *F = MBB->getParent();
6981 MachineBasicBlock *thisMBB = MBB;
6982 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6983 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6984 F->insert(MBBIter, newMBB);
6985 F->insert(MBBIter, nextMBB);
6986
6987 // Move all successors to thisMBB to nextMBB
6988 nextMBB->transferSuccessors(thisMBB);
6989
6990 // Update thisMBB to fall through to newMBB
6991 thisMBB->addSuccessor(newMBB);
6992
6993 // newMBB jumps to itself and fall through to nextMBB
6994 newMBB->addSuccessor(nextMBB);
6995 newMBB->addSuccessor(newMBB);
6996
6997 // Insert instructions into newMBB based on incoming instruction
6998 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
6999 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
7000 MachineOperand& dest1Oper = bInstr->getOperand(0);
7001 MachineOperand& dest2Oper = bInstr->getOperand(1);
7002 MachineOperand* argOpers[6];
7003 for (int i=0; i < 6; ++i)
7004 argOpers[i] = &bInstr->getOperand(i+2);
7005
7006 // x86 address has 4 operands: base, index, scale, and displacement
7007 int lastAddrIndx = 3; // [0,3]
7008
7009 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
7010 MachineInstrBuilder MIB = BuildMI(thisMBB, TII->get(LoadOpc), t1);
7011 for (int i=0; i <= lastAddrIndx; ++i)
7012 (*MIB).addOperand(*argOpers[i]);
7013 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
7014 MIB = BuildMI(thisMBB, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007015 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00007016 for (int i=0; i <= lastAddrIndx-1; ++i)
7017 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007018 MachineOperand newOp3 = *(argOpers[3]);
7019 if (newOp3.isImm())
7020 newOp3.setImm(newOp3.getImm()+4);
7021 else
7022 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00007023 (*MIB).addOperand(newOp3);
7024
7025 // t3/4 are defined later, at the bottom of the loop
7026 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7027 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
7028 BuildMI(newMBB, TII->get(X86::PHI), dest1Oper.getReg())
7029 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
7030 BuildMI(newMBB, TII->get(X86::PHI), dest2Oper.getReg())
7031 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7032
7033 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7034 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
7035 if (invSrc) {
7036 MIB = BuildMI(newMBB, TII->get(NotOpc), tt1).addReg(t1);
7037 MIB = BuildMI(newMBB, TII->get(NotOpc), tt2).addReg(t2);
7038 } else {
7039 tt1 = t1;
7040 tt2 = t2;
7041 }
7042
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007043 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00007044 "invalid operand");
7045 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7046 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007047 if (argOpers[4]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007048 MIB = BuildMI(newMBB, TII->get(regOpcL), t5);
7049 else
7050 MIB = BuildMI(newMBB, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007051 if (regOpcL != X86::MOV32rr)
7052 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00007053 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007054 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
7055 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
7056 if (argOpers[5]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007057 MIB = BuildMI(newMBB, TII->get(regOpcH), t6);
7058 else
7059 MIB = BuildMI(newMBB, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007060 if (regOpcH != X86::MOV32rr)
7061 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00007062 (*MIB).addOperand(*argOpers[5]);
7063
7064 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EAX);
7065 MIB.addReg(t1);
7066 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EDX);
7067 MIB.addReg(t2);
7068
7069 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EBX);
7070 MIB.addReg(t5);
7071 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::ECX);
7072 MIB.addReg(t6);
7073
7074 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG8B));
7075 for (int i=0; i <= lastAddrIndx; ++i)
7076 (*MIB).addOperand(*argOpers[i]);
7077
7078 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7079 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7080
7081 MIB = BuildMI(newMBB, TII->get(copyOpc), t3);
7082 MIB.addReg(X86::EAX);
7083 MIB = BuildMI(newMBB, TII->get(copyOpc), t4);
7084 MIB.addReg(X86::EDX);
7085
7086 // insert branch
7087 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
7088
7089 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7090 return nextMBB;
7091}
7092
7093// private utility function
7094MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00007095X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7096 MachineBasicBlock *MBB,
7097 unsigned cmovOpc) {
7098 // For the atomic min/max operator, we generate
7099 // thisMBB:
7100 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00007101 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00007102 // mov t2 = [min/max.val]
7103 // cmp t1, t2
7104 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00007105 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00007106 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7107 // bz newMBB
7108 // fallthrough -->nextMBB
7109 //
7110 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7111 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007112 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00007113 ++MBBIter;
7114
7115 /// First build the CFG
7116 MachineFunction *F = MBB->getParent();
7117 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00007118 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7119 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7120 F->insert(MBBIter, newMBB);
7121 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007122
7123 // Move all successors to thisMBB to nextMBB
7124 nextMBB->transferSuccessors(thisMBB);
7125
7126 // Update thisMBB to fall through to newMBB
7127 thisMBB->addSuccessor(newMBB);
7128
7129 // newMBB jumps to newMBB and fall through to nextMBB
7130 newMBB->addSuccessor(nextMBB);
7131 newMBB->addSuccessor(newMBB);
7132
7133 // Insert instructions into newMBB based on incoming instruction
7134 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
7135 MachineOperand& destOper = mInstr->getOperand(0);
7136 MachineOperand* argOpers[6];
7137 int numArgs = mInstr->getNumOperands() - 1;
7138 for (int i=0; i < numArgs; ++i)
7139 argOpers[i] = &mInstr->getOperand(i+1);
7140
7141 // x86 address has 4 operands: base, index, scale, and displacement
7142 int lastAddrIndx = 3; // [0,3]
7143 int valArgIndx = 4;
7144
Mon P Wang318b0372008-05-05 22:56:23 +00007145 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
7146 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00007147 for (int i=0; i <= lastAddrIndx; ++i)
7148 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00007149
Mon P Wang078a62d2008-05-05 19:05:59 +00007150 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007151 assert((argOpers[valArgIndx]->isReg() ||
7152 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00007153 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00007154
7155 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007156 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00007157 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
7158 else
7159 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
7160 (*MIB).addOperand(*argOpers[valArgIndx]);
7161
Mon P Wang318b0372008-05-05 22:56:23 +00007162 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
7163 MIB.addReg(t1);
7164
Mon P Wang078a62d2008-05-05 19:05:59 +00007165 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
7166 MIB.addReg(t1);
7167 MIB.addReg(t2);
7168
7169 // Generate movc
7170 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
7171 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
7172 MIB.addReg(t2);
7173 MIB.addReg(t1);
7174
7175 // Cmp and exchange if none has modified the memory location
7176 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
7177 for (int i=0; i <= lastAddrIndx; ++i)
7178 (*MIB).addOperand(*argOpers[i]);
7179 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00007180 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7181 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00007182
7183 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
7184 MIB.addReg(X86::EAX);
7185
7186 // insert branch
7187 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
7188
Dan Gohman221a4372008-07-07 23:14:23 +00007189 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00007190 return nextMBB;
7191}
7192
7193
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007194MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00007195X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
7196 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007197 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7198 switch (MI->getOpcode()) {
7199 default: assert(false && "Unexpected instr type to insert");
Mon P Wang83edba52008-12-12 01:25:51 +00007200 case X86::CMOV_V1I64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007201 case X86::CMOV_FR32:
7202 case X86::CMOV_FR64:
7203 case X86::CMOV_V4F32:
7204 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00007205 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007206 // To "insert" a SELECT_CC instruction, we actually have to insert the
7207 // diamond control-flow pattern. The incoming instruction knows the
7208 // destination vreg to set, the condition code register to branch on, the
7209 // true/false values to select between, and a branch opcode to use.
7210 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007211 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007212 ++It;
7213
7214 // thisMBB:
7215 // ...
7216 // TrueVal = ...
7217 // cmpTY ccX, r1, r2
7218 // bCC copy1MBB
7219 // fallthrough --> copy0MBB
7220 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00007221 MachineFunction *F = BB->getParent();
7222 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7223 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007224 unsigned Opc =
7225 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
7226 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00007227 F->insert(It, copy0MBB);
7228 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007229 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007230 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00007231 sinkMBB->transferSuccessors(BB);
7232
7233 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007234 BB->addSuccessor(copy0MBB);
7235 BB->addSuccessor(sinkMBB);
7236
7237 // copy0MBB:
7238 // %FalseValue = ...
7239 // # fallthrough to sinkMBB
7240 BB = copy0MBB;
7241
7242 // Update machine-CFG edges
7243 BB->addSuccessor(sinkMBB);
7244
7245 // sinkMBB:
7246 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7247 // ...
7248 BB = sinkMBB;
7249 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
7250 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7251 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7252
Dan Gohman221a4372008-07-07 23:14:23 +00007253 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007254 return BB;
7255 }
7256
7257 case X86::FP32_TO_INT16_IN_MEM:
7258 case X86::FP32_TO_INT32_IN_MEM:
7259 case X86::FP32_TO_INT64_IN_MEM:
7260 case X86::FP64_TO_INT16_IN_MEM:
7261 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007262 case X86::FP64_TO_INT64_IN_MEM:
7263 case X86::FP80_TO_INT16_IN_MEM:
7264 case X86::FP80_TO_INT32_IN_MEM:
7265 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007266 // Change the floating point control register to use "round towards zero"
7267 // mode when truncating to an integer value.
7268 MachineFunction *F = BB->getParent();
7269 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
7270 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
7271
7272 // Load the old value of the high byte of the control word...
7273 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00007274 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007275 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
7276
7277 // Set the high part to be round to zero...
7278 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
7279 .addImm(0xC7F);
7280
7281 // Reload the modified control word now...
7282 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7283
7284 // Restore the memory image of control word to original value
7285 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
7286 .addReg(OldCW);
7287
7288 // Get the X86 opcode to use.
7289 unsigned Opc;
7290 switch (MI->getOpcode()) {
7291 default: assert(0 && "illegal opcode!");
7292 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7293 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7294 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7295 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7296 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7297 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007298 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7299 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7300 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007301 }
7302
7303 X86AddressMode AM;
7304 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007305 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007306 AM.BaseType = X86AddressMode::RegBase;
7307 AM.Base.Reg = Op.getReg();
7308 } else {
7309 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00007310 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007311 }
7312 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007313 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007314 AM.Scale = Op.getImm();
7315 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007316 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007317 AM.IndexReg = Op.getImm();
7318 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007319 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007320 AM.GV = Op.getGlobal();
7321 } else {
7322 AM.Disp = Op.getImm();
7323 }
7324 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
7325 .addReg(MI->getOperand(4).getReg());
7326
7327 // Reload the original control word now.
7328 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7329
Dan Gohman221a4372008-07-07 23:14:23 +00007330 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007331 return BB;
7332 }
Mon P Wang078a62d2008-05-05 19:05:59 +00007333 case X86::ATOMAND32:
7334 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007335 X86::AND32ri, X86::MOV32rm,
7336 X86::LCMPXCHG32, X86::MOV32rr,
7337 X86::NOT32r, X86::EAX,
7338 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007339 case X86::ATOMOR32:
7340 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007341 X86::OR32ri, X86::MOV32rm,
7342 X86::LCMPXCHG32, X86::MOV32rr,
7343 X86::NOT32r, X86::EAX,
7344 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007345 case X86::ATOMXOR32:
7346 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007347 X86::XOR32ri, X86::MOV32rm,
7348 X86::LCMPXCHG32, X86::MOV32rr,
7349 X86::NOT32r, X86::EAX,
7350 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007351 case X86::ATOMNAND32:
7352 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007353 X86::AND32ri, X86::MOV32rm,
7354 X86::LCMPXCHG32, X86::MOV32rr,
7355 X86::NOT32r, X86::EAX,
7356 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00007357 case X86::ATOMMIN32:
7358 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7359 case X86::ATOMMAX32:
7360 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7361 case X86::ATOMUMIN32:
7362 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7363 case X86::ATOMUMAX32:
7364 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00007365
7366 case X86::ATOMAND16:
7367 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7368 X86::AND16ri, X86::MOV16rm,
7369 X86::LCMPXCHG16, X86::MOV16rr,
7370 X86::NOT16r, X86::AX,
7371 X86::GR16RegisterClass);
7372 case X86::ATOMOR16:
7373 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7374 X86::OR16ri, X86::MOV16rm,
7375 X86::LCMPXCHG16, X86::MOV16rr,
7376 X86::NOT16r, X86::AX,
7377 X86::GR16RegisterClass);
7378 case X86::ATOMXOR16:
7379 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7380 X86::XOR16ri, X86::MOV16rm,
7381 X86::LCMPXCHG16, X86::MOV16rr,
7382 X86::NOT16r, X86::AX,
7383 X86::GR16RegisterClass);
7384 case X86::ATOMNAND16:
7385 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7386 X86::AND16ri, X86::MOV16rm,
7387 X86::LCMPXCHG16, X86::MOV16rr,
7388 X86::NOT16r, X86::AX,
7389 X86::GR16RegisterClass, true);
7390 case X86::ATOMMIN16:
7391 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7392 case X86::ATOMMAX16:
7393 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7394 case X86::ATOMUMIN16:
7395 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7396 case X86::ATOMUMAX16:
7397 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7398
7399 case X86::ATOMAND8:
7400 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7401 X86::AND8ri, X86::MOV8rm,
7402 X86::LCMPXCHG8, X86::MOV8rr,
7403 X86::NOT8r, X86::AL,
7404 X86::GR8RegisterClass);
7405 case X86::ATOMOR8:
7406 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7407 X86::OR8ri, X86::MOV8rm,
7408 X86::LCMPXCHG8, X86::MOV8rr,
7409 X86::NOT8r, X86::AL,
7410 X86::GR8RegisterClass);
7411 case X86::ATOMXOR8:
7412 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7413 X86::XOR8ri, X86::MOV8rm,
7414 X86::LCMPXCHG8, X86::MOV8rr,
7415 X86::NOT8r, X86::AL,
7416 X86::GR8RegisterClass);
7417 case X86::ATOMNAND8:
7418 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7419 X86::AND8ri, X86::MOV8rm,
7420 X86::LCMPXCHG8, X86::MOV8rr,
7421 X86::NOT8r, X86::AL,
7422 X86::GR8RegisterClass, true);
7423 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00007424 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007425 case X86::ATOMAND64:
7426 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7427 X86::AND64ri32, X86::MOV64rm,
7428 X86::LCMPXCHG64, X86::MOV64rr,
7429 X86::NOT64r, X86::RAX,
7430 X86::GR64RegisterClass);
7431 case X86::ATOMOR64:
7432 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7433 X86::OR64ri32, X86::MOV64rm,
7434 X86::LCMPXCHG64, X86::MOV64rr,
7435 X86::NOT64r, X86::RAX,
7436 X86::GR64RegisterClass);
7437 case X86::ATOMXOR64:
7438 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7439 X86::XOR64ri32, X86::MOV64rm,
7440 X86::LCMPXCHG64, X86::MOV64rr,
7441 X86::NOT64r, X86::RAX,
7442 X86::GR64RegisterClass);
7443 case X86::ATOMNAND64:
7444 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7445 X86::AND64ri32, X86::MOV64rm,
7446 X86::LCMPXCHG64, X86::MOV64rr,
7447 X86::NOT64r, X86::RAX,
7448 X86::GR64RegisterClass, true);
7449 case X86::ATOMMIN64:
7450 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7451 case X86::ATOMMAX64:
7452 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7453 case X86::ATOMUMIN64:
7454 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7455 case X86::ATOMUMAX64:
7456 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00007457
7458 // This group does 64-bit operations on a 32-bit host.
7459 case X86::ATOMAND6432:
7460 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7461 X86::AND32rr, X86::AND32rr,
7462 X86::AND32ri, X86::AND32ri,
7463 false);
7464 case X86::ATOMOR6432:
7465 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7466 X86::OR32rr, X86::OR32rr,
7467 X86::OR32ri, X86::OR32ri,
7468 false);
7469 case X86::ATOMXOR6432:
7470 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7471 X86::XOR32rr, X86::XOR32rr,
7472 X86::XOR32ri, X86::XOR32ri,
7473 false);
7474 case X86::ATOMNAND6432:
7475 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7476 X86::AND32rr, X86::AND32rr,
7477 X86::AND32ri, X86::AND32ri,
7478 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00007479 case X86::ATOMADD6432:
7480 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7481 X86::ADD32rr, X86::ADC32rr,
7482 X86::ADD32ri, X86::ADC32ri,
7483 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00007484 case X86::ATOMSUB6432:
7485 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7486 X86::SUB32rr, X86::SBB32rr,
7487 X86::SUB32ri, X86::SBB32ri,
7488 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007489 case X86::ATOMSWAP6432:
7490 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7491 X86::MOV32rr, X86::MOV32rr,
7492 X86::MOV32ri, X86::MOV32ri,
7493 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007494 }
7495}
7496
7497//===----------------------------------------------------------------------===//
7498// X86 Optimization Hooks
7499//===----------------------------------------------------------------------===//
7500
Dan Gohman8181bd12008-07-27 21:46:04 +00007501void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00007502 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00007503 APInt &KnownZero,
7504 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007505 const SelectionDAG &DAG,
7506 unsigned Depth) const {
7507 unsigned Opc = Op.getOpcode();
7508 assert((Opc >= ISD::BUILTIN_OP_END ||
7509 Opc == ISD::INTRINSIC_WO_CHAIN ||
7510 Opc == ISD::INTRINSIC_W_CHAIN ||
7511 Opc == ISD::INTRINSIC_VOID) &&
7512 "Should use MaskedValueIsZero if you don't know whether Op"
7513 " is a target node!");
7514
Dan Gohman1d79e432008-02-13 23:07:24 +00007515 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007516 switch (Opc) {
7517 default: break;
Evan Cheng8e9b21c2009-02-02 09:15:04 +00007518 case X86ISD::ADD:
7519 case X86ISD::SUB:
7520 case X86ISD::SMUL:
7521 case X86ISD::UMUL:
7522 // These nodes' second result is a boolean.
7523 if (Op.getResNo() == 0)
7524 break;
7525 // Fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007526 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007527 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7528 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007529 break;
7530 }
7531}
7532
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007533/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007534/// node is a GlobalAddress + offset.
7535bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7536 GlobalValue* &GA, int64_t &Offset) const{
7537 if (N->getOpcode() == X86ISD::Wrapper) {
7538 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007539 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00007540 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007541 return true;
7542 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007543 }
Evan Chengef7be082008-05-12 19:56:52 +00007544 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007545}
7546
Evan Chengef7be082008-05-12 19:56:52 +00007547static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7548 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007549 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007550 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007551 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007552 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007553 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007554 return false;
7555}
7556
Dan Gohman8181bd12008-07-27 21:46:04 +00007557static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007558 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007559 SDNode *&Base,
7560 SelectionDAG &DAG, MachineFrameInfo *MFI,
7561 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007562 Base = NULL;
7563 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007564 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007565 if (Idx.getOpcode() == ISD::UNDEF) {
7566 if (!Base)
7567 return false;
7568 continue;
7569 }
7570
Dan Gohman8181bd12008-07-27 21:46:04 +00007571 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007572 if (!Elt.getNode() ||
7573 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007574 return false;
7575 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007576 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007577 if (Base->getOpcode() == ISD::UNDEF)
7578 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007579 continue;
7580 }
7581 if (Elt.getOpcode() == ISD::UNDEF)
7582 continue;
7583
Gabor Greif1c80d112008-08-28 21:40:38 +00007584 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007585 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007586 return false;
7587 }
7588 return true;
7589}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007590
7591/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7592/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7593/// if the load addresses are consecutive, non-overlapping, and in the right
7594/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007595static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007596 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007597 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00007598 MVT VT = N->getValueType(0);
7599 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007600 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007601 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007602 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007603 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7604 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007605 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007606
Dan Gohman11821702007-07-27 17:16:43 +00007607 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007608 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007609 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00007610 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00007611 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
7612 LD->getSrcValueOffset(), LD->isVolatile(),
7613 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007614}
7615
Evan Chengb6290462008-05-12 23:04:07 +00007616/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007617static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohman22cefb02009-01-29 01:59:02 +00007618 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng6617eed2008-09-24 23:26:36 +00007619 const X86Subtarget *Subtarget,
7620 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007621 unsigned NumOps = N->getNumOperands();
7622
Evan Chenge9b9c672008-05-09 21:53:03 +00007623 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007624 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007625 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007626
Duncan Sands92c43912008-06-06 12:08:01 +00007627 MVT VT = N->getValueType(0);
7628 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007629 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7630 // We are looking for load i64 and zero extend. We want to transform
7631 // it before legalizer has a chance to expand it. Also look for i64
7632 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007633 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007634 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007635 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007636 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007637 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007638
7639 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007640 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007641 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007642 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007643 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007644 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007645 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007646 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007647 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007648
7649 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007650 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007651
7652 // Load must not be an extload.
7653 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007654 return SDValue();
Mon P Wang28b36412009-01-30 07:07:40 +00007655
7656 // Load type should legal type so we don't have to legalize it.
7657 if (!TLI.isTypeLegal(VT))
7658 return SDValue();
7659
Evan Cheng6617eed2008-09-24 23:26:36 +00007660 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7661 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7662 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
Dan Gohman22cefb02009-01-29 01:59:02 +00007663 TargetLowering::TargetLoweringOpt TLO(DAG);
7664 TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1));
7665 DCI.CommitTargetLoweringOpt(TLO);
Evan Cheng6617eed2008-09-24 23:26:36 +00007666 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007667}
7668
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007669/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007670static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007671 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007672 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007673
7674 // If we have SSE[12] support, try to form min/max nodes.
7675 if (Subtarget->hasSSE2() &&
7676 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7677 if (Cond.getOpcode() == ISD::SETCC) {
7678 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007679 SDValue LHS = N->getOperand(1);
7680 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007681 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7682
7683 unsigned Opcode = 0;
7684 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7685 switch (CC) {
7686 default: break;
7687 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7688 case ISD::SETULE:
7689 case ISD::SETLE:
7690 if (!UnsafeFPMath) break;
7691 // FALL THROUGH.
7692 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7693 case ISD::SETLT:
7694 Opcode = X86ISD::FMIN;
7695 break;
7696
7697 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7698 case ISD::SETUGT:
7699 case ISD::SETGT:
7700 if (!UnsafeFPMath) break;
7701 // FALL THROUGH.
7702 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7703 case ISD::SETGE:
7704 Opcode = X86ISD::FMAX;
7705 break;
7706 }
7707 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7708 switch (CC) {
7709 default: break;
7710 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7711 case ISD::SETUGT:
7712 case ISD::SETGT:
7713 if (!UnsafeFPMath) break;
7714 // FALL THROUGH.
7715 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7716 case ISD::SETGE:
7717 Opcode = X86ISD::FMIN;
7718 break;
7719
7720 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7721 case ISD::SETULE:
7722 case ISD::SETLE:
7723 if (!UnsafeFPMath) break;
7724 // FALL THROUGH.
7725 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7726 case ISD::SETLT:
7727 Opcode = X86ISD::FMAX;
7728 break;
7729 }
7730 }
7731
7732 if (Opcode)
7733 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
7734 }
7735
7736 }
7737
Dan Gohman8181bd12008-07-27 21:46:04 +00007738 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007739}
7740
sampo025b75c2009-01-26 00:52:55 +00007741/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
7742/// when possible.
7743static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
7744 const X86Subtarget *Subtarget) {
7745 // On X86 with SSE2 support, we can transform this to a vector shift if
7746 // all elements are shifted by the same amount. We can't do this in legalize
7747 // because the a constant vector is typically transformed to a constant pool
7748 // so we have no knowledge of the shift amount.
sampo087d53c2009-01-26 03:15:31 +00007749 if (!Subtarget->hasSSE2())
7750 return SDValue();
7751
sampo025b75c2009-01-26 00:52:55 +00007752 MVT VT = N->getValueType(0);
sampo087d53c2009-01-26 03:15:31 +00007753 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
7754 return SDValue();
7755
Mon P Wanga91e9642009-01-28 08:12:05 +00007756 SDValue ShAmtOp = N->getOperand(1);
7757 MVT EltVT = VT.getVectorElementType();
7758 SDValue BaseShAmt;
7759 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
7760 unsigned NumElts = VT.getVectorNumElements();
7761 unsigned i = 0;
7762 for (; i != NumElts; ++i) {
7763 SDValue Arg = ShAmtOp.getOperand(i);
7764 if (Arg.getOpcode() == ISD::UNDEF) continue;
7765 BaseShAmt = Arg;
7766 break;
7767 }
7768 for (; i != NumElts; ++i) {
7769 SDValue Arg = ShAmtOp.getOperand(i);
7770 if (Arg.getOpcode() == ISD::UNDEF) continue;
7771 if (Arg != BaseShAmt) {
7772 return SDValue();
7773 }
7774 }
7775 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
7776 isSplatMask(ShAmtOp.getOperand(2).getNode())) {
7777 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, ShAmtOp,
7778 DAG.getIntPtrConstant(0));
7779 } else
sampo087d53c2009-01-26 03:15:31 +00007780 return SDValue();
sampo025b75c2009-01-26 00:52:55 +00007781
sampo087d53c2009-01-26 03:15:31 +00007782 if (EltVT.bitsGT(MVT::i32))
7783 BaseShAmt = DAG.getNode(ISD::TRUNCATE, MVT::i32, BaseShAmt);
7784 else if (EltVT.bitsLT(MVT::i32))
7785 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, BaseShAmt);
sampo025b75c2009-01-26 00:52:55 +00007786
sampo087d53c2009-01-26 03:15:31 +00007787 // The shift amount is identical so we can do a vector shift.
7788 SDValue ValOp = N->getOperand(0);
7789 switch (N->getOpcode()) {
7790 default:
7791 assert(0 && "Unknown shift opcode!");
7792 break;
7793 case ISD::SHL:
7794 if (VT == MVT::v2i64)
7795 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007796 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7797 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007798 if (VT == MVT::v4i32)
7799 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007800 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
7801 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007802 if (VT == MVT::v8i16)
7803 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007804 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
7805 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007806 break;
7807 case ISD::SRA:
7808 if (VT == MVT::v4i32)
7809 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007810 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
7811 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007812 if (VT == MVT::v8i16)
7813 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007814 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
7815 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007816 break;
7817 case ISD::SRL:
7818 if (VT == MVT::v2i64)
7819 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007820 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7821 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007822 if (VT == MVT::v4i32)
7823 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007824 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
7825 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007826 if (VT == MVT::v8i16)
7827 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007828 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
7829 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007830 break;
sampo025b75c2009-01-26 00:52:55 +00007831 }
7832 return SDValue();
7833}
7834
Chris Lattnerce84ae42008-02-22 02:09:43 +00007835/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007836static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007837 const X86Subtarget *Subtarget) {
7838 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7839 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007840 // A preferable solution to the general problem is to figure out the right
7841 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007842 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007843 if (St->getValue().getValueType().isVector() &&
7844 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007845 isa<LoadSDNode>(St->getValue()) &&
7846 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7847 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007848 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007849 LoadSDNode *Ld = 0;
7850 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007851 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007852 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007853 // Must be a store of a load. We currently handle two cases: the load
7854 // is a direct child, and it's under an intervening TokenFactor. It is
7855 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007856 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007857 Ld = cast<LoadSDNode>(St->getChain());
7858 else if (St->getValue().hasOneUse() &&
7859 ChainVal->getOpcode() == ISD::TokenFactor) {
7860 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007861 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007862 TokenFactorIndex = i;
7863 Ld = cast<LoadSDNode>(St->getValue());
7864 } else
7865 Ops.push_back(ChainVal->getOperand(i));
7866 }
7867 }
7868 if (Ld) {
7869 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7870 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007871 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007872 Ld->getBasePtr(), Ld->getSrcValue(),
7873 Ld->getSrcValueOffset(), Ld->isVolatile(),
7874 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007875 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007876 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00007877 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00007878 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7879 Ops.size());
7880 }
7881 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
7882 St->getSrcValue(), St->getSrcValueOffset(),
7883 St->isVolatile(), St->getAlignment());
7884 }
7885
7886 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00007887 SDValue LoAddr = Ld->getBasePtr();
7888 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007889 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007890
Dan Gohman8181bd12008-07-27 21:46:04 +00007891 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007892 Ld->getSrcValue(), Ld->getSrcValueOffset(),
7893 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007894 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007895 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
7896 Ld->isVolatile(),
7897 MinAlign(Ld->getAlignment(), 4));
7898
Dan Gohman8181bd12008-07-27 21:46:04 +00007899 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007900 if (TokenFactorIndex != -1) {
7901 Ops.push_back(LoLd);
7902 Ops.push_back(HiLd);
7903 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7904 Ops.size());
7905 }
7906
7907 LoAddr = St->getBasePtr();
7908 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007909 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007910
Dan Gohman8181bd12008-07-27 21:46:04 +00007911 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007912 St->getSrcValue(), St->getSrcValueOffset(),
7913 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007914 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00007915 St->getSrcValue(),
7916 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00007917 St->isVolatile(),
7918 MinAlign(St->getAlignment(), 4));
7919 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00007920 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00007921 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007922 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00007923}
7924
Chris Lattner470d5dc2008-01-25 06:14:17 +00007925/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7926/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007927static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00007928 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7929 // F[X]OR(0.0, x) -> x
7930 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00007931 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7932 if (C->getValueAPF().isPosZero())
7933 return N->getOperand(1);
7934 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7935 if (C->getValueAPF().isPosZero())
7936 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007937 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007938}
7939
7940/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007941static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007942 // FAND(0.0, x) -> 0.0
7943 // FAND(x, 0.0) -> 0.0
7944 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7945 if (C->getValueAPF().isPosZero())
7946 return N->getOperand(0);
7947 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7948 if (C->getValueAPF().isPosZero())
7949 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007950 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007951}
7952
Dan Gohman22cefb02009-01-29 01:59:02 +00007953static SDValue PerformBTCombine(SDNode *N,
7954 SelectionDAG &DAG,
7955 TargetLowering::DAGCombinerInfo &DCI) {
7956 // BT ignores high bits in the bit index operand.
7957 SDValue Op1 = N->getOperand(1);
7958 if (Op1.hasOneUse()) {
7959 unsigned BitWidth = Op1.getValueSizeInBits();
7960 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
7961 APInt KnownZero, KnownOne;
7962 TargetLowering::TargetLoweringOpt TLO(DAG);
7963 TargetLowering &TLI = DAG.getTargetLoweringInfo();
7964 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
7965 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
7966 DCI.CommitTargetLoweringOpt(TLO);
7967 }
7968 return SDValue();
7969}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007970
Dan Gohman8181bd12008-07-27 21:46:04 +00007971SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00007972 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007973 SelectionDAG &DAG = DCI.DAG;
7974 switch (N->getOpcode()) {
7975 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007976 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7977 case ISD::BUILD_VECTOR:
Dan Gohman22cefb02009-01-29 01:59:02 +00007978 return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007979 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
sampo025b75c2009-01-26 00:52:55 +00007980 case ISD::SHL:
7981 case ISD::SRA:
7982 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007983 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007984 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007985 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7986 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohman22cefb02009-01-29 01:59:02 +00007987 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007988 }
7989
Dan Gohman8181bd12008-07-27 21:46:04 +00007990 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007991}
7992
7993//===----------------------------------------------------------------------===//
7994// X86 Inline Assembly Support
7995//===----------------------------------------------------------------------===//
7996
7997/// getConstraintType - Given a constraint letter, return the type of
7998/// constraint it is for this target.
7999X86TargetLowering::ConstraintType
8000X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8001 if (Constraint.size() == 1) {
8002 switch (Constraint[0]) {
8003 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +00008004 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +00008005 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008006 case 'r':
8007 case 'R':
8008 case 'l':
8009 case 'q':
8010 case 'Q':
8011 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00008012 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008013 case 'Y':
8014 return C_RegisterClass;
8015 default:
8016 break;
8017 }
8018 }
8019 return TargetLowering::getConstraintType(Constraint);
8020}
8021
Dale Johannesene99fc902008-01-29 02:21:21 +00008022/// LowerXConstraint - try to replace an X constraint, which matches anything,
8023/// with another that has more specific requirements based on the type of the
8024/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00008025const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00008026LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00008027 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8028 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00008029 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00008030 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00008031 return "Y";
8032 if (Subtarget->hasSSE1())
8033 return "x";
8034 }
8035
8036 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00008037}
8038
Chris Lattnera531abc2007-08-25 00:47:38 +00008039/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8040/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00008041void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00008042 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00008043 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00008044 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00008045 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00008046 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00008047
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008048 switch (Constraint) {
8049 default: break;
8050 case 'I':
8051 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008052 if (C->getZExtValue() <= 31) {
8053 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008054 break;
8055 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008056 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008057 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00008058 case 'J':
8059 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8060 if (C->getZExtValue() <= 63) {
8061 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8062 break;
8063 }
8064 }
8065 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008066 case 'N':
8067 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008068 if (C->getZExtValue() <= 255) {
8069 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008070 break;
8071 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008072 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008073 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008074 case 'i': {
8075 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00008076 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008077 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008078 break;
8079 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008080
8081 // If we are in non-pic codegen mode, we allow the address of a global (with
8082 // an optional displacement) to be used with 'i'.
8083 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
8084 int64_t Offset = 0;
8085
8086 // Match either (GA) or (GA+C)
8087 if (GA) {
8088 Offset = GA->getOffset();
8089 } else if (Op.getOpcode() == ISD::ADD) {
8090 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8091 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8092 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008093 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008094 } else {
8095 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8096 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8097 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008098 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008099 else
8100 C = 0, GA = 0;
8101 }
8102 }
8103
8104 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00008105 if (hasMemory)
Dan Gohman36322c72008-10-18 02:06:02 +00008106 Op = LowerGlobalAddress(GA->getGlobal(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00008107 else
8108 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8109 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00008110 Result = Op;
8111 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008112 }
8113
8114 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00008115 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008116 }
8117 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008118
Gabor Greif1c80d112008-08-28 21:40:38 +00008119 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00008120 Ops.push_back(Result);
8121 return;
8122 }
Evan Cheng7f250d62008-09-24 00:05:32 +00008123 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8124 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008125}
8126
8127std::vector<unsigned> X86TargetLowering::
8128getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00008129 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008130 if (Constraint.size() == 1) {
8131 // FIXME: not handling fp-stack yet!
8132 switch (Constraint[0]) { // GCC X86 Constraint Letters
8133 default: break; // Unknown constraint letter
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008134 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8135 case 'Q': // Q_REGS
8136 if (VT == MVT::i32)
8137 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8138 else if (VT == MVT::i16)
8139 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8140 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00008141 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00008142 else if (VT == MVT::i64)
8143 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8144 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008145 }
8146 }
8147
8148 return std::vector<unsigned>();
8149}
8150
8151std::pair<unsigned, const TargetRegisterClass*>
8152X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00008153 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008154 // First, see if this is a constraint that directly corresponds to an LLVM
8155 // register class.
8156 if (Constraint.size() == 1) {
8157 // GCC Constraint Letters
8158 switch (Constraint[0]) {
8159 default: break;
8160 case 'r': // GENERAL_REGS
8161 case 'R': // LEGACY_REGS
8162 case 'l': // INDEX_REGS
Chris Lattnerbbfea052008-10-17 18:15:05 +00008163 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008164 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00008165 if (VT == MVT::i16)
8166 return std::make_pair(0U, X86::GR16RegisterClass);
8167 if (VT == MVT::i32 || !Subtarget->is64Bit())
8168 return std::make_pair(0U, X86::GR32RegisterClass);
8169 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00008170 case 'f': // FP Stack registers.
8171 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8172 // value to the correct fpstack register class.
8173 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8174 return std::make_pair(0U, X86::RFP32RegisterClass);
8175 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8176 return std::make_pair(0U, X86::RFP64RegisterClass);
8177 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008178 case 'y': // MMX_REGS if MMX allowed.
8179 if (!Subtarget->hasMMX()) break;
8180 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008181 case 'Y': // SSE_REGS if SSE2 allowed
8182 if (!Subtarget->hasSSE2()) break;
8183 // FALL THROUGH.
8184 case 'x': // SSE_REGS if SSE1 allowed
8185 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00008186
8187 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008188 default: break;
8189 // Scalar SSE types.
8190 case MVT::f32:
8191 case MVT::i32:
8192 return std::make_pair(0U, X86::FR32RegisterClass);
8193 case MVT::f64:
8194 case MVT::i64:
8195 return std::make_pair(0U, X86::FR64RegisterClass);
8196 // Vector types.
8197 case MVT::v16i8:
8198 case MVT::v8i16:
8199 case MVT::v4i32:
8200 case MVT::v2i64:
8201 case MVT::v4f32:
8202 case MVT::v2f64:
8203 return std::make_pair(0U, X86::VR128RegisterClass);
8204 }
8205 break;
8206 }
8207 }
8208
8209 // Use the default implementation in TargetLowering to convert the register
8210 // constraint into a member of a register class.
8211 std::pair<unsigned, const TargetRegisterClass*> Res;
8212 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8213
8214 // Not found as a standard register?
8215 if (Res.second == 0) {
8216 // GCC calls "st(0)" just plain "st".
8217 if (StringsEqualNoCase("{st}", Constraint)) {
8218 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00008219 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008220 }
Dale Johannesen73920c02008-11-13 21:52:36 +00008221 // 'A' means EAX + EDX.
8222 if (Constraint == "A") {
8223 Res.first = X86::EAX;
8224 Res.second = X86::GRADRegisterClass;
8225 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008226 return Res;
8227 }
8228
8229 // Otherwise, check to see if this is a register class of the wrong value
8230 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8231 // turn into {ax},{dx}.
8232 if (Res.second->hasType(VT))
8233 return Res; // Correct type already, nothing to do.
8234
8235 // All of the single-register GCC register classes map their values onto
8236 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8237 // really want an 8-bit or 32-bit register, map to the appropriate register
8238 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00008239 if (Res.second == X86::GR16RegisterClass) {
8240 if (VT == MVT::i8) {
8241 unsigned DestReg = 0;
8242 switch (Res.first) {
8243 default: break;
8244 case X86::AX: DestReg = X86::AL; break;
8245 case X86::DX: DestReg = X86::DL; break;
8246 case X86::CX: DestReg = X86::CL; break;
8247 case X86::BX: DestReg = X86::BL; break;
8248 }
8249 if (DestReg) {
8250 Res.first = DestReg;
8251 Res.second = Res.second = X86::GR8RegisterClass;
8252 }
8253 } else if (VT == MVT::i32) {
8254 unsigned DestReg = 0;
8255 switch (Res.first) {
8256 default: break;
8257 case X86::AX: DestReg = X86::EAX; break;
8258 case X86::DX: DestReg = X86::EDX; break;
8259 case X86::CX: DestReg = X86::ECX; break;
8260 case X86::BX: DestReg = X86::EBX; break;
8261 case X86::SI: DestReg = X86::ESI; break;
8262 case X86::DI: DestReg = X86::EDI; break;
8263 case X86::BP: DestReg = X86::EBP; break;
8264 case X86::SP: DestReg = X86::ESP; break;
8265 }
8266 if (DestReg) {
8267 Res.first = DestReg;
8268 Res.second = Res.second = X86::GR32RegisterClass;
8269 }
8270 } else if (VT == MVT::i64) {
8271 unsigned DestReg = 0;
8272 switch (Res.first) {
8273 default: break;
8274 case X86::AX: DestReg = X86::RAX; break;
8275 case X86::DX: DestReg = X86::RDX; break;
8276 case X86::CX: DestReg = X86::RCX; break;
8277 case X86::BX: DestReg = X86::RBX; break;
8278 case X86::SI: DestReg = X86::RSI; break;
8279 case X86::DI: DestReg = X86::RDI; break;
8280 case X86::BP: DestReg = X86::RBP; break;
8281 case X86::SP: DestReg = X86::RSP; break;
8282 }
8283 if (DestReg) {
8284 Res.first = DestReg;
8285 Res.second = Res.second = X86::GR64RegisterClass;
8286 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008287 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00008288 } else if (Res.second == X86::FR32RegisterClass ||
8289 Res.second == X86::FR64RegisterClass ||
8290 Res.second == X86::VR128RegisterClass) {
8291 // Handle references to XMM physical registers that got mapped into the
8292 // wrong class. This can happen with constraints like {xmm0} where the
8293 // target independent register mapper will just pick the first match it can
8294 // find, ignoring the required type.
8295 if (VT == MVT::f32)
8296 Res.second = X86::FR32RegisterClass;
8297 else if (VT == MVT::f64)
8298 Res.second = X86::FR64RegisterClass;
8299 else if (X86::VR128RegisterClass->hasType(VT))
8300 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008301 }
8302
8303 return Res;
8304}
Mon P Wang1448aad2008-10-30 08:01:45 +00008305
8306//===----------------------------------------------------------------------===//
8307// X86 Widen vector type
8308//===----------------------------------------------------------------------===//
8309
8310/// getWidenVectorType: given a vector type, returns the type to widen
8311/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8312/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +00008313/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +00008314/// scalarizing vs using the wider vector type.
8315
Dan Gohman0fe66c92009-01-15 17:34:08 +00008316MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang1448aad2008-10-30 08:01:45 +00008317 assert(VT.isVector());
8318 if (isTypeLegal(VT))
8319 return VT;
8320
8321 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8322 // type based on element type. This would speed up our search (though
8323 // it may not be worth it since the size of the list is relatively
8324 // small).
8325 MVT EltVT = VT.getVectorElementType();
8326 unsigned NElts = VT.getVectorNumElements();
8327
8328 // On X86, it make sense to widen any vector wider than 1
8329 if (NElts <= 1)
8330 return MVT::Other;
8331
8332 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
8333 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8334 MVT SVT = (MVT::SimpleValueType)nVT;
8335
8336 if (isTypeLegal(SVT) &&
8337 SVT.getVectorElementType() == EltVT &&
8338 SVT.getVectorNumElements() > NElts)
8339 return SVT;
8340 }
8341 return MVT::Other;
8342}