blob: 9955f9c4d524a2faf98c01e68dd3f50276eaa496 [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.
Dale Johannesence0805b2009-02-03 19:33:06 +000048static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG, DebugLoc dl);
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) {
Dale Johannesence0805b2009-02-03 19:33:06 +0000917 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
919
920 SmallVector<CCValAssign, 16> RVLocs;
921 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
922 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
923 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000924 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000925
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000926 // If this is the first return lowered for this function, add the regs to the
927 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000928 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 for (unsigned i = 0; i != RVLocs.size(); ++i)
930 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000931 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000933 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000934
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000935 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000936 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000937 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000938 SDValue TailCall = Chain;
939 SDValue TargetAddress = TailCall.getOperand(1);
940 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000941 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000942 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000943 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000944 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000945 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
946 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000947 assert(StackAdjustment.getOpcode() == ISD::Constant &&
948 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000949
Dan Gohman8181bd12008-07-27 21:46:04 +0000950 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000951 Operands.push_back(Chain.getOperand(0));
952 Operands.push_back(TargetAddress);
953 Operands.push_back(StackAdjustment);
954 // Copy registers used by the call. Last operand is a flag so it is not
955 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000956 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000957 Operands.push_back(Chain.getOperand(i));
958 }
Dale Johannesence0805b2009-02-03 19:33:06 +0000959 return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000960 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000961 }
962
963 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000964 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000965
Dan Gohman8181bd12008-07-27 21:46:04 +0000966 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000967 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
968 // Operand #1 = Bytes To Pop
969 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
970
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000972 for (unsigned i = 0; i != RVLocs.size(); ++i) {
973 CCValAssign &VA = RVLocs[i];
974 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000975 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976
Chris Lattnerb56cc342008-03-11 03:23:40 +0000977 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
978 // the RET instruction and handled by the FP Stackifier.
979 if (RVLocs[i].getLocReg() == X86::ST0 ||
980 RVLocs[i].getLocReg() == X86::ST1) {
981 // If this is a copy from an xmm register to ST(0), use an FPExtend to
982 // change the value to the FP stack register class.
983 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
Dale Johannesence0805b2009-02-03 19:33:06 +0000984 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattnerb56cc342008-03-11 03:23:40 +0000985 RetOps.push_back(ValToCopy);
986 // Don't emit a copytoreg.
987 continue;
988 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000989
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000990 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 Flag = Chain.getValue(1);
992 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000993
994 // The x86-64 ABI for returning structs by value requires that we copy
995 // the sret argument into %rax for the return. We saved the argument into
996 // a virtual register in the entry block, so now we copy the value out
997 // and into %rax.
998 if (Subtarget->is64Bit() &&
999 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1000 MachineFunction &MF = DAG.getMachineFunction();
1001 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1002 unsigned Reg = FuncInfo->getSRetReturnReg();
1003 if (!Reg) {
1004 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1005 FuncInfo->setSRetReturnReg(Reg);
1006 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001007 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001008
1009 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
1010 Flag = Chain.getValue(1);
1011 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012
Chris Lattnerb56cc342008-03-11 03:23:40 +00001013 RetOps[0] = Chain; // Update chain.
1014
1015 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +00001016 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +00001017 RetOps.push_back(Flag);
1018
Dale Johannesence0805b2009-02-03 19:33:06 +00001019 return DAG.getNode(X86ISD::RET_FLAG, dl,
1020 MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001021}
1022
1023
1024/// LowerCallResult - Lower the result values of an ISD::CALL into the
1025/// appropriate copies out of appropriate physical registers. This assumes that
1026/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1027/// being lowered. The returns a SDNode with the same number of values as the
1028/// ISD::CALL.
1029SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +00001030LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031 unsigned CallingConv, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00001032
1033 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001034 // Assign locations to each value returned by this call.
1035 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +00001036 bool isVarArg = TheCall->isVarArg();
Edwin Törökaf8e1332009-02-01 18:15:56 +00001037 bool Is64Bit = Subtarget->is64Bit();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001038 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
1039 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1040
Dan Gohman8181bd12008-07-27 21:46:04 +00001041 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001042
1043 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001044 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00001045 MVT CopyVT = RVLocs[i].getValVT();
Edwin Törökaf8e1332009-02-01 18:15:56 +00001046
1047 // If this is x86-64, and we disabled SSE, we can't return FP values
1048 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
1049 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
1050 cerr << "SSE register return with SSE disabled\n";
1051 exit(1);
1052 }
1053
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001054 // If this is a call to a function that returns an fp value on the floating
1055 // point stack, but where we prefer to use the value in xmm registers, copy
1056 // 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 +00001057 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1058 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001059 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1060 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001063 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1064 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001065 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001066 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001067
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001068 if (CopyVT != RVLocs[i].getValVT()) {
1069 // Round the F80 the right size, which also moves to the appropriate xmm
1070 // register.
Dale Johannesence0805b2009-02-03 19:33:06 +00001071 Val = DAG.getNode(ISD::FP_ROUND, dl, RVLocs[i].getValVT(), Val,
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001072 // This truncation won't change the value.
1073 DAG.getIntPtrConstant(1));
1074 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001075
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001076 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001077 }
Duncan Sands698842f2008-07-02 17:40:58 +00001078
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001079 // Merge everything together with a MERGE_VALUES node.
1080 ResultVals.push_back(Chain);
Dale Johannesence0805b2009-02-03 19:33:06 +00001081 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
1082 &ResultVals[0], ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001083}
1084
1085
1086//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001087// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001088//===----------------------------------------------------------------------===//
1089// StdCall calling convention seems to be standard for many Windows' API
1090// routines and around. It differs from C calling convention just a little:
1091// callee should clean up the stack, not caller. Symbols should be also
1092// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001093// For info on fast calling convention see Fast Calling Convention (tail call)
1094// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001095
1096/// AddLiveIn - This helper function adds the specified physical register to the
1097/// MachineFunction as a live in value. It also creates a corresponding virtual
1098/// register for it.
1099static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1100 const TargetRegisterClass *RC) {
1101 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001102 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1103 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001104 return VReg;
1105}
1106
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001107/// CallIsStructReturn - Determines whether a CALL node uses struct return
1108/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001109static bool CallIsStructReturn(CallSDNode *TheCall) {
1110 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001111 if (!NumOps)
1112 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001113
Dan Gohman705e3f72008-09-13 01:54:27 +00001114 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001115}
1116
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001117/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1118/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001119static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001120 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001121 if (!NumArgs)
1122 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001123
1124 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001125}
1126
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001127/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1128/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001129/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001130bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001131 if (IsVarArg)
1132 return false;
1133
Dan Gohman705e3f72008-09-13 01:54:27 +00001134 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001135 default:
1136 return false;
1137 case CallingConv::X86_StdCall:
1138 return !Subtarget->is64Bit();
1139 case CallingConv::X86_FastCall:
1140 return !Subtarget->is64Bit();
1141 case CallingConv::Fast:
1142 return PerformTailCallOpt;
1143 }
1144}
1145
Dan Gohman705e3f72008-09-13 01:54:27 +00001146/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1147/// given CallingConvention value.
1148CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001149 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001150 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001151 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001152 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1153 return CC_X86_64_TailCall;
1154 else
1155 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001156 }
1157
Gordon Henriksen18ace102008-01-05 16:56:59 +00001158 if (CC == CallingConv::X86_FastCall)
1159 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001160 else if (CC == CallingConv::Fast)
1161 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001162 else
1163 return CC_X86_32_C;
1164}
1165
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001166/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1167/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001168NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001169X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001170 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001171 if (CC == CallingConv::X86_FastCall)
1172 return FastCall;
1173 else if (CC == CallingConv::X86_StdCall)
1174 return StdCall;
1175 return None;
1176}
1177
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001178
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001179/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1180/// in a register before calling.
1181bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1182 return !IsTailCall && !Is64Bit &&
1183 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1184 Subtarget->isPICStyleGOT();
1185}
1186
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001187/// CallRequiresFnAddressInReg - Check whether the call requires the function
1188/// address to be loaded in a register.
1189bool
1190X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1191 return !Is64Bit && IsTailCall &&
1192 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1193 Subtarget->isPICStyleGOT();
1194}
1195
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001196/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1197/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001198/// the specific parameter attribute. The copy will be passed as a byval
1199/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001200static SDValue
1201CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001202 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001203 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001204 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001205 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001206}
1207
Dan Gohman8181bd12008-07-27 21:46:04 +00001208SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001209 const CCValAssign &VA,
1210 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001211 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001212 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001213 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001214 ISD::ArgFlagsTy Flags =
1215 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001216 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001217 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001218
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001219 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1220 // changed with more analysis.
1221 // In case of tail call optimization mark all arguments mutable. Since they
1222 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001223 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001224 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001225 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001226 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001227 return FIN;
Dale Johannesence0805b2009-02-03 19:33:06 +00001228 return DAG.getLoad(VA.getValVT(), Op.getNode()->getDebugLoc(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001229 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001230}
1231
Dan Gohman8181bd12008-07-27 21:46:04 +00001232SDValue
1233X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001234 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001235 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Dale Johannesence0805b2009-02-03 19:33:06 +00001236 DebugLoc dl = Op.getNode()->getDebugLoc();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001237
1238 const Function* Fn = MF.getFunction();
1239 if (Fn->hasExternalLinkage() &&
1240 Subtarget->isTargetCygMing() &&
1241 Fn->getName() == "main")
1242 FuncInfo->setForceFramePointer(true);
1243
1244 // Decorate the function name.
1245 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1246
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001248 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001249 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001250 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001251 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001252 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001253
1254 assert(!(isVarArg && CC == CallingConv::Fast) &&
1255 "Var args not supported with calling convention fastcc");
1256
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001257 // Assign locations to all of the incoming arguments.
1258 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001259 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001260 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001261
Dan Gohman8181bd12008-07-27 21:46:04 +00001262 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001263 unsigned LastVal = ~0U;
1264 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1265 CCValAssign &VA = ArgLocs[i];
1266 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1267 // places.
1268 assert(VA.getValNo() != LastVal &&
1269 "Don't support value assigned to multiple locs yet");
1270 LastVal = VA.getValNo();
1271
1272 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001273 MVT RegVT = VA.getLocVT();
Devang Patelf3707e82009-01-05 17:31:22 +00001274 TargetRegisterClass *RC = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001275 if (RegVT == MVT::i32)
1276 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001277 else if (Is64Bit && RegVT == MVT::i64)
1278 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001279 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001280 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001281 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001282 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001283 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001284 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001285 else if (RegVT.isVector()) {
1286 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001287 if (!Is64Bit)
1288 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1289 else {
1290 // Darwin calling convention passes MMX values in either GPRs or
1291 // XMMs in x86-64. Other targets pass them in memory.
1292 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1293 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1294 RegVT = MVT::v2i64;
1295 } else {
1296 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1297 RegVT = MVT::i64;
1298 }
1299 }
1300 } else {
1301 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001303
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001304 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001305 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306
1307 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1308 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1309 // right size.
1310 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesence0805b2009-02-03 19:33:06 +00001311 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312 DAG.getValueType(VA.getValVT()));
1313 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesence0805b2009-02-03 19:33:06 +00001314 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001315 DAG.getValueType(VA.getValVT()));
1316
1317 if (VA.getLocInfo() != CCValAssign::Full)
Dale Johannesence0805b2009-02-03 19:33:06 +00001318 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001319
Gordon Henriksen18ace102008-01-05 16:56:59 +00001320 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001321 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001322 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Dale Johannesence0805b2009-02-03 19:33:06 +00001323 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Chengad6980b2008-04-25 20:13:28 +00001324 else if (RC == X86::VR128RegisterClass) {
Dale Johannesence0805b2009-02-03 19:33:06 +00001325 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1326 ArgValue, DAG.getConstant(0, MVT::i64));
1327 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Chengad6980b2008-04-25 20:13:28 +00001328 }
1329 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001330
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001331 ArgValues.push_back(ArgValue);
1332 } else {
1333 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001334 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001335 }
1336 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001337
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001338 // The x86-64 ABI for returning structs by value requires that we copy
1339 // the sret argument into %rax for the return. Save the argument into
1340 // a virtual register so that we can access it from the return points.
1341 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1342 MachineFunction &MF = DAG.getMachineFunction();
1343 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1344 unsigned Reg = FuncInfo->getSRetReturnReg();
1345 if (!Reg) {
1346 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1347 FuncInfo->setSRetReturnReg(Reg);
1348 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001349 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dale Johannesence0805b2009-02-03 19:33:06 +00001350 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001351 }
1352
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001354 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001355 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001356 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001357
1358 // If the function takes variable number of arguments, make a frame index for
1359 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001360 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001361 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1362 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1363 }
1364 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001365 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1366
1367 // FIXME: We should really autogenerate these arrays
1368 static const unsigned GPR64ArgRegsWin64[] = {
1369 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001370 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001371 static const unsigned XMMArgRegsWin64[] = {
1372 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1373 };
1374 static const unsigned GPR64ArgRegs64Bit[] = {
1375 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1376 };
1377 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001378 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1379 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1380 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001381 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1382
1383 if (IsWin64) {
1384 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1385 GPR64ArgRegs = GPR64ArgRegsWin64;
1386 XMMArgRegs = XMMArgRegsWin64;
1387 } else {
1388 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1389 GPR64ArgRegs = GPR64ArgRegs64Bit;
1390 XMMArgRegs = XMMArgRegs64Bit;
1391 }
1392 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1393 TotalNumIntRegs);
1394 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1395 TotalNumXMMRegs);
1396
Edwin Törökaf8e1332009-02-01 18:15:56 +00001397 assert((Subtarget->hasSSE1() || !NumXMMRegs) &&
1398 "SSE register cannot be used when SSE is disabled!");
1399 if (!Subtarget->hasSSE1()) {
1400 // Kernel mode asks for SSE to be disabled, so don't push them
1401 // on the stack.
1402 TotalNumXMMRegs = 0;
1403 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001404 // For X86-64, if there are vararg parameters that are passed via
1405 // registers, then we must store them to their spots on the stack so they
1406 // may be loaded by deferencing the result of va_next.
1407 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001408 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1409 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1410 TotalNumXMMRegs * 16, 16);
1411
Gordon Henriksen18ace102008-01-05 16:56:59 +00001412 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001413 SmallVector<SDValue, 8> MemOps;
1414 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00001415 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001416 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001417 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001418 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1419 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001420 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1421 SDValue Store =
Dale Johannesence0805b2009-02-03 19:33:06 +00001422 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001423 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001424 MemOps.push_back(Store);
Dale Johannesence0805b2009-02-03 19:33:06 +00001425 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001426 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001427 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001428
Gordon Henriksen18ace102008-01-05 16:56:59 +00001429 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesence0805b2009-02-03 19:33:06 +00001430 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001431 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001432 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001433 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1434 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001435 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1436 SDValue Store =
Dale Johannesence0805b2009-02-03 19:33:06 +00001437 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001438 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001439 MemOps.push_back(Store);
Dale Johannesence0805b2009-02-03 19:33:06 +00001440 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001441 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001442 }
1443 if (!MemOps.empty())
Dale Johannesence0805b2009-02-03 19:33:06 +00001444 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Gordon Henriksen18ace102008-01-05 16:56:59 +00001445 &MemOps[0], MemOps.size());
1446 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001447 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001448
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001449 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001450
Gordon Henriksen18ace102008-01-05 16:56:59 +00001451 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001452 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001453 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001454 BytesCallerReserves = 0;
1455 } else {
1456 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001457 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001458 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001459 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001460 BytesCallerReserves = StackSize;
1461 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001462
Gordon Henriksen18ace102008-01-05 16:56:59 +00001463 if (!Is64Bit) {
1464 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1465 if (CC == CallingConv::X86_FastCall)
1466 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1467 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001468
Anton Korobeynikove844e472007-08-15 17:12:32 +00001469 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001470
1471 // Return the new list of results.
Dale Johannesence0805b2009-02-03 19:33:06 +00001472 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sands42d7bb82008-12-01 11:41:29 +00001473 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001474}
1475
Dan Gohman8181bd12008-07-27 21:46:04 +00001476SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001477X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001478 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001479 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001480 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001481 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dale Johannesence0805b2009-02-03 19:33:06 +00001482 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohman1190f3a2008-02-07 16:28:05 +00001483 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001484 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesence0805b2009-02-03 19:33:06 +00001485 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001486 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001487 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001488 }
Dale Johannesence0805b2009-02-03 19:33:06 +00001489 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001490 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001491}
1492
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001493/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001494/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001495SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001496X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001497 SDValue &OutRetAddr,
1498 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001499 bool IsTailCall,
1500 bool Is64Bit,
Dale Johannesence0805b2009-02-03 19:33:06 +00001501 int FPDiff,
1502 DebugLoc dl) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001503 if (!IsTailCall || FPDiff==0) return Chain;
1504
1505 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001506 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001507 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001508
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001509 // Load the "old" Return address.
Dale Johannesence0805b2009-02-03 19:33:06 +00001510 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001511 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001512}
1513
1514/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1515/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001516static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001517EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001518 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesence0805b2009-02-03 19:33:06 +00001519 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001520 // Store the return address to the appropriate stack slot.
1521 if (!FPDiff) return Chain;
1522 // Calculate the new stack slot for the return address.
1523 int SlotSize = Is64Bit ? 8 : 4;
1524 int NewReturnAddrFI =
1525 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001526 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001527 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Dale Johannesence0805b2009-02-03 19:33:06 +00001528 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001529 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001530 return Chain;
1531}
1532
Dan Gohman8181bd12008-07-27 21:46:04 +00001533SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001534 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001535 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1536 SDValue Chain = TheCall->getChain();
1537 unsigned CC = TheCall->getCallingConv();
1538 bool isVarArg = TheCall->isVarArg();
1539 bool IsTailCall = TheCall->isTailCall() &&
1540 CC == CallingConv::Fast && PerformTailCallOpt;
1541 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001542 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001543 bool IsStructRet = CallIsStructReturn(TheCall);
Dale Johannesence0805b2009-02-03 19:33:06 +00001544 DebugLoc dl = TheCall->getDebugLoc();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001545
1546 assert(!(isVarArg && CC == CallingConv::Fast) &&
1547 "Var args not supported with calling convention fastcc");
1548
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001549 // Analyze operands of the call, assigning locations to each operand.
1550 SmallVector<CCValAssign, 16> ArgLocs;
1551 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001552 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001553
1554 // Get a count of how many bytes are to be pushed on the stack.
1555 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001556 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001557 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001558
Gordon Henriksen18ace102008-01-05 16:56:59 +00001559 int FPDiff = 0;
1560 if (IsTailCall) {
1561 // Lower arguments at fp - stackoffset + fpdiff.
1562 unsigned NumBytesCallerPushed =
1563 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1564 FPDiff = NumBytesCallerPushed - NumBytes;
1565
1566 // Set the delta of movement of the returnaddr stackslot.
1567 // But only set if delta is greater than previous delta.
1568 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1569 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1570 }
1571
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001572 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001573
Dan Gohman8181bd12008-07-27 21:46:04 +00001574 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001575 // Load return adress for tail calls.
1576 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
Dale Johannesence0805b2009-02-03 19:33:06 +00001577 FPDiff, dl);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001578
Dan Gohman8181bd12008-07-27 21:46:04 +00001579 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1580 SmallVector<SDValue, 8> MemOpChains;
1581 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001582
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001583 // Walk the register/memloc assignments, inserting copies/loads. In the case
1584 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001585 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1586 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001587 SDValue Arg = TheCall->getArg(i);
1588 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1589 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001590
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001591 // Promote the value if needed.
1592 switch (VA.getLocInfo()) {
1593 default: assert(0 && "Unknown loc info!");
1594 case CCValAssign::Full: break;
1595 case CCValAssign::SExt:
Dale Johannesence0805b2009-02-03 19:33:06 +00001596 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001597 break;
1598 case CCValAssign::ZExt:
Dale Johannesence0805b2009-02-03 19:33:06 +00001599 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001600 break;
1601 case CCValAssign::AExt:
Dale Johannesence0805b2009-02-03 19:33:06 +00001602 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001603 break;
1604 }
1605
1606 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001607 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001608 MVT RegVT = VA.getLocVT();
1609 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001610 switch (VA.getLocReg()) {
1611 default:
1612 break;
1613 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1614 case X86::R8: {
1615 // Special case: passing MMX values in GPR registers.
Dale Johannesence0805b2009-02-03 19:33:06 +00001616 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
Evan Cheng2aea0b42008-04-25 19:11:04 +00001617 break;
1618 }
1619 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1620 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1621 // Special case: passing MMX values in XMM registers.
Dale Johannesence0805b2009-02-03 19:33:06 +00001622 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1623 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1624 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v2i64,
1625 DAG.getNode(ISD::UNDEF, dl, MVT::v2i64), Arg,
1626 getMOVLMask(2, DAG, dl));
Evan Cheng2aea0b42008-04-25 19:11:04 +00001627 break;
1628 }
1629 }
1630 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001631 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1632 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001633 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001634 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001635 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001636 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1637
Dan Gohman705e3f72008-09-13 01:54:27 +00001638 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1639 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001640 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001641 }
1642 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001643
1644 if (!MemOpChains.empty())
Dale Johannesence0805b2009-02-03 19:33:06 +00001645 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001646 &MemOpChains[0], MemOpChains.size());
1647
1648 // Build a sequence of copy-to-reg nodes chained together with token chain
1649 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001650 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001651 // Tail call byval lowering might overwrite argument registers so in case of
1652 // tail call optimization the copies to registers are lowered later.
1653 if (!IsTailCall)
1654 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1655 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1656 InFlag);
1657 InFlag = Chain.getValue(1);
1658 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001659
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001660 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001661 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001662 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1663 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1664 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1665 InFlag);
1666 InFlag = Chain.getValue(1);
1667 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001668 // If we are tail calling and generating PIC/GOT style code load the address
1669 // of the callee into ecx. The value in ecx is used as target of the tail
1670 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1671 // calls on PIC/GOT architectures. Normally we would just put the address of
1672 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1673 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001674 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001675 // Note: The actual moving to ecx is done further down.
1676 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001677 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001678 !G->getGlobal()->hasProtectedVisibility())
1679 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001680 else if (isa<ExternalSymbolSDNode>(Callee))
1681 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001682 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001683
Gordon Henriksen18ace102008-01-05 16:56:59 +00001684 if (Is64Bit && isVarArg) {
1685 // From AMD64 ABI document:
1686 // For calls that may call functions that use varargs or stdargs
1687 // (prototype-less calls or calls to functions containing ellipsis (...) in
1688 // the declaration) %al is used as hidden argument to specify the number
1689 // of SSE registers used. The contents of %al do not need to match exactly
1690 // the number of registers, but must be an ubound on the number of SSE
1691 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001692
1693 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001694 // Count the number of XMM registers allocated.
1695 static const unsigned XMMArgRegs[] = {
1696 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1697 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1698 };
1699 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Edwin Törökaf8e1332009-02-01 18:15:56 +00001700 assert((Subtarget->hasSSE1() || !NumXMMRegs)
1701 && "SSE registers cannot be used when SSE is disabled");
Gordon Henriksen18ace102008-01-05 16:56:59 +00001702
1703 Chain = DAG.getCopyToReg(Chain, X86::AL,
1704 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1705 InFlag = Chain.getValue(1);
1706 }
1707
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001708
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001709 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001710 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001711 SmallVector<SDValue, 8> MemOpChains2;
1712 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001713 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001714 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001715 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001716 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1717 CCValAssign &VA = ArgLocs[i];
1718 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001719 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001720 SDValue Arg = TheCall->getArg(i);
1721 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001722 // Create frame index.
1723 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001724 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001725 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001726 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001727
Duncan Sandsc93fae32008-03-21 09:14:45 +00001728 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001729 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001730 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001731 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001732 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00001733 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001734
1735 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001736 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001737 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001738 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001739 MemOpChains2.push_back(
Dale Johannesence0805b2009-02-03 19:33:06 +00001740 DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001741 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001742 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001743 }
1744 }
1745
1746 if (!MemOpChains2.empty())
Dale Johannesence0805b2009-02-03 19:33:06 +00001747 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001748 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001749
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001750 // Copy arguments to their registers.
1751 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1752 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1753 InFlag);
1754 InFlag = Chain.getValue(1);
1755 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001756 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001757
Gordon Henriksen18ace102008-01-05 16:56:59 +00001758 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001759 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesence0805b2009-02-03 19:33:06 +00001760 FPDiff, dl);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001761 }
1762
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001763 // If the callee is a GlobalAddress node (quite common, every direct call is)
1764 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1765 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1766 // We should use extra load for direct calls to dllimported functions in
1767 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001768 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1769 getTargetMachine(), true))
Dan Gohman36322c72008-10-18 02:06:02 +00001770 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1771 G->getOffset());
Bill Wendlingfef06052008-09-16 21:48:12 +00001772 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1773 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001774 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001775 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001776
1777 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001778 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001779 Callee,InFlag);
1780 Callee = DAG.getRegister(Opc, getPointerTy());
1781 // Add register as live out.
1782 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001783 }
1784
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001785 // Returns a chain & a flag for retval copy to use.
1786 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001787 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001788
1789 if (IsTailCall) {
1790 Ops.push_back(Chain);
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001791 Ops.push_back(DAG.getIntPtrConstant(NumBytes, true));
1792 Ops.push_back(DAG.getIntPtrConstant(0, true));
Gabor Greif1c80d112008-08-28 21:40:38 +00001793 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001794 Ops.push_back(InFlag);
1795 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1796 InFlag = Chain.getValue(1);
1797
1798 // Returns a chain & a flag for retval copy to use.
1799 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1800 Ops.clear();
1801 }
1802
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001803 Ops.push_back(Chain);
1804 Ops.push_back(Callee);
1805
Gordon Henriksen18ace102008-01-05 16:56:59 +00001806 if (IsTailCall)
1807 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001808
Gordon Henriksen18ace102008-01-05 16:56:59 +00001809 // Add argument registers to the end of the list so that they are known live
1810 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001811 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1812 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1813 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001814
Evan Cheng8ba45e62008-03-18 23:36:35 +00001815 // Add an implicit use GOT pointer in EBX.
1816 if (!IsTailCall && !Is64Bit &&
1817 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1818 Subtarget->isPICStyleGOT())
1819 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1820
1821 // Add an implicit use of AL for x86 vararg functions.
1822 if (Is64Bit && isVarArg)
1823 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1824
Gabor Greif1c80d112008-08-28 21:40:38 +00001825 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001826 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001827
Gordon Henriksen18ace102008-01-05 16:56:59 +00001828 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001829 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001830 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesence0805b2009-02-03 19:33:06 +00001831 Chain = DAG.getNode(X86ISD::TAILCALL, dl,
Dan Gohman705e3f72008-09-13 01:54:27 +00001832 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001833
Gabor Greif1c80d112008-08-28 21:40:38 +00001834 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001835 }
1836
Dale Johannesence0805b2009-02-03 19:33:06 +00001837 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001838 InFlag = Chain.getValue(1);
1839
1840 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001841 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001842 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001843 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001844 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001845 // If this is is a call to a struct-return function, the callee
1846 // pops the hidden struct pointer, so we have to push it back.
1847 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001848 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001849 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001850 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001851
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001852 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001853 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001854 DAG.getIntPtrConstant(NumBytes, true),
1855 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1856 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001857 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001858 InFlag = Chain.getValue(1);
1859
1860 // Handle result values, copying them out of physregs into vregs that we
1861 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001862 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001863 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001864}
1865
1866
1867//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001868// Fast Calling Convention (tail call) implementation
1869//===----------------------------------------------------------------------===//
1870
1871// Like std call, callee cleans arguments, convention except that ECX is
1872// reserved for storing the tail called function address. Only 2 registers are
1873// free for argument passing (inreg). Tail call optimization is performed
1874// provided:
1875// * tailcallopt is enabled
1876// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001877// On X86_64 architecture with GOT-style position independent code only local
1878// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001879// To keep the stack aligned according to platform abi the function
1880// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1881// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001882// If a tail called function callee has more arguments than the caller the
1883// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001884// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001885// original REtADDR, but before the saved framepointer or the spilled registers
1886// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1887// stack layout:
1888// arg1
1889// arg2
1890// RETADDR
1891// [ new RETADDR
1892// move area ]
1893// (possible EBP)
1894// ESI
1895// EDI
1896// local1 ..
1897
1898/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1899/// for a 16 byte align requirement.
1900unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1901 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001902 MachineFunction &MF = DAG.getMachineFunction();
1903 const TargetMachine &TM = MF.getTarget();
1904 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1905 unsigned StackAlignment = TFI.getStackAlignment();
1906 uint64_t AlignMask = StackAlignment - 1;
1907 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001908 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001909 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1910 // Number smaller than 12 so just add the difference.
1911 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1912 } else {
1913 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1914 Offset = ((~AlignMask) & Offset) + StackAlignment +
1915 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001916 }
Evan Chengded8f902008-09-07 09:07:23 +00001917 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001918}
1919
1920/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001921/// following the call is a return. A function is eligible if caller/callee
1922/// calling conventions match, currently only fastcc supports tail calls, and
1923/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001924bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001925 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001926 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001927 if (!PerformTailCallOpt)
1928 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001929
Dan Gohman705e3f72008-09-13 01:54:27 +00001930 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001931 MachineFunction &MF = DAG.getMachineFunction();
1932 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001933 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001934 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001935 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001936 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001937 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001938 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001939 return true;
1940
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001941 // Can only do local tail calls (in same module, hidden or protected) on
1942 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001943 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1944 return G->getGlobal()->hasHiddenVisibility()
1945 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001946 }
1947 }
Evan Chenge7a87392007-11-02 01:26:22 +00001948
1949 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001950}
1951
Dan Gohmanca4857a2008-09-03 23:12:08 +00001952FastISel *
1953X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001954 MachineModuleInfo *mmo,
Devang Patelfcf1c752009-01-13 00:35:13 +00001955 DwarfWriter *dw,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001956 DenseMap<const Value *, unsigned> &vm,
1957 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001958 MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00001959 DenseMap<const AllocaInst *, int> &am
1960#ifndef NDEBUG
1961 , SmallSet<Instruction*, 8> &cil
1962#endif
1963 ) {
Devang Patelfcf1c752009-01-13 00:35:13 +00001964 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohman9dd43582008-10-14 23:54:11 +00001965#ifndef NDEBUG
1966 , cil
1967#endif
1968 );
Dan Gohman97805ee2008-08-19 21:32:53 +00001969}
1970
1971
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001972//===----------------------------------------------------------------------===//
1973// Other Lowering Hooks
1974//===----------------------------------------------------------------------===//
1975
1976
Dan Gohman8181bd12008-07-27 21:46:04 +00001977SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001978 MachineFunction &MF = DAG.getMachineFunction();
1979 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1980 int ReturnAddrIndex = FuncInfo->getRAIndex();
1981
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001982 if (ReturnAddrIndex == 0) {
1983 // Set up a frame object for the return address.
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001984 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001985 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001986 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001987 }
1988
1989 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1990}
1991
1992
Chris Lattnerebb91142008-12-24 23:53:05 +00001993/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
1994/// specific condition code, returning the condition code and the LHS/RHS of the
1995/// comparison to make.
1996static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1997 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001998 if (!isFP) {
1999 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2000 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2001 // X > -1 -> X == 0, jump !sign.
2002 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00002003 return X86::COND_NS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002004 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2005 // X < 0 -> X == 0, jump on sign.
Chris Lattnerebb91142008-12-24 23:53:05 +00002006 return X86::COND_S;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002007 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00002008 // X < 1 -> X <= 0
2009 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00002010 return X86::COND_LE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002011 }
2012 }
2013
2014 switch (SetCCOpcode) {
Chris Lattnerb8397512008-12-23 23:42:27 +00002015 default: assert(0 && "Invalid integer condition!");
Chris Lattnerebb91142008-12-24 23:53:05 +00002016 case ISD::SETEQ: return X86::COND_E;
2017 case ISD::SETGT: return X86::COND_G;
2018 case ISD::SETGE: return X86::COND_GE;
2019 case ISD::SETLT: return X86::COND_L;
2020 case ISD::SETLE: return X86::COND_LE;
2021 case ISD::SETNE: return X86::COND_NE;
2022 case ISD::SETULT: return X86::COND_B;
2023 case ISD::SETUGT: return X86::COND_A;
2024 case ISD::SETULE: return X86::COND_BE;
2025 case ISD::SETUGE: return X86::COND_AE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002026 }
Chris Lattnerb8397512008-12-23 23:42:27 +00002027 }
2028
2029 // First determine if it is required or is profitable to flip the operands.
Duncan Sandsc2a04622008-10-24 13:03:10 +00002030
Chris Lattnerb8397512008-12-23 23:42:27 +00002031 // If LHS is a foldable load, but RHS is not, flip the condition.
2032 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2033 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2034 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2035 std::swap(LHS, RHS);
Evan Chengfc937c92008-08-28 23:48:31 +00002036 }
2037
Chris Lattnerb8397512008-12-23 23:42:27 +00002038 switch (SetCCOpcode) {
2039 default: break;
2040 case ISD::SETOLT:
2041 case ISD::SETOLE:
2042 case ISD::SETUGT:
2043 case ISD::SETUGE:
2044 std::swap(LHS, RHS);
2045 break;
2046 }
2047
2048 // On a floating point condition, the flags are set as follows:
2049 // ZF PF CF op
2050 // 0 | 0 | 0 | X > Y
2051 // 0 | 0 | 1 | X < Y
2052 // 1 | 0 | 0 | X == Y
2053 // 1 | 1 | 1 | unordered
2054 switch (SetCCOpcode) {
Chris Lattnerebb91142008-12-24 23:53:05 +00002055 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattnerb8397512008-12-23 23:42:27 +00002056 case ISD::SETUEQ:
Chris Lattnerebb91142008-12-24 23:53:05 +00002057 case ISD::SETEQ: return X86::COND_E;
Chris Lattnerb8397512008-12-23 23:42:27 +00002058 case ISD::SETOLT: // flipped
2059 case ISD::SETOGT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002060 case ISD::SETGT: return X86::COND_A;
Chris Lattnerb8397512008-12-23 23:42:27 +00002061 case ISD::SETOLE: // flipped
2062 case ISD::SETOGE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002063 case ISD::SETGE: return X86::COND_AE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002064 case ISD::SETUGT: // flipped
2065 case ISD::SETULT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002066 case ISD::SETLT: return X86::COND_B;
Chris Lattnerb8397512008-12-23 23:42:27 +00002067 case ISD::SETUGE: // flipped
2068 case ISD::SETULE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002069 case ISD::SETLE: return X86::COND_BE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002070 case ISD::SETONE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002071 case ISD::SETNE: return X86::COND_NE;
2072 case ISD::SETUO: return X86::COND_P;
2073 case ISD::SETO: return X86::COND_NP;
Chris Lattnerb8397512008-12-23 23:42:27 +00002074 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002075}
2076
2077/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2078/// code. Current x86 isa includes the following FP cmov instructions:
2079/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2080static bool hasFPCMov(unsigned X86CC) {
2081 switch (X86CC) {
2082 default:
2083 return false;
2084 case X86::COND_B:
2085 case X86::COND_BE:
2086 case X86::COND_E:
2087 case X86::COND_P:
2088 case X86::COND_A:
2089 case X86::COND_AE:
2090 case X86::COND_NE:
2091 case X86::COND_NP:
2092 return true;
2093 }
2094}
2095
2096/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2097/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002098static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002099 if (Op.getOpcode() == ISD::UNDEF)
2100 return true;
2101
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002102 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002103 return (Val >= Low && Val < Hi);
2104}
2105
2106/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2107/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002108static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002109 if (Op.getOpcode() == ISD::UNDEF)
2110 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002111 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002112}
2113
2114/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2115/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2116bool X86::isPSHUFDMask(SDNode *N) {
2117 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2118
Dan Gohman7dc19012007-08-02 21:17:01 +00002119 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002120 return false;
2121
2122 // Check if the value doesn't reference the second vector.
2123 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002124 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002125 if (Arg.getOpcode() == ISD::UNDEF) continue;
2126 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002127 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002128 return false;
2129 }
2130
2131 return true;
2132}
2133
2134/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2135/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2136bool X86::isPSHUFHWMask(SDNode *N) {
2137 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2138
2139 if (N->getNumOperands() != 8)
2140 return false;
2141
2142 // Lower quadword copied in order.
2143 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002144 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002145 if (Arg.getOpcode() == ISD::UNDEF) continue;
2146 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002147 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002148 return false;
2149 }
2150
2151 // Upper quadword shuffled.
2152 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002153 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002154 if (Arg.getOpcode() == ISD::UNDEF) continue;
2155 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002156 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002157 if (Val < 4 || Val > 7)
2158 return false;
2159 }
2160
2161 return true;
2162}
2163
2164/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2165/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2166bool X86::isPSHUFLWMask(SDNode *N) {
2167 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2168
2169 if (N->getNumOperands() != 8)
2170 return false;
2171
2172 // Upper quadword copied in order.
2173 for (unsigned i = 4; i != 8; ++i)
2174 if (!isUndefOrEqual(N->getOperand(i), i))
2175 return false;
2176
2177 // Lower quadword shuffled.
2178 for (unsigned i = 0; i != 4; ++i)
2179 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2180 return false;
2181
2182 return true;
2183}
2184
2185/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2186/// specifies a shuffle of elements that is suitable for input to SHUFP*.
djgc2517d32009-01-26 04:35:06 +00002187template<class SDOperand>
2188static bool isSHUFPMask(SDOperand *Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002189 if (NumElems != 2 && NumElems != 4) return false;
2190
2191 unsigned Half = NumElems / 2;
2192 for (unsigned i = 0; i < Half; ++i)
2193 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2194 return false;
2195 for (unsigned i = Half; i < NumElems; ++i)
2196 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2197 return false;
2198
2199 return true;
2200}
2201
2202bool X86::isSHUFPMask(SDNode *N) {
2203 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2204 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2205}
2206
2207/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2208/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2209/// half elements to come from vector 1 (which would equal the dest.) and
2210/// the upper half to come from vector 2.
djgc2517d32009-01-26 04:35:06 +00002211template<class SDOperand>
2212static bool isCommutedSHUFP(SDOperand *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002213 if (NumOps != 2 && NumOps != 4) return false;
2214
2215 unsigned Half = NumOps / 2;
2216 for (unsigned i = 0; i < Half; ++i)
2217 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2218 return false;
2219 for (unsigned i = Half; i < NumOps; ++i)
2220 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2221 return false;
2222 return true;
2223}
2224
2225static bool isCommutedSHUFP(SDNode *N) {
2226 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2227 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2228}
2229
2230/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2231/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2232bool X86::isMOVHLPSMask(SDNode *N) {
2233 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2234
2235 if (N->getNumOperands() != 4)
2236 return false;
2237
2238 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2239 return isUndefOrEqual(N->getOperand(0), 6) &&
2240 isUndefOrEqual(N->getOperand(1), 7) &&
2241 isUndefOrEqual(N->getOperand(2), 2) &&
2242 isUndefOrEqual(N->getOperand(3), 3);
2243}
2244
2245/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2246/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2247/// <2, 3, 2, 3>
2248bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2249 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2250
2251 if (N->getNumOperands() != 4)
2252 return false;
2253
2254 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2255 return isUndefOrEqual(N->getOperand(0), 2) &&
2256 isUndefOrEqual(N->getOperand(1), 3) &&
2257 isUndefOrEqual(N->getOperand(2), 2) &&
2258 isUndefOrEqual(N->getOperand(3), 3);
2259}
2260
2261/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2262/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2263bool X86::isMOVLPMask(SDNode *N) {
2264 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2265
2266 unsigned NumElems = N->getNumOperands();
2267 if (NumElems != 2 && NumElems != 4)
2268 return false;
2269
2270 for (unsigned i = 0; i < NumElems/2; ++i)
2271 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2272 return false;
2273
2274 for (unsigned i = NumElems/2; i < NumElems; ++i)
2275 if (!isUndefOrEqual(N->getOperand(i), i))
2276 return false;
2277
2278 return true;
2279}
2280
2281/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2282/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2283/// and MOVLHPS.
2284bool X86::isMOVHPMask(SDNode *N) {
2285 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2286
2287 unsigned NumElems = N->getNumOperands();
2288 if (NumElems != 2 && NumElems != 4)
2289 return false;
2290
2291 for (unsigned i = 0; i < NumElems/2; ++i)
2292 if (!isUndefOrEqual(N->getOperand(i), i))
2293 return false;
2294
2295 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002296 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002297 if (!isUndefOrEqual(Arg, i + NumElems))
2298 return false;
2299 }
2300
2301 return true;
2302}
2303
2304/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2305/// specifies a shuffle of elements that is suitable for input to UNPCKL.
djgc2517d32009-01-26 04:35:06 +00002306template<class SDOperand>
2307bool static isUNPCKLMask(SDOperand *Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002308 bool V2IsSplat = false) {
2309 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2310 return false;
2311
2312 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002313 SDValue BitI = Elts[i];
2314 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002315 if (!isUndefOrEqual(BitI, j))
2316 return false;
2317 if (V2IsSplat) {
2318 if (isUndefOrEqual(BitI1, NumElts))
2319 return false;
2320 } else {
2321 if (!isUndefOrEqual(BitI1, j + NumElts))
2322 return false;
2323 }
2324 }
2325
2326 return true;
2327}
2328
2329bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2330 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2331 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2332}
2333
2334/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2335/// specifies a shuffle of elements that is suitable for input to UNPCKH.
djgc2517d32009-01-26 04:35:06 +00002336template<class SDOperand>
2337bool static isUNPCKHMask(SDOperand *Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002338 bool V2IsSplat = false) {
2339 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2340 return false;
2341
2342 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002343 SDValue BitI = Elts[i];
2344 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002345 if (!isUndefOrEqual(BitI, j + NumElts/2))
2346 return false;
2347 if (V2IsSplat) {
2348 if (isUndefOrEqual(BitI1, NumElts))
2349 return false;
2350 } else {
2351 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2352 return false;
2353 }
2354 }
2355
2356 return true;
2357}
2358
2359bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2360 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2361 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2362}
2363
2364/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2365/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2366/// <0, 0, 1, 1>
2367bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2368 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2369
2370 unsigned NumElems = N->getNumOperands();
2371 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2372 return false;
2373
2374 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002375 SDValue BitI = N->getOperand(i);
2376 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002377
2378 if (!isUndefOrEqual(BitI, j))
2379 return false;
2380 if (!isUndefOrEqual(BitI1, j))
2381 return false;
2382 }
2383
2384 return true;
2385}
2386
2387/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2388/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2389/// <2, 2, 3, 3>
2390bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2391 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2392
2393 unsigned NumElems = N->getNumOperands();
2394 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2395 return false;
2396
2397 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002398 SDValue BitI = N->getOperand(i);
2399 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002400
2401 if (!isUndefOrEqual(BitI, j))
2402 return false;
2403 if (!isUndefOrEqual(BitI1, j))
2404 return false;
2405 }
2406
2407 return true;
2408}
2409
2410/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2411/// specifies a shuffle of elements that is suitable for input to MOVSS,
2412/// MOVSD, and MOVD, i.e. setting the lowest element.
djgc2517d32009-01-26 04:35:06 +00002413template<class SDOperand>
2414static bool isMOVLMask(SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002415 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 return false;
2417
2418 if (!isUndefOrEqual(Elts[0], NumElts))
2419 return false;
2420
2421 for (unsigned i = 1; i < NumElts; ++i) {
2422 if (!isUndefOrEqual(Elts[i], i))
2423 return false;
2424 }
2425
2426 return true;
2427}
2428
2429bool X86::isMOVLMask(SDNode *N) {
2430 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2431 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2432}
2433
2434/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2435/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2436/// element of vector 2 and the other elements to come from vector 1 in order.
djgc2517d32009-01-26 04:35:06 +00002437template<class SDOperand>
2438static bool isCommutedMOVL(SDOperand *Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002439 bool V2IsSplat = false,
2440 bool V2IsUndef = false) {
2441 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2442 return false;
2443
2444 if (!isUndefOrEqual(Ops[0], 0))
2445 return false;
2446
2447 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002448 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002449 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2450 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2451 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2452 return false;
2453 }
2454
2455 return true;
2456}
2457
2458static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2459 bool V2IsUndef = false) {
2460 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2461 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2462 V2IsSplat, V2IsUndef);
2463}
2464
2465/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2466/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2467bool X86::isMOVSHDUPMask(SDNode *N) {
2468 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2469
2470 if (N->getNumOperands() != 4)
2471 return false;
2472
2473 // Expect 1, 1, 3, 3
2474 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002475 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002476 if (Arg.getOpcode() == ISD::UNDEF) continue;
2477 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002478 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002479 if (Val != 1) return false;
2480 }
2481
2482 bool HasHi = false;
2483 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002484 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002485 if (Arg.getOpcode() == ISD::UNDEF) continue;
2486 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002487 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002488 if (Val != 3) return false;
2489 HasHi = true;
2490 }
2491
2492 // Don't use movshdup if it can be done with a shufps.
2493 return HasHi;
2494}
2495
2496/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2497/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2498bool X86::isMOVSLDUPMask(SDNode *N) {
2499 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2500
2501 if (N->getNumOperands() != 4)
2502 return false;
2503
2504 // Expect 0, 0, 2, 2
2505 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002506 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002507 if (Arg.getOpcode() == ISD::UNDEF) continue;
2508 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002509 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002510 if (Val != 0) return false;
2511 }
2512
2513 bool HasHi = false;
2514 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002515 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002516 if (Arg.getOpcode() == ISD::UNDEF) continue;
2517 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002518 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002519 if (Val != 2) return false;
2520 HasHi = true;
2521 }
2522
2523 // Don't use movshdup if it can be done with a shufps.
2524 return HasHi;
2525}
2526
2527/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2528/// specifies a identity operation on the LHS or RHS.
2529static bool isIdentityMask(SDNode *N, bool RHS = false) {
2530 unsigned NumElems = N->getNumOperands();
2531 for (unsigned i = 0; i < NumElems; ++i)
2532 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2533 return false;
2534 return true;
2535}
2536
2537/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2538/// a splat of a single element.
2539static bool isSplatMask(SDNode *N) {
2540 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2541
2542 // This is a splat operation if each element of the permute is the same, and
2543 // if the value doesn't reference the second vector.
2544 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002545 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002546 unsigned i = 0;
2547 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002548 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002549 if (isa<ConstantSDNode>(Elt)) {
2550 ElementBase = Elt;
2551 break;
2552 }
2553 }
2554
Gabor Greif1c80d112008-08-28 21:40:38 +00002555 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002556 return false;
2557
2558 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002559 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002560 if (Arg.getOpcode() == ISD::UNDEF) continue;
2561 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2562 if (Arg != ElementBase) return false;
2563 }
2564
2565 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002566 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002567}
2568
Mon P Wang532c9632008-12-23 04:03:27 +00002569/// getSplatMaskEltNo - Given a splat mask, return the index to the element
2570/// we want to splat.
2571static SDValue getSplatMaskEltNo(SDNode *N) {
2572 assert(isSplatMask(N) && "Not a splat mask");
2573 unsigned NumElems = N->getNumOperands();
2574 SDValue ElementBase;
2575 unsigned i = 0;
2576 for (; i != NumElems; ++i) {
2577 SDValue Elt = N->getOperand(i);
2578 if (isa<ConstantSDNode>(Elt))
2579 return Elt;
2580 }
2581 assert(0 && " No splat value found!");
2582 return SDValue();
2583}
2584
2585
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002586/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2587/// a splat of a single element and it's a 2 or 4 element mask.
2588bool X86::isSplatMask(SDNode *N) {
2589 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2590
2591 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2592 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2593 return false;
2594 return ::isSplatMask(N);
2595}
2596
2597/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2598/// specifies a splat of zero element.
2599bool X86::isSplatLoMask(SDNode *N) {
2600 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2601
2602 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2603 if (!isUndefOrEqual(N->getOperand(i), 0))
2604 return false;
2605 return true;
2606}
2607
Evan Chenga2497eb2008-09-25 20:50:48 +00002608/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2609/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2610bool X86::isMOVDDUPMask(SDNode *N) {
2611 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2612
2613 unsigned e = N->getNumOperands() / 2;
2614 for (unsigned i = 0; i < e; ++i)
2615 if (!isUndefOrEqual(N->getOperand(i), i))
2616 return false;
2617 for (unsigned i = 0; i < e; ++i)
2618 if (!isUndefOrEqual(N->getOperand(e+i), i))
2619 return false;
2620 return true;
2621}
2622
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002623/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2624/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2625/// instructions.
2626unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2627 unsigned NumOperands = N->getNumOperands();
2628 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2629 unsigned Mask = 0;
2630 for (unsigned i = 0; i < NumOperands; ++i) {
2631 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002632 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002633 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002634 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002635 if (Val >= NumOperands) Val -= NumOperands;
2636 Mask |= Val;
2637 if (i != NumOperands - 1)
2638 Mask <<= Shift;
2639 }
2640
2641 return Mask;
2642}
2643
2644/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2645/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2646/// instructions.
2647unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2648 unsigned Mask = 0;
2649 // 8 nodes, but we only care about the last 4.
2650 for (unsigned i = 7; i >= 4; --i) {
2651 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002652 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002653 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002654 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002655 Mask |= (Val - 4);
2656 if (i != 4)
2657 Mask <<= 2;
2658 }
2659
2660 return Mask;
2661}
2662
2663/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2664/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2665/// instructions.
2666unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2667 unsigned Mask = 0;
2668 // 8 nodes, but we only care about the first 4.
2669 for (int i = 3; i >= 0; --i) {
2670 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002671 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002672 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002673 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002674 Mask |= Val;
2675 if (i != 0)
2676 Mask <<= 2;
2677 }
2678
2679 return Mask;
2680}
2681
2682/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2683/// specifies a 8 element shuffle that can be broken into a pair of
2684/// PSHUFHW and PSHUFLW.
2685static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2686 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2687
2688 if (N->getNumOperands() != 8)
2689 return false;
2690
2691 // Lower quadword shuffled.
2692 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002693 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002694 if (Arg.getOpcode() == ISD::UNDEF) continue;
2695 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002696 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002697 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002698 return false;
2699 }
2700
2701 // Upper quadword shuffled.
2702 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002703 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002704 if (Arg.getOpcode() == ISD::UNDEF) continue;
2705 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002706 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002707 if (Val < 4 || Val > 7)
2708 return false;
2709 }
2710
2711 return true;
2712}
2713
Chris Lattnere6aa3862007-11-25 00:24:49 +00002714/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002715/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002716static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2717 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002718 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002719 MVT VT = Op.getValueType();
2720 MVT MaskVT = Mask.getValueType();
2721 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002722 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002723 SmallVector<SDValue, 8> MaskVec;
Dale Johannesence0805b2009-02-03 19:33:06 +00002724 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002725
2726 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002727 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002728 if (Arg.getOpcode() == ISD::UNDEF) {
Dale Johannesence0805b2009-02-03 19:33:06 +00002729 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002730 continue;
2731 }
2732 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002733 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002734 if (Val < NumElems)
2735 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2736 else
2737 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2738 }
2739
2740 std::swap(V1, V2);
Dale Johannesence0805b2009-02-03 19:33:06 +00002741 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &MaskVec[0], NumElems);
2742 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002743}
2744
Evan Chenga6769df2007-12-07 21:30:01 +00002745/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2746/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002747static
Dale Johannesence0805b2009-02-03 19:33:06 +00002748SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002749 MVT MaskVT = Mask.getValueType();
2750 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002751 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002752 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002753 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002754 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002755 if (Arg.getOpcode() == ISD::UNDEF) {
Dale Johannesence0805b2009-02-03 19:33:06 +00002756 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, EltVT));
Evan Chengfca29242007-12-07 08:07:39 +00002757 continue;
2758 }
2759 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002760 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002761 if (Val < NumElems)
2762 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2763 else
2764 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2765 }
Dale Johannesence0805b2009-02-03 19:33:06 +00002766 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &MaskVec[0], NumElems);
Evan Chengfca29242007-12-07 08:07:39 +00002767}
2768
2769
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002770/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2771/// match movhlps. The lower half elements should come from upper half of
2772/// V1 (and in order), and the upper half elements should come from the upper
2773/// half of V2 (and in order).
2774static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2775 unsigned NumElems = Mask->getNumOperands();
2776 if (NumElems != 4)
2777 return false;
2778 for (unsigned i = 0, e = 2; i != e; ++i)
2779 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2780 return false;
2781 for (unsigned i = 2; i != 4; ++i)
2782 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2783 return false;
2784 return true;
2785}
2786
2787/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002788/// is promoted to a vector. It also returns the LoadSDNode by reference if
2789/// required.
2790static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002791 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2792 return false;
2793 N = N->getOperand(0).getNode();
2794 if (!ISD::isNON_EXTLoad(N))
2795 return false;
2796 if (LD)
2797 *LD = cast<LoadSDNode>(N);
2798 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002799}
2800
2801/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2802/// match movlp{s|d}. The lower half elements should come from lower half of
2803/// V1 (and in order), and the upper half elements should come from the upper
2804/// half of V2 (and in order). And since V1 will become the source of the
2805/// MOVLP, it must be either a vector load or a scalar load to vector.
2806static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2807 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2808 return false;
2809 // Is V2 is a vector load, don't do this transformation. We will try to use
2810 // load folding shufps op.
2811 if (ISD::isNON_EXTLoad(V2))
2812 return false;
2813
2814 unsigned NumElems = Mask->getNumOperands();
2815 if (NumElems != 2 && NumElems != 4)
2816 return false;
2817 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2818 if (!isUndefOrEqual(Mask->getOperand(i), i))
2819 return false;
2820 for (unsigned i = NumElems/2; i != NumElems; ++i)
2821 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2822 return false;
2823 return true;
2824}
2825
2826/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2827/// all the same.
2828static bool isSplatVector(SDNode *N) {
2829 if (N->getOpcode() != ISD::BUILD_VECTOR)
2830 return false;
2831
Dan Gohman8181bd12008-07-27 21:46:04 +00002832 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002833 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2834 if (N->getOperand(i) != SplatValue)
2835 return false;
2836 return true;
2837}
2838
2839/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2840/// to an undef.
2841static bool isUndefShuffle(SDNode *N) {
2842 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2843 return false;
2844
Dan Gohman8181bd12008-07-27 21:46:04 +00002845 SDValue V1 = N->getOperand(0);
2846 SDValue V2 = N->getOperand(1);
2847 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002848 unsigned NumElems = Mask.getNumOperands();
2849 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002850 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002851 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002852 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002853 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2854 return false;
2855 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2856 return false;
2857 }
2858 }
2859 return true;
2860}
2861
2862/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2863/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002864static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002865 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002866 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002867 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002868 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002869}
2870
2871/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2872/// to an zero vector.
2873static bool isZeroShuffle(SDNode *N) {
2874 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2875 return false;
2876
Dan Gohman8181bd12008-07-27 21:46:04 +00002877 SDValue V1 = N->getOperand(0);
2878 SDValue V2 = N->getOperand(1);
2879 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002880 unsigned NumElems = Mask.getNumOperands();
2881 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002882 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002883 if (Arg.getOpcode() == ISD::UNDEF)
2884 continue;
2885
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002886 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002887 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002888 unsigned Opc = V1.getNode()->getOpcode();
2889 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002890 continue;
2891 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002892 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002893 return false;
2894 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002895 unsigned Opc = V2.getNode()->getOpcode();
2896 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002897 continue;
2898 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002899 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002900 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002901 }
2902 }
2903 return true;
2904}
2905
2906/// getZeroVector - Returns a vector of specified type with all zero elements.
2907///
Dale Johannesence0805b2009-02-03 19:33:06 +00002908static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2909 DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002910 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002911
2912 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2913 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002914 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002915 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002916 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Dale Johannesence0805b2009-02-03 19:33:06 +00002917 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002918 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002919 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Dale Johannesence0805b2009-02-03 19:33:06 +00002920 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002921 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002922 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Dale Johannesence0805b2009-02-03 19:33:06 +00002923 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002924 }
Dale Johannesence0805b2009-02-03 19:33:06 +00002925 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002926}
2927
Chris Lattnere6aa3862007-11-25 00:24:49 +00002928/// getOnesVector - Returns a vector of specified type with all bits set.
2929///
Dale Johannesence0805b2009-02-03 19:33:06 +00002930static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002931 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002932
2933 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2934 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002935 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2936 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002937 if (VT.getSizeInBits() == 64) // MMX
Dale Johannesence0805b2009-02-03 19:33:06 +00002938 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002939 else // SSE
Dale Johannesence0805b2009-02-03 19:33:06 +00002940 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
2941 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002942}
2943
2944
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002945/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2946/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002947static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002948 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2949
2950 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002951 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002952 unsigned NumElems = Mask.getNumOperands();
2953 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002954 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002955 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002956 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002957 if (Val > NumElems) {
2958 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2959 Changed = true;
2960 }
2961 }
2962 MaskVec.push_back(Arg);
2963 }
2964
2965 if (Changed)
Dale Johannesence0805b2009-02-03 19:33:06 +00002966 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getNode()->getDebugLoc(),
2967 Mask.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002968 &MaskVec[0], MaskVec.size());
2969 return Mask;
2970}
2971
2972/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2973/// operation of specified width.
Dale Johannesence0805b2009-02-03 19:33:06 +00002974static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002975 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2976 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002977
Dan Gohman8181bd12008-07-27 21:46:04 +00002978 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002979 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2980 for (unsigned i = 1; i != NumElems; ++i)
2981 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00002982 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
2983 &MaskVec[0], MaskVec.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002984}
2985
2986/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2987/// of specified width.
Dale Johannesence0805b2009-02-03 19:33:06 +00002988static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG,
2989 DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002990 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2991 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002992 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002993 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2994 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2995 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2996 }
Dale Johannesence0805b2009-02-03 19:33:06 +00002997 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
2998 &MaskVec[0], MaskVec.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002999}
3000
3001/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
3002/// of specified width.
Dale Johannesence0805b2009-02-03 19:33:06 +00003003static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG,
3004 DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003005 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3006 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003007 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00003008 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003009 for (unsigned i = 0; i != Half; ++i) {
3010 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
3011 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
3012 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003013 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3014 &MaskVec[0], MaskVec.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003015}
3016
Chris Lattner2d91b962008-03-09 01:05:04 +00003017/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
3018/// element #0 of a vector with the specified index, leaving the rest of the
3019/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00003020static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Dale Johannesence0805b2009-02-03 19:33:06 +00003021 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003022 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3023 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003024 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00003025 // Element #0 of the result gets the elt we are replacing.
3026 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
3027 for (unsigned i = 1; i != NumElems; ++i)
3028 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003029 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3030 &MaskVec[0], MaskVec.size());
Chris Lattner2d91b962008-03-09 01:05:04 +00003031}
3032
Evan Chengbf8b2c52008-04-05 00:30:36 +00003033/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00003034static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003035 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
3036 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003037 if (PVT == VT)
3038 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00003039 SDValue V1 = Op.getOperand(0);
3040 SDValue Mask = Op.getOperand(2);
Mon P Wang532c9632008-12-23 04:03:27 +00003041 unsigned MaskNumElems = Mask.getNumOperands();
3042 unsigned NumElems = MaskNumElems;
Dale Johannesence0805b2009-02-03 19:33:06 +00003043 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003044 // Special handling of v4f32 -> v4i32.
3045 if (VT != MVT::v4f32) {
Mon P Wang532c9632008-12-23 04:03:27 +00003046 // Find which element we want to splat.
3047 SDNode* EltNoNode = getSplatMaskEltNo(Mask.getNode()).getNode();
3048 unsigned EltNo = cast<ConstantSDNode>(EltNoNode)->getZExtValue();
3049 // unpack elements to the correct location
Evan Chengbf8b2c52008-04-05 00:30:36 +00003050 while (NumElems > 4) {
Mon P Wang532c9632008-12-23 04:03:27 +00003051 if (EltNo < NumElems/2) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003052 Mask = getUnpacklMask(MaskNumElems, DAG, dl);
Mon P Wang532c9632008-12-23 04:03:27 +00003053 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00003054 Mask = getUnpackhMask(MaskNumElems, DAG, dl);
Mon P Wang532c9632008-12-23 04:03:27 +00003055 EltNo -= NumElems/2;
3056 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003057 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V1, Mask);
Evan Chengbf8b2c52008-04-05 00:30:36 +00003058 NumElems >>= 1;
3059 }
Mon P Wang532c9632008-12-23 04:03:27 +00003060 SDValue Cst = DAG.getConstant(EltNo, MVT::i32);
Dale Johannesence0805b2009-02-03 19:33:06 +00003061 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003062 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003063
Dale Johannesence0805b2009-02-03 19:33:06 +00003064 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
3065 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00003066 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dale Johannesence0805b2009-02-03 19:33:06 +00003067 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuffle);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003068}
3069
Evan Chenga2497eb2008-09-25 20:50:48 +00003070/// isVectorLoad - Returns true if the node is a vector load, a scalar
3071/// load that's promoted to vector, or a load bitcasted.
3072static bool isVectorLoad(SDValue Op) {
3073 assert(Op.getValueType().isVector() && "Expected a vector type");
3074 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
3075 Op.getOpcode() == ISD::BIT_CONVERT) {
3076 return isa<LoadSDNode>(Op.getOperand(0));
3077 }
3078 return isa<LoadSDNode>(Op);
3079}
3080
3081
3082/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3083///
3084static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3085 SelectionDAG &DAG, bool HasSSE3) {
3086 // If we have sse3 and shuffle has more than one use or input is a load, then
3087 // use movddup. Otherwise, use movlhps.
3088 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3089 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3090 MVT VT = Op.getValueType();
3091 if (VT == PVT)
3092 return Op;
Dale Johannesence0805b2009-02-03 19:33:06 +00003093 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Chenga2497eb2008-09-25 20:50:48 +00003094 unsigned NumElems = PVT.getVectorNumElements();
3095 if (NumElems == 2) {
3096 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Dale Johannesence0805b2009-02-03 19:33:06 +00003097 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chenga2497eb2008-09-25 20:50:48 +00003098 } else {
3099 assert(NumElems == 4);
3100 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3101 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
Dale Johannesence0805b2009-02-03 19:33:06 +00003102 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
3103 Cst0, Cst1, Cst0, Cst1);
Evan Chenga2497eb2008-09-25 20:50:48 +00003104 }
3105
Dale Johannesence0805b2009-02-03 19:33:06 +00003106 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
3107 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, PVT, V1,
3108 DAG.getNode(ISD::UNDEF, dl, PVT), Mask);
3109 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuffle);
Evan Chenga2497eb2008-09-25 20:50:48 +00003110}
3111
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003112/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003113/// vector of zero or undef vector. This produces a shuffle where the low
3114/// element of V2 is swizzled into the zero/undef vector, landing at element
3115/// 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 +00003116static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003117 bool isZero, bool HasSSE2,
3118 SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003119 DebugLoc dl = V2.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00003120 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003121 SDValue V1 = isZero
Dale Johannesence0805b2009-02-03 19:33:06 +00003122 ? getZeroVector(VT, HasSSE2, DAG, dl) : DAG.getNode(ISD::UNDEF, dl, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003123 unsigned NumElems = V2.getValueType().getVectorNumElements();
3124 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3125 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003126 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003127 for (unsigned i = 0; i != NumElems; ++i)
3128 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3129 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3130 else
3131 MaskVec.push_back(DAG.getConstant(i, EVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003132 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003133 &MaskVec[0], MaskVec.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00003134 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003135}
3136
Evan Chengdea99362008-05-29 08:22:04 +00003137/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3138/// a shuffle that is zero.
3139static
Dan Gohman8181bd12008-07-27 21:46:04 +00003140unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003141 unsigned NumElems, bool Low,
3142 SelectionDAG &DAG) {
3143 unsigned NumZeros = 0;
3144 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003145 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003146 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003147 if (Idx.getOpcode() == ISD::UNDEF) {
3148 ++NumZeros;
3149 continue;
3150 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003151 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3152 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003153 ++NumZeros;
3154 else
3155 break;
3156 }
3157 return NumZeros;
3158}
3159
3160/// isVectorShift - Returns true if the shuffle can be implemented as a
3161/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003162static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3163 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003164 unsigned NumElems = Mask.getNumOperands();
3165
3166 isLeft = true;
3167 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3168 if (!NumZeros) {
3169 isLeft = false;
3170 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3171 if (!NumZeros)
3172 return false;
3173 }
3174
3175 bool SeenV1 = false;
3176 bool SeenV2 = false;
3177 for (unsigned i = NumZeros; i < NumElems; ++i) {
3178 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003179 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003180 if (Idx.getOpcode() == ISD::UNDEF)
3181 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003182 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003183 if (Index < NumElems)
3184 SeenV1 = true;
3185 else {
3186 Index -= NumElems;
3187 SeenV2 = true;
3188 }
3189 if (Index != Val)
3190 return false;
3191 }
3192 if (SeenV1 && SeenV2)
3193 return false;
3194
3195 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3196 ShAmt = NumZeros;
3197 return true;
3198}
3199
3200
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003201/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3202///
Dan Gohman8181bd12008-07-27 21:46:04 +00003203static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003204 unsigned NumNonZero, unsigned NumZero,
3205 SelectionDAG &DAG, TargetLowering &TLI) {
3206 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003207 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003208
Dale Johannesence0805b2009-02-03 19:33:06 +00003209 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00003210 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003211 bool First = true;
3212 for (unsigned i = 0; i < 16; ++i) {
3213 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3214 if (ThisIsNonZero && First) {
3215 if (NumZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003216 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003217 else
Dale Johannesence0805b2009-02-03 19:33:06 +00003218 V = DAG.getNode(ISD::UNDEF, dl, MVT::v8i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003219 First = false;
3220 }
3221
3222 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003223 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003224 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3225 if (LastIsNonZero) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003226 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
3227 MVT::i16, Op.getOperand(i-1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003228 }
3229 if (ThisIsNonZero) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003230 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3231 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003232 ThisElt, DAG.getConstant(8, MVT::i8));
3233 if (LastIsNonZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003234 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003235 } else
3236 ThisElt = LastElt;
3237
Gabor Greif1c80d112008-08-28 21:40:38 +00003238 if (ThisElt.getNode())
Dale Johannesence0805b2009-02-03 19:33:06 +00003239 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003240 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003241 }
3242 }
3243
Dale Johannesence0805b2009-02-03 19:33:06 +00003244 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003245}
3246
3247/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3248///
Dan Gohman8181bd12008-07-27 21:46:04 +00003249static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003250 unsigned NumNonZero, unsigned NumZero,
3251 SelectionDAG &DAG, TargetLowering &TLI) {
3252 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003253 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003254
Dale Johannesence0805b2009-02-03 19:33:06 +00003255 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00003256 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003257 bool First = true;
3258 for (unsigned i = 0; i < 8; ++i) {
3259 bool isNonZero = (NonZeros & (1 << i)) != 0;
3260 if (isNonZero) {
3261 if (First) {
3262 if (NumZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003263 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003264 else
Dale Johannesence0805b2009-02-03 19:33:06 +00003265 V = DAG.getNode(ISD::UNDEF, dl, MVT::v8i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003266 First = false;
3267 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003268 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
3269 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003270 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003271 }
3272 }
3273
3274 return V;
3275}
3276
Evan Chengdea99362008-05-29 08:22:04 +00003277/// getVShift - Return a vector logical shift node.
3278///
Dan Gohman8181bd12008-07-27 21:46:04 +00003279static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003280 unsigned NumBits, SelectionDAG &DAG,
Dale Johannesence0805b2009-02-03 19:33:06 +00003281 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003282 bool isMMX = VT.getSizeInBits() == 64;
3283 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003284 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesence0805b2009-02-03 19:33:06 +00003285 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3286 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3287 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003288 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003289}
3290
Dan Gohman8181bd12008-07-27 21:46:04 +00003291SDValue
3292X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003293 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattnere6aa3862007-11-25 00:24:49 +00003294 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003295 if (ISD::isBuildVectorAllZeros(Op.getNode())
3296 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003297 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3298 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3299 // eliminated on x86-32 hosts.
3300 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3301 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003302
Gabor Greif1c80d112008-08-28 21:40:38 +00003303 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00003304 return getOnesVector(Op.getValueType(), DAG, dl);
3305 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003306 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003307
Duncan Sands92c43912008-06-06 12:08:01 +00003308 MVT VT = Op.getValueType();
3309 MVT EVT = VT.getVectorElementType();
3310 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003311
3312 unsigned NumElems = Op.getNumOperands();
3313 unsigned NumZero = 0;
3314 unsigned NumNonZero = 0;
3315 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003316 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003317 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003318 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003319 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003320 if (Elt.getOpcode() == ISD::UNDEF)
3321 continue;
3322 Values.insert(Elt);
3323 if (Elt.getOpcode() != ISD::Constant &&
3324 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003325 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003326 if (isZeroNode(Elt))
3327 NumZero++;
3328 else {
3329 NonZeros |= (1 << i);
3330 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003331 }
3332 }
3333
3334 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003335 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesence0805b2009-02-03 19:33:06 +00003336 return DAG.getNode(ISD::UNDEF, dl, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003337 }
3338
Chris Lattner66a4dda2008-03-09 05:42:06 +00003339 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003340 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003341 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003342 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003343
Chris Lattner2d91b962008-03-09 01:05:04 +00003344 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3345 // the value are obviously zero, truncate the value to i32 and do the
3346 // insertion that way. Only do this if the value is non-constant or if the
3347 // value is a constant being inserted into element 0. It is cheaper to do
3348 // a constant pool load than it is to do a movd + shuffle.
3349 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3350 (!IsAllConstants || Idx == 0)) {
3351 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3352 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003353 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3354 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003355
3356 // Truncate the value (which may itself be a constant) to i32, and
3357 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesence0805b2009-02-03 19:33:06 +00003358 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3359 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003360 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3361 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003362
3363 // Now we have our 32-bit value zero extended in the low element of
3364 // a vector. If Idx != 0, swizzle it into place.
3365 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003366 SDValue Ops[] = {
Dale Johannesence0805b2009-02-03 19:33:06 +00003367 Item, DAG.getNode(ISD::UNDEF, dl, Item.getValueType()),
3368 getSwapEltZeroMask(VecElts, Idx, DAG, dl)
Chris Lattner2d91b962008-03-09 01:05:04 +00003369 };
Dale Johannesence0805b2009-02-03 19:33:06 +00003370 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VecVT, Ops, 3);
Chris Lattner2d91b962008-03-09 01:05:04 +00003371 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003372 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner2d91b962008-03-09 01:05:04 +00003373 }
3374 }
3375
Chris Lattnerac914892008-03-08 22:59:52 +00003376 // If we have a constant or non-constant insertion into the low element of
3377 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3378 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3379 // depending on what the source datatype is. Because we can only get here
3380 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3381 if (Idx == 0 &&
3382 // Don't do this for i64 values on x86-32.
3383 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003384 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003385 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003386 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3387 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003388 }
Evan Chengdea99362008-05-29 08:22:04 +00003389
3390 // Is it a vector logical left shift?
3391 if (NumElems == 2 && Idx == 1 &&
3392 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003393 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003394 return getVShift(true, VT,
3395 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
Dale Johannesence0805b2009-02-03 19:33:06 +00003396 NumBits/2, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00003397 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003398
3399 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003400 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003401
Chris Lattnerac914892008-03-08 22:59:52 +00003402 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3403 // is a non-constant being inserted into an element other than the low one,
3404 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3405 // movd/movss) to move this into the low element, then shuffle it into
3406 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003407 if (EVTBits == 32) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003408 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003409
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003410 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003411 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3412 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003413 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3414 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003415 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003416 for (unsigned i = 0; i < NumElems; i++)
3417 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003418 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003419 &MaskVec[0], MaskVec.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00003420 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, Item,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003421 DAG.getNode(ISD::UNDEF, VT), Mask);
3422 }
3423 }
3424
Chris Lattner66a4dda2008-03-09 05:42:06 +00003425 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3426 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003427 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003428
Dan Gohman21463242007-07-24 22:55:08 +00003429 // A vector full of immediates; various special cases are already
3430 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003431 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003432 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003433
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003434 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003435 if (EVTBits == 64) {
3436 if (NumNonZero == 1) {
3437 // One half is zero or undef.
3438 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesence0805b2009-02-03 19:33:06 +00003439 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003440 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003441 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3442 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003443 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003444 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003445 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003446
3447 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3448 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003449 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003450 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003451 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003452 }
3453
3454 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003455 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003456 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003457 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003458 }
3459
3460 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003461 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003462 V.resize(NumElems);
3463 if (NumElems == 4 && NumZero > 0) {
3464 for (unsigned i = 0; i < 4; ++i) {
3465 bool isZero = !(NonZeros & (1 << i));
3466 if (isZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003467 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003468 else
Dale Johannesence0805b2009-02-03 19:33:06 +00003469 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003470 }
3471
3472 for (unsigned i = 0; i < 2; ++i) {
3473 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3474 default: break;
3475 case 0:
3476 V[i] = V[i*2]; // Must be a zero vector.
3477 break;
3478 case 1:
Dale Johannesence0805b2009-02-03 19:33:06 +00003479 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i*2+1], V[i*2],
3480 getMOVLMask(NumElems, DAG, dl));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003481 break;
3482 case 2:
Dale Johannesence0805b2009-02-03 19:33:06 +00003483 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i*2], V[i*2+1],
3484 getMOVLMask(NumElems, DAG, dl));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003485 break;
3486 case 3:
Dale Johannesence0805b2009-02-03 19:33:06 +00003487 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i*2], V[i*2+1],
3488 getUnpacklMask(NumElems, DAG, dl));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003489 break;
3490 }
3491 }
3492
Duncan Sands92c43912008-06-06 12:08:01 +00003493 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3494 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003495 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003496 bool Reverse = (NonZeros & 0x3) == 2;
3497 for (unsigned i = 0; i < 2; ++i)
3498 if (Reverse)
3499 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3500 else
3501 MaskVec.push_back(DAG.getConstant(i, EVT));
3502 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3503 for (unsigned i = 0; i < 2; ++i)
3504 if (Reverse)
3505 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3506 else
3507 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003508 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003509 &MaskVec[0], MaskVec.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00003510 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[0], V[1], ShufMask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003511 }
3512
3513 if (Values.size() > 2) {
3514 // Expand into a number of unpckl*.
3515 // e.g. for v4f32
3516 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3517 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3518 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dale Johannesence0805b2009-02-03 19:33:06 +00003519 SDValue UnpckMask = getUnpacklMask(NumElems, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003520 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesence0805b2009-02-03 19:33:06 +00003521 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003522 NumElems >>= 1;
3523 while (NumElems != 0) {
3524 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesence0805b2009-02-03 19:33:06 +00003525 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i], V[i + NumElems],
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003526 UnpckMask);
3527 NumElems >>= 1;
3528 }
3529 return V[0];
3530 }
3531
Dan Gohman8181bd12008-07-27 21:46:04 +00003532 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003533}
3534
Evan Chengfca29242007-12-07 08:07:39 +00003535static
Dan Gohman8181bd12008-07-27 21:46:04 +00003536SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003537 SDValue PermMask, SelectionDAG &DAG,
Dale Johannesence0805b2009-02-03 19:33:06 +00003538 TargetLowering &TLI, DebugLoc dl) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003539 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003540 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3541 MVT MaskEVT = MaskVT.getVectorElementType();
3542 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003543 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3544 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003545
3546 // First record which half of which vector the low elements come from.
3547 SmallVector<unsigned, 4> LowQuad(4);
3548 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003549 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003550 if (Elt.getOpcode() == ISD::UNDEF)
3551 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003552 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003553 int QuadIdx = EltIdx / 4;
3554 ++LowQuad[QuadIdx];
3555 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003556
Evan Cheng75184a92007-12-11 01:46:18 +00003557 int BestLowQuad = -1;
3558 unsigned MaxQuad = 1;
3559 for (unsigned i = 0; i < 4; ++i) {
3560 if (LowQuad[i] > MaxQuad) {
3561 BestLowQuad = i;
3562 MaxQuad = LowQuad[i];
3563 }
Evan Chengfca29242007-12-07 08:07:39 +00003564 }
3565
Evan Cheng75184a92007-12-11 01:46:18 +00003566 // Record which half of which vector the high elements come from.
3567 SmallVector<unsigned, 4> HighQuad(4);
3568 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003569 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003570 if (Elt.getOpcode() == ISD::UNDEF)
3571 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003572 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003573 int QuadIdx = EltIdx / 4;
3574 ++HighQuad[QuadIdx];
3575 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003576
Evan Cheng75184a92007-12-11 01:46:18 +00003577 int BestHighQuad = -1;
3578 MaxQuad = 1;
3579 for (unsigned i = 0; i < 4; ++i) {
3580 if (HighQuad[i] > MaxQuad) {
3581 BestHighQuad = i;
3582 MaxQuad = HighQuad[i];
3583 }
3584 }
3585
3586 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3587 if (BestLowQuad != -1 || BestHighQuad != -1) {
3588 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003589 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003590
Evan Cheng75184a92007-12-11 01:46:18 +00003591 if (BestLowQuad != -1)
3592 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3593 else
3594 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003595
Evan Cheng75184a92007-12-11 01:46:18 +00003596 if (BestHighQuad != -1)
3597 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3598 else
3599 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003600
Dale Johannesence0805b2009-02-03 19:33:06 +00003601 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, &MaskVec[0],2);
3602 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v2i64,
3603 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3604 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), Mask);
3605 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng75184a92007-12-11 01:46:18 +00003606
3607 // Now sort high and low parts separately.
3608 BitVector InOrder(8);
3609 if (BestLowQuad != -1) {
3610 // Sort lower half in order using PSHUFLW.
3611 MaskVec.clear();
3612 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003613
Evan Cheng75184a92007-12-11 01:46:18 +00003614 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003615 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003616 if (Elt.getOpcode() == ISD::UNDEF) {
3617 MaskVec.push_back(Elt);
3618 InOrder.set(i);
3619 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003620 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003621 if (EltIdx != i)
3622 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003623
Evan Cheng75184a92007-12-11 01:46:18 +00003624 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003625
Evan Cheng75184a92007-12-11 01:46:18 +00003626 // If this element is in the right place after this shuffle, then
3627 // remember it.
3628 if ((int)(EltIdx / 4) == BestLowQuad)
3629 InOrder.set(i);
3630 }
3631 }
3632 if (AnyOutOrder) {
3633 for (unsigned i = 4; i != 8; ++i)
3634 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003635 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3636 &MaskVec[0], 8);
3637 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v8i16,
3638 NewV, NewV, Mask);
Evan Cheng75184a92007-12-11 01:46:18 +00003639 }
3640 }
3641
3642 if (BestHighQuad != -1) {
3643 // Sort high half in order using PSHUFHW if possible.
3644 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003645
Evan Cheng75184a92007-12-11 01:46:18 +00003646 for (unsigned i = 0; i != 4; ++i)
3647 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003648
Evan Cheng75184a92007-12-11 01:46:18 +00003649 bool AnyOutOrder = false;
3650 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003651 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003652 if (Elt.getOpcode() == ISD::UNDEF) {
3653 MaskVec.push_back(Elt);
3654 InOrder.set(i);
3655 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003656 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003657 if (EltIdx != i)
3658 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003659
Evan Cheng75184a92007-12-11 01:46:18 +00003660 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003661
Evan Cheng75184a92007-12-11 01:46:18 +00003662 // If this element is in the right place after this shuffle, then
3663 // remember it.
3664 if ((int)(EltIdx / 4) == BestHighQuad)
3665 InOrder.set(i);
3666 }
3667 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003668
Evan Cheng75184a92007-12-11 01:46:18 +00003669 if (AnyOutOrder) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003670 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl,
3671 MaskVT, &MaskVec[0], 8);
3672 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v8i16,
3673 NewV, NewV, Mask);
Evan Cheng75184a92007-12-11 01:46:18 +00003674 }
3675 }
3676
3677 // The other elements are put in the right place using pextrw and pinsrw.
3678 for (unsigned i = 0; i != 8; ++i) {
3679 if (InOrder[i])
3680 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003681 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003682 if (Elt.getOpcode() == ISD::UNDEF)
3683 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003684 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003685 SDValue ExtOp = (EltIdx < 8)
Dale Johannesence0805b2009-02-03 19:33:06 +00003686 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003687 DAG.getConstant(EltIdx, PtrVT))
Dale Johannesence0805b2009-02-03 19:33:06 +00003688 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003689 DAG.getConstant(EltIdx - 8, PtrVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003690 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Evan Cheng75184a92007-12-11 01:46:18 +00003691 DAG.getConstant(i, PtrVT));
3692 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003693
Evan Cheng75184a92007-12-11 01:46:18 +00003694 return NewV;
3695 }
3696
Bill Wendling2c7cd592008-08-21 22:35:37 +00003697 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3698 // few as possible. First, let's find out how many elements are already in the
3699 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003700 unsigned V1InOrder = 0;
3701 unsigned V1FromV1 = 0;
3702 unsigned V2InOrder = 0;
3703 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003704 SmallVector<SDValue, 8> V1Elts;
3705 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003706 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003707 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003708 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003709 V1Elts.push_back(Elt);
3710 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003711 ++V1InOrder;
3712 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003713 continue;
3714 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003715 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003716 if (EltIdx == i) {
3717 V1Elts.push_back(Elt);
3718 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3719 ++V1InOrder;
3720 } else if (EltIdx == i+8) {
3721 V1Elts.push_back(Elt);
3722 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3723 ++V2InOrder;
3724 } else if (EltIdx < 8) {
3725 V1Elts.push_back(Elt);
Mon P Wang75498182009-01-28 23:11:14 +00003726 V2Elts.push_back(DAG.getConstant(EltIdx+8, MaskEVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003727 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003728 } else {
Mon P Wang532c9632008-12-23 04:03:27 +00003729 V1Elts.push_back(Elt);
Evan Cheng75184a92007-12-11 01:46:18 +00003730 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3731 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003732 }
3733 }
3734
3735 if (V2InOrder > V1InOrder) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003736 PermMask = CommuteVectorShuffleMask(PermMask, DAG, dl);
Evan Chengfca29242007-12-07 08:07:39 +00003737 std::swap(V1, V2);
3738 std::swap(V1Elts, V2Elts);
3739 std::swap(V1FromV1, V2FromV2);
3740 }
3741
Evan Cheng75184a92007-12-11 01:46:18 +00003742 if ((V1FromV1 + V1InOrder) != 8) {
3743 // Some elements are from V2.
3744 if (V1FromV1) {
3745 // If there are elements that are from V1 but out of place,
3746 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003747 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003748 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003749 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003750 if (Elt.getOpcode() == ISD::UNDEF) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003751 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003752 continue;
3753 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003754 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003755 if (EltIdx >= 8)
Dale Johannesence0805b2009-02-03 19:33:06 +00003756 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003757 else
3758 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3759 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003760 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &MaskVec[0], 8);
3761 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003762 }
Evan Cheng75184a92007-12-11 01:46:18 +00003763
3764 NewV = V1;
3765 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003766 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003767 if (Elt.getOpcode() == ISD::UNDEF)
3768 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003769 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003770 if (EltIdx < 8)
3771 continue;
Dale Johannesence0805b2009-02-03 19:33:06 +00003772 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003773 DAG.getConstant(EltIdx - 8, PtrVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003774 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Evan Cheng75184a92007-12-11 01:46:18 +00003775 DAG.getConstant(i, PtrVT));
3776 }
3777 return NewV;
3778 } else {
3779 // All elements are from V1.
3780 NewV = V1;
3781 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003782 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003783 if (Elt.getOpcode() == ISD::UNDEF)
3784 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003785 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dale Johannesence0805b2009-02-03 19:33:06 +00003786 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003787 DAG.getConstant(EltIdx, PtrVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003788 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Evan Cheng75184a92007-12-11 01:46:18 +00003789 DAG.getConstant(i, PtrVT));
3790 }
3791 return NewV;
3792 }
3793}
3794
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003795/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3796/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3797/// done when every pair / quad of shuffle mask elements point to elements in
3798/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003799/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3800static
Dan Gohman8181bd12008-07-27 21:46:04 +00003801SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003802 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003803 SDValue PermMask, SelectionDAG &DAG,
Dale Johannesence0805b2009-02-03 19:33:06 +00003804 TargetLowering &TLI, DebugLoc dl) {
Evan Cheng75184a92007-12-11 01:46:18 +00003805 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003806 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003807 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003808 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003809 MVT NewVT = MaskVT;
3810 switch (VT.getSimpleVT()) {
3811 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003812 case MVT::v4f32: NewVT = MVT::v2f64; break;
3813 case MVT::v4i32: NewVT = MVT::v2i64; break;
3814 case MVT::v8i16: NewVT = MVT::v4i32; break;
3815 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003816 }
3817
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003818 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003819 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003820 NewVT = MVT::v2i64;
3821 else
3822 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003823 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003824 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003825 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003826 for (unsigned i = 0; i < NumElems; i += Scale) {
3827 unsigned StartIdx = ~0U;
3828 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003829 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003830 if (Elt.getOpcode() == ISD::UNDEF)
3831 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003832 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003833 if (StartIdx == ~0U)
3834 StartIdx = EltIdx - (EltIdx % Scale);
3835 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003836 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003837 }
3838 if (StartIdx == ~0U)
Dale Johannesence0805b2009-02-03 19:33:06 +00003839 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003840 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003841 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003842 }
3843
Dale Johannesence0805b2009-02-03 19:33:06 +00003844 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3845 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
3846 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, NewVT, V1, V2,
3847 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003848 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003849}
3850
Evan Chenge9b9c672008-05-09 21:53:03 +00003851/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003852///
Dan Gohman8181bd12008-07-27 21:46:04 +00003853static SDValue getVZextMovL(MVT VT, MVT OpVT,
3854 SDValue SrcOp, SelectionDAG &DAG,
Dale Johannesence0805b2009-02-03 19:33:06 +00003855 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003856 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3857 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003858 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003859 LD = dyn_cast<LoadSDNode>(SrcOp);
3860 if (!LD) {
3861 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3862 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003863 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003864 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3865 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3866 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3867 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3868 // PR2108
3869 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesence0805b2009-02-03 19:33:06 +00003870 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3871 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3872 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3873 OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003874 SrcOp.getOperand(0)
3875 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003876 }
3877 }
3878 }
3879
Dale Johannesence0805b2009-02-03 19:33:06 +00003880 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3881 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3882 DAG.getNode(ISD::BIT_CONVERT, dl,
3883 OpVT, SrcOp)));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003884}
3885
Evan Chengf50554e2008-07-22 21:13:36 +00003886/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3887/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003888static SDValue
3889LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
Dale Johannesence0805b2009-02-03 19:33:06 +00003890 SDValue PermMask, MVT VT, SelectionDAG &DAG,
3891 DebugLoc dl) {
Evan Chengf50554e2008-07-22 21:13:36 +00003892 MVT MaskVT = PermMask.getValueType();
3893 MVT MaskEVT = MaskVT.getVectorElementType();
3894 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003895 Locs.resize(4);
Dale Johannesence0805b2009-02-03 19:33:06 +00003896 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003897 unsigned NumHi = 0;
3898 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003899 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003900 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003901 if (Elt.getOpcode() == ISD::UNDEF) {
3902 Locs[i] = std::make_pair(-1, -1);
3903 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003904 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003905 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003906 if (Val < 4) {
3907 Locs[i] = std::make_pair(0, NumLo);
3908 Mask1[NumLo] = Elt;
3909 NumLo++;
3910 } else {
3911 Locs[i] = std::make_pair(1, NumHi);
3912 if (2+NumHi < 4)
3913 Mask1[2+NumHi] = Elt;
3914 NumHi++;
3915 }
3916 }
3917 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003918
Evan Chengf50554e2008-07-22 21:13:36 +00003919 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003920 // If no more than two elements come from either vector. This can be
3921 // implemented with two shuffles. First shuffle gather the elements.
3922 // The second shuffle, which takes the first shuffle as both of its
3923 // vector operands, put the elements into the right order.
Dale Johannesence0805b2009-02-03 19:33:06 +00003924 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
3925 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengf50554e2008-07-22 21:13:36 +00003926 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003927
Dale Johannesence0805b2009-02-03 19:33:06 +00003928 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003929 for (unsigned i = 0; i != 4; ++i) {
3930 if (Locs[i].first == -1)
3931 continue;
3932 else {
3933 unsigned Idx = (i < 2) ? 0 : 4;
3934 Idx += Locs[i].first * 2 + Locs[i].second;
3935 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3936 }
3937 }
3938
Dale Johannesence0805b2009-02-03 19:33:06 +00003939 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V1,
3940 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengf50554e2008-07-22 21:13:36 +00003941 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003942 } else if (NumLo == 3 || NumHi == 3) {
3943 // Otherwise, we must have three elements from one vector, call it X, and
3944 // one element from the other, call it Y. First, use a shufps to build an
3945 // intermediate vector with the one element from Y and the element from X
3946 // that will be in the same half in the final destination (the indexes don't
3947 // matter). Then, use a shufps to build the final vector, taking the half
3948 // containing the element from Y from the intermediate, and the other half
3949 // from X.
3950 if (NumHi == 3) {
3951 // Normalize it so the 3 elements come from V1.
Dale Johannesence0805b2009-02-03 19:33:06 +00003952 PermMask = CommuteVectorShuffleMask(PermMask, DAG, dl);
Evan Cheng3cae0332008-07-23 00:22:17 +00003953 std::swap(V1, V2);
3954 }
3955
3956 // Find the element from V2.
3957 unsigned HiIndex;
3958 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003959 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003960 if (Elt.getOpcode() == ISD::UNDEF)
3961 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003962 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003963 if (Val >= 4)
3964 break;
3965 }
3966
3967 Mask1[0] = PermMask.getOperand(HiIndex);
Dale Johannesence0805b2009-02-03 19:33:06 +00003968 Mask1[1] = DAG.getNode(ISD::UNDEF, dl, MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003969 Mask1[2] = PermMask.getOperand(HiIndex^1);
Dale Johannesence0805b2009-02-03 19:33:06 +00003970 Mask1[3] = DAG.getNode(ISD::UNDEF, dl, MaskEVT);
3971 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
Evan Cheng3cae0332008-07-23 00:22:17 +00003972 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3973
3974 if (HiIndex >= 2) {
3975 Mask1[0] = PermMask.getOperand(0);
3976 Mask1[1] = PermMask.getOperand(1);
3977 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3978 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
Dale Johannesence0805b2009-02-03 19:33:06 +00003979 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
3980 DAG.getNode(ISD::BUILD_VECTOR, dl,
3981 MaskVT, &Mask1[0], 4));
Evan Cheng3cae0332008-07-23 00:22:17 +00003982 } else {
3983 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3984 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3985 Mask1[2] = PermMask.getOperand(2);
3986 Mask1[3] = PermMask.getOperand(3);
3987 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003988 Mask1[2] =
3989 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3990 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003991 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003992 Mask1[3] =
3993 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3994 MaskEVT);
Dale Johannesence0805b2009-02-03 19:33:06 +00003995 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V2, V1,
3996 DAG.getNode(ISD::BUILD_VECTOR, dl,
3997 MaskVT, &Mask1[0], 4));
Evan Cheng3cae0332008-07-23 00:22:17 +00003998 }
Evan Chengf50554e2008-07-22 21:13:36 +00003999 }
4000
4001 // Break it into (shuffle shuffle_hi, shuffle_lo).
4002 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00004003 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
4004 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
4005 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00004006 unsigned MaskIdx = 0;
4007 unsigned LoIdx = 0;
4008 unsigned HiIdx = 2;
4009 for (unsigned i = 0; i != 4; ++i) {
4010 if (i == 2) {
4011 MaskPtr = &HiMask;
4012 MaskIdx = 1;
4013 LoIdx = 0;
4014 HiIdx = 2;
4015 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004016 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00004017 if (Elt.getOpcode() == ISD::UNDEF) {
4018 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004019 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00004020 Locs[i] = std::make_pair(MaskIdx, LoIdx);
4021 (*MaskPtr)[LoIdx] = Elt;
4022 LoIdx++;
4023 } else {
4024 Locs[i] = std::make_pair(MaskIdx, HiIdx);
4025 (*MaskPtr)[HiIdx] = Elt;
4026 HiIdx++;
4027 }
4028 }
4029
Dale Johannesence0805b2009-02-03 19:33:06 +00004030 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
4031 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengf50554e2008-07-22 21:13:36 +00004032 &LoMask[0], LoMask.size()));
Dale Johannesence0805b2009-02-03 19:33:06 +00004033 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
4034 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengf50554e2008-07-22 21:13:36 +00004035 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004036 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00004037 for (unsigned i = 0; i != 4; ++i) {
4038 if (Locs[i].first == -1) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004039 MaskOps.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00004040 } else {
4041 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
4042 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
4043 }
4044 }
Dale Johannesence0805b2009-02-03 19:33:06 +00004045 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, LoShuffle, HiShuffle,
4046 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengf50554e2008-07-22 21:13:36 +00004047 &MaskOps[0], MaskOps.size()));
4048}
4049
Dan Gohman8181bd12008-07-27 21:46:04 +00004050SDValue
4051X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
4052 SDValue V1 = Op.getOperand(0);
4053 SDValue V2 = Op.getOperand(1);
4054 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00004055 MVT VT = Op.getValueType();
Dale Johannesence0805b2009-02-03 19:33:06 +00004056 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004057 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00004058 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004059 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4060 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
4061 bool V1IsSplat = false;
4062 bool V2IsSplat = false;
4063
Gabor Greif1c80d112008-08-28 21:40:38 +00004064 if (isUndefShuffle(Op.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00004065 return DAG.getNode(ISD::UNDEF, dl, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004066
Gabor Greif1c80d112008-08-28 21:40:38 +00004067 if (isZeroShuffle(Op.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00004068 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004069
Gabor Greif1c80d112008-08-28 21:40:38 +00004070 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004071 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00004072 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004073 return V2;
4074
Evan Chengae6c9212008-09-25 23:35:16 +00004075 // Canonicalize movddup shuffles.
4076 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Chengbdd9d9f2008-10-06 21:13:08 +00004077 VT.getSizeInBits() == 128 &&
Evan Chengae6c9212008-09-25 23:35:16 +00004078 X86::isMOVDDUPMask(PermMask.getNode()))
4079 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
4080
Gabor Greif1c80d112008-08-28 21:40:38 +00004081 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004082 if (isMMX || NumElems < 4) return Op;
4083 // Promote it to a v4{if}32 splat.
4084 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004085 }
4086
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004087 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4088 // do it!
4089 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004090 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG,
4091 *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004092 if (NewOp.getNode())
Dale Johannesence0805b2009-02-03 19:33:06 +00004093 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4094 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004095 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4096 // FIXME: Figure out a cleaner way to do this.
4097 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004098 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004099 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Dale Johannesence0805b2009-02-03 19:33:06 +00004100 DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004101 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004102 SDValue NewV1 = NewOp.getOperand(0);
4103 SDValue NewV2 = NewOp.getOperand(1);
4104 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00004105 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004106 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Dale Johannesence0805b2009-02-03 19:33:06 +00004107 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget,
4108 dl);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004109 }
4110 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004111 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004112 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Dale Johannesence0805b2009-02-03 19:33:06 +00004113 DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004114 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004115 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Dale Johannesence0805b2009-02-03 19:33:06 +00004116 DAG, Subtarget, dl);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004117 }
4118 }
4119
Evan Chengdea99362008-05-29 08:22:04 +00004120 // Check if this can be converted into a logical shift.
4121 bool isLeft = false;
4122 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004123 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00004124 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4125 if (isShift && ShVal.hasOneUse()) {
4126 // If the shifted value has multiple uses, it may be cheaper to use
4127 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004128 MVT EVT = VT.getVectorElementType();
4129 ShAmt *= EVT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004130 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00004131 }
4132
Gabor Greif1c80d112008-08-28 21:40:38 +00004133 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004134 if (V1IsUndef)
4135 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004136 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00004137 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004138 if (!isMMX)
4139 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004140 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004141
Gabor Greif1c80d112008-08-28 21:40:38 +00004142 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4143 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4144 X86::isMOVHLPSMask(PermMask.getNode()) ||
4145 X86::isMOVHPMask(PermMask.getNode()) ||
4146 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004147 return Op;
4148
Gabor Greif1c80d112008-08-28 21:40:38 +00004149 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4150 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004151 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4152
Evan Chengdea99362008-05-29 08:22:04 +00004153 if (isShift) {
4154 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004155 MVT EVT = VT.getVectorElementType();
4156 ShAmt *= EVT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004157 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00004158 }
4159
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004160 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004161 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4162 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004163 V1IsSplat = isSplatVector(V1.getNode());
4164 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004165
4166 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004167 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4168 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4169 std::swap(V1IsSplat, V2IsSplat);
4170 std::swap(V1IsUndef, V2IsUndef);
4171 Commuted = true;
4172 }
4173
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004174 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004175 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004176 if (V2IsUndef) return V1;
4177 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4178 if (V2IsSplat) {
4179 // V2 is a splat, so the mask may be malformed. That is, it may point
4180 // to any V2 element. The instruction selectior won't like this. Get
4181 // a corrected mask and commute to form a proper MOVS{S|D}.
Dale Johannesence0805b2009-02-03 19:33:06 +00004182 SDValue NewMask = getMOVLMask(NumElems, DAG, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004183 if (NewMask.getNode() != PermMask.getNode())
Dale Johannesence0805b2009-02-03 19:33:06 +00004184 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, NewMask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004185 }
4186 return Op;
4187 }
4188
Gabor Greif1c80d112008-08-28 21:40:38 +00004189 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4190 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4191 X86::isUNPCKLMask(PermMask.getNode()) ||
4192 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004193 return Op;
4194
4195 if (V2IsSplat) {
4196 // Normalize mask so all entries that point to V2 points to its first
4197 // element then try to match unpck{h|l} again. If match, return a
4198 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004199 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004200 if (NewMask.getNode() != PermMask.getNode()) {
4201 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004202 SDValue NewMask = getUnpacklMask(NumElems, DAG, dl);
4203 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004204 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004205 SDValue NewMask = getUnpackhMask(NumElems, DAG, dl);
4206 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, NewMask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004207 }
4208 }
4209 }
4210
4211 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004212 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004213 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4214
4215 if (Commuted) {
4216 // Commute is back and try unpck* again.
4217 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004218 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4219 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4220 X86::isUNPCKLMask(PermMask.getNode()) ||
4221 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004222 return Op;
4223 }
4224
Evan Chengbf8b2c52008-04-05 00:30:36 +00004225 // Try PSHUF* first, then SHUFP*.
4226 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4227 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004228 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004229 if (V2.getOpcode() != ISD::UNDEF)
Dale Johannesence0805b2009-02-03 19:33:06 +00004230 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00004231 DAG.getNode(ISD::UNDEF, VT), PermMask);
4232 return Op;
4233 }
4234
4235 if (!isMMX) {
4236 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004237 (X86::isPSHUFDMask(PermMask.getNode()) ||
4238 X86::isPSHUFHWMask(PermMask.getNode()) ||
4239 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004240 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004241 if (VT == MVT::v4f32) {
4242 RVT = MVT::v4i32;
Dale Johannesence0805b2009-02-03 19:33:06 +00004243 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, RVT,
4244 DAG.getNode(ISD::BIT_CONVERT, dl, RVT, V1),
4245 DAG.getNode(ISD::UNDEF, dl, RVT), PermMask);
Evan Chengbf8b2c52008-04-05 00:30:36 +00004246 } else if (V2.getOpcode() != ISD::UNDEF)
Dale Johannesence0805b2009-02-03 19:33:06 +00004247 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, RVT, V1,
4248 DAG.getNode(ISD::UNDEF, dl, RVT), PermMask);
Evan Chengbf8b2c52008-04-05 00:30:36 +00004249 if (RVT != VT)
Dale Johannesence0805b2009-02-03 19:33:06 +00004250 Op = DAG.getNode(ISD::BIT_CONVERT, dl, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004251 return Op;
4252 }
4253
Evan Chengbf8b2c52008-04-05 00:30:36 +00004254 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004255 if (X86::isSHUFPMask(PermMask.getNode()) ||
4256 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004257 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004258 }
4259
Evan Cheng75184a92007-12-11 01:46:18 +00004260 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4261 if (VT == MVT::v8i16) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004262 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004263 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004264 return NewOp;
4265 }
4266
Evan Chengf50554e2008-07-22 21:13:36 +00004267 // Handle all 4 wide cases with a number of shuffles except for MMX.
4268 if (NumElems == 4 && !isMMX)
Dale Johannesence0805b2009-02-03 19:33:06 +00004269 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004270
Dan Gohman8181bd12008-07-27 21:46:04 +00004271 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004272}
4273
Dan Gohman8181bd12008-07-27 21:46:04 +00004274SDValue
4275X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004276 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004277 MVT VT = Op.getValueType();
Dale Johannesence0805b2009-02-03 19:33:06 +00004278 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00004279 if (VT.getSizeInBits() == 8) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004280 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004281 Op.getOperand(0), Op.getOperand(1));
Dale Johannesence0805b2009-02-03 19:33:06 +00004282 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004283 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004284 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004285 } else if (VT.getSizeInBits() == 16) {
Evan Chengf9393b32009-01-02 05:29:08 +00004286 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4287 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4288 if (Idx == 0)
Dale Johannesence0805b2009-02-03 19:33:06 +00004289 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4290 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4291 DAG.getNode(ISD::BIT_CONVERT, dl,
4292 MVT::v4i32,
Evan Chengf9393b32009-01-02 05:29:08 +00004293 Op.getOperand(0)),
4294 Op.getOperand(1)));
Dale Johannesence0805b2009-02-03 19:33:06 +00004295 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004296 Op.getOperand(0), Op.getOperand(1));
Dale Johannesence0805b2009-02-03 19:33:06 +00004297 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004298 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004299 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004300 } else if (VT == MVT::f32) {
4301 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4302 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004303 // result has a single use which is a store or a bitcast to i32. And in
4304 // the case of a store, it's not worth it if the index is a constant 0,
4305 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004306 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004307 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004308 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004309 if ((User->getOpcode() != ISD::STORE ||
4310 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4311 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004312 (User->getOpcode() != ISD::BIT_CONVERT ||
4313 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004314 return SDValue();
Dale Johannesence0805b2009-02-03 19:33:06 +00004315 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4316 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
4317 Op.getOperand(0)),
4318 Op.getOperand(1));
4319 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004320 } else if (VT == MVT::i32) {
4321 // ExtractPS works with constant index.
4322 if (isa<ConstantSDNode>(Op.getOperand(1)))
4323 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004324 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004325 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004326}
4327
4328
Dan Gohman8181bd12008-07-27 21:46:04 +00004329SDValue
4330X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004331 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004332 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004333
Evan Cheng6c249332008-03-24 21:52:23 +00004334 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004335 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004336 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004337 return Res;
4338 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004339
Duncan Sands92c43912008-06-06 12:08:01 +00004340 MVT VT = Op.getValueType();
Dale Johannesence0805b2009-02-03 19:33:06 +00004341 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004342 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004343 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004344 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004345 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004346 if (Idx == 0)
Dale Johannesence0805b2009-02-03 19:33:06 +00004347 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4348 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4349 DAG.getNode(ISD::BIT_CONVERT, dl,
4350 MVT::v4i32, Vec),
Evan Cheng75184a92007-12-11 01:46:18 +00004351 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004352 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004353 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesence0805b2009-02-03 19:33:06 +00004354 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004355 Op.getOperand(0), Op.getOperand(1));
Dale Johannesence0805b2009-02-03 19:33:06 +00004356 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004357 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004358 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004359 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004360 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004361 if (Idx == 0)
4362 return Op;
4363 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004364 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004365 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004366 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004367 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004368 IdxVec.
Dale Johannesence0805b2009-02-03 19:33:06 +00004369 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004370 IdxVec.
Dale Johannesence0805b2009-02-03 19:33:06 +00004371 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004372 IdxVec.
Dale Johannesence0805b2009-02-03 19:33:06 +00004373 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
4374 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004375 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004376 SDValue Vec = Op.getOperand(0);
Dale Johannesence0805b2009-02-03 19:33:06 +00004377 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, Vec.getValueType(),
4378 Vec, DAG.getNode(ISD::UNDEF, dl, Vec.getValueType()),
4379 Mask);
4380 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004381 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004382 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004383 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4384 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4385 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004386 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004387 if (Idx == 0)
4388 return Op;
4389
4390 // UNPCKHPD the element to the lowest double word, then movsd.
4391 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4392 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004393 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004394 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004395 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004396 IdxVec.
Dale Johannesence0805b2009-02-03 19:33:06 +00004397 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
4398 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004399 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004400 SDValue Vec = Op.getOperand(0);
Dale Johannesence0805b2009-02-03 19:33:06 +00004401 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, Vec.getValueType(),
4402 Vec, DAG.getNode(ISD::UNDEF, dl, Vec.getValueType()),
4403 Mask);
4404 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004405 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004406 }
4407
Dan Gohman8181bd12008-07-27 21:46:04 +00004408 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004409}
4410
Dan Gohman8181bd12008-07-27 21:46:04 +00004411SDValue
4412X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004413 MVT VT = Op.getValueType();
4414 MVT EVT = VT.getVectorElementType();
Dale Johannesence0805b2009-02-03 19:33:06 +00004415 DebugLoc dl = Op.getNode()->getDebugLoc();
Nate Begemand77e59e2008-02-11 04:19:36 +00004416
Dan Gohman8181bd12008-07-27 21:46:04 +00004417 SDValue N0 = Op.getOperand(0);
4418 SDValue N1 = Op.getOperand(1);
4419 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004420
Dan Gohman5a7af042008-08-14 22:53:18 +00004421 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4422 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004423 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004424 : X86ISD::PINSRW;
4425 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4426 // argument.
4427 if (N1.getValueType() != MVT::i32)
Dale Johannesence0805b2009-02-03 19:33:06 +00004428 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begemand77e59e2008-02-11 04:19:36 +00004429 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004430 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesence0805b2009-02-03 19:33:06 +00004431 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004432 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004433 // Bits [7:6] of the constant are the source select. This will always be
4434 // zero here. The DAG Combiner may combine an extract_elt index into these
4435 // bits. For example (insert (extract, 3), 2) could be matched by putting
4436 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4437 // Bits [5:4] of the constant are the destination select. This is the
4438 // value of the incoming immediate.
4439 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4440 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004441 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00004442 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004443 } else if (EVT == MVT::i32) {
4444 // InsertPS works with constant index.
4445 if (isa<ConstantSDNode>(N2))
4446 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004447 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004448 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004449}
4450
Dan Gohman8181bd12008-07-27 21:46:04 +00004451SDValue
4452X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004453 MVT VT = Op.getValueType();
4454 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004455
4456 if (Subtarget->hasSSE41())
4457 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4458
Evan Chenge12a7eb2007-12-12 07:55:34 +00004459 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004460 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004461
Dale Johannesence0805b2009-02-03 19:33:06 +00004462 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00004463 SDValue N0 = Op.getOperand(0);
4464 SDValue N1 = Op.getOperand(1);
4465 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004466
Duncan Sands92c43912008-06-06 12:08:01 +00004467 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004468 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4469 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004470 if (N1.getValueType() != MVT::i32)
Dale Johannesence0805b2009-02-03 19:33:06 +00004471 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004472 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004473 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesence0805b2009-02-03 19:33:06 +00004474 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004475 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004476 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004477}
4478
Dan Gohman8181bd12008-07-27 21:46:04 +00004479SDValue
4480X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004481 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng759fe022008-07-22 18:39:19 +00004482 if (Op.getValueType() == MVT::v2f32)
Dale Johannesence0805b2009-02-03 19:33:06 +00004483 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4484 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4485 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng759fe022008-07-22 18:39:19 +00004486 Op.getOperand(0))));
4487
Dale Johannesence0805b2009-02-03 19:33:06 +00004488 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004489 MVT VT = MVT::v2i32;
4490 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004491 default: break;
4492 case MVT::v16i8:
4493 case MVT::v8i16:
4494 VT = MVT::v4i32;
4495 break;
4496 }
Dale Johannesence0805b2009-02-03 19:33:06 +00004497 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4498 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004499}
4500
Bill Wendlingfef06052008-09-16 21:48:12 +00004501// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4502// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4503// one of the above mentioned nodes. It has to be wrapped because otherwise
4504// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4505// be used to form addressing mode. These wrapped nodes will be selected
4506// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004507SDValue
4508X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004509 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004510 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004511 getPointerTy(),
4512 CP->getAlignment());
4513 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4514 // With PIC, the address is actually $g + Offset.
4515 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4516 !Subtarget->isPICStyleRIPRel()) {
4517 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4518 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4519 Result);
4520 }
4521
4522 return Result;
4523}
4524
Dan Gohman8181bd12008-07-27 21:46:04 +00004525SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004526X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
Dan Gohman36322c72008-10-18 02:06:02 +00004527 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00004528 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00004529 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4530 bool ExtraLoadRequired =
4531 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4532
4533 // Create the TargetGlobalAddress node, folding in the constant
4534 // offset if it is legal.
4535 SDValue Result;
Dan Gohman3d5257c2008-10-21 03:38:42 +00004536 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +00004537 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4538 Offset = 0;
4539 } else
4540 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004541 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00004542
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004543 // With PIC, the address is actually $g + Offset.
Dan Gohman36322c72008-10-18 02:06:02 +00004544 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004545 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4546 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4547 Result);
4548 }
4549
4550 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4551 // load the value at address GV, not the value of GV itself. This means that
4552 // the GlobalAddress must be in the base or index register of the address, not
4553 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4554 // The same applies for external symbols during PIC codegen
Dan Gohman36322c72008-10-18 02:06:02 +00004555 if (ExtraLoadRequired)
Dan Gohman12a9c082008-02-06 22:27:42 +00004556 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004557 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004558
Dan Gohman36322c72008-10-18 02:06:02 +00004559 // If there was a non-zero offset that we didn't fold, create an explicit
4560 // addition for it.
4561 if (Offset != 0)
4562 Result = DAG.getNode(ISD::ADD, getPointerTy(), Result,
4563 DAG.getConstant(Offset, getPointerTy()));
4564
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004565 return Result;
4566}
4567
Evan Cheng7f250d62008-09-24 00:05:32 +00004568SDValue
4569X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4570 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00004571 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
4572 return LowerGlobalAddress(GV, Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00004573}
4574
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004575// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004576static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004577LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004578 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004579 SDValue InFlag;
4580 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004581 DAG.getNode(X86ISD::GlobalBaseReg,
4582 PtrVT), InFlag);
4583 InFlag = Chain.getValue(1);
4584
4585 // emit leal symbol@TLSGD(,%ebx,1), %eax
4586 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004587 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004588 GA->getValueType(0),
4589 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004590 SDValue Ops[] = { Chain, TGA, InFlag };
4591 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004592 InFlag = Result.getValue(2);
4593 Chain = Result.getValue(1);
4594
4595 // call ___tls_get_addr. This function receives its argument in
4596 // the register EAX.
4597 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4598 InFlag = Chain.getValue(1);
4599
4600 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004601 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004602 DAG.getTargetExternalSymbol("___tls_get_addr",
4603 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004604 DAG.getRegister(X86::EAX, PtrVT),
4605 DAG.getRegister(X86::EBX, PtrVT),
4606 InFlag };
4607 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4608 InFlag = Chain.getValue(1);
4609
4610 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4611}
4612
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004613// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004614static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004615LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004616 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004617 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004618
4619 // emit leaq symbol@TLSGD(%rip), %rdi
4620 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004621 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004622 GA->getValueType(0),
4623 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004624 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4625 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004626 Chain = Result.getValue(1);
4627 InFlag = Result.getValue(2);
4628
aslb204cd52008-08-16 12:58:29 +00004629 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004630 // the register RDI.
4631 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4632 InFlag = Chain.getValue(1);
4633
4634 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004635 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004636 DAG.getTargetExternalSymbol("__tls_get_addr",
4637 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004638 DAG.getRegister(X86::RDI, PtrVT),
4639 InFlag };
4640 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4641 InFlag = Chain.getValue(1);
4642
4643 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4644}
4645
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004646// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4647// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004648static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004649 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004650 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004651 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004652 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4653 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004654 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004655 GA->getValueType(0),
4656 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004657 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004658
4659 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004660 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004661 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004662
4663 // The address of the thread local variable is the add of the thread
4664 // pointer with the offset of the variable.
4665 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4666}
4667
Dan Gohman8181bd12008-07-27 21:46:04 +00004668SDValue
4669X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004670 // TODO: implement the "local dynamic" model
4671 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004672 assert(Subtarget->isTargetELF() &&
4673 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004674 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4675 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4676 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004677 if (Subtarget->is64Bit()) {
4678 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4679 } else {
4680 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4681 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4682 else
4683 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4684 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004685}
4686
Dan Gohman8181bd12008-07-27 21:46:04 +00004687SDValue
4688X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004689 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4690 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004691 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4692 // With PIC, the address is actually $g + Offset.
4693 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4694 !Subtarget->isPICStyleRIPRel()) {
4695 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4696 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4697 Result);
4698 }
4699
4700 return Result;
4701}
4702
Dan Gohman8181bd12008-07-27 21:46:04 +00004703SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004704 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004705 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004706 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4707 // With PIC, the address is actually $g + Offset.
4708 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4709 !Subtarget->isPICStyleRIPRel()) {
4710 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4711 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4712 Result);
4713 }
4714
4715 return Result;
4716}
4717
Chris Lattner62814a32007-10-17 06:02:13 +00004718/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4719/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004720SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004721 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004722 MVT VT = Op.getValueType();
4723 unsigned VTBits = VT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004724 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattner62814a32007-10-17 06:02:13 +00004725 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004726 SDValue ShOpLo = Op.getOperand(0);
4727 SDValue ShOpHi = Op.getOperand(1);
4728 SDValue ShAmt = Op.getOperand(2);
4729 SDValue Tmp1 = isSRA ?
Dale Johannesence0805b2009-02-03 19:33:06 +00004730 DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
4731 DAG.getConstant(VTBits - 1, MVT::i8)) :
Dan Gohman092014e2008-03-03 22:22:09 +00004732 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004733
Dan Gohman8181bd12008-07-27 21:46:04 +00004734 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004735 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004736 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4737 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004738 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00004739 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4740 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004741 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004742
Dale Johannesence0805b2009-02-03 19:33:06 +00004743 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004744 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesence0805b2009-02-03 19:33:06 +00004745 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004746 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004747
Dan Gohman8181bd12008-07-27 21:46:04 +00004748 SDValue Hi, Lo;
4749 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4750 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4751 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004752
Chris Lattner62814a32007-10-17 06:02:13 +00004753 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004754 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4755 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004756 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00004757 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4758 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004759 }
4760
Dan Gohman8181bd12008-07-27 21:46:04 +00004761 SDValue Ops[2] = { Lo, Hi };
Dale Johannesence0805b2009-02-03 19:33:06 +00004762 return DAG.getMergeValues(Ops, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004763}
4764
Dan Gohman8181bd12008-07-27 21:46:04 +00004765SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004766 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004767 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004768 "Unknown SINT_TO_FP to lower!");
4769
4770 // These are really Legal; caller falls through into that case.
4771 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004772 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004773 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4774 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004775 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004776
Dale Johannesence0805b2009-02-03 19:33:06 +00004777 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00004778 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004779 MachineFunction &MF = DAG.getMachineFunction();
4780 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004781 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00004782 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004783 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004784 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004785
4786 // Build the FILD
4787 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004788 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004789 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004790 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4791 else
4792 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004793 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004794 Ops.push_back(Chain);
4795 Ops.push_back(StackSlot);
4796 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004797 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004798 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004799
Dale Johannesen2fc20782007-09-14 22:26:36 +00004800 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004801 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004802 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004803
4804 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4805 // shouldn't be necessary except that RFP cannot be live across
4806 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4807 MachineFunction &MF = DAG.getMachineFunction();
4808 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004809 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004810 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004811 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004812 Ops.push_back(Chain);
4813 Ops.push_back(Result);
4814 Ops.push_back(StackSlot);
4815 Ops.push_back(DAG.getValueType(Op.getValueType()));
4816 Ops.push_back(InFlag);
Dale Johannesence0805b2009-02-03 19:33:06 +00004817 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4818 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004819 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004820 }
4821
4822 return Result;
4823}
4824
Bill Wendling14a30ef2009-01-17 03:56:04 +00004825// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4826SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4827 // This algorithm is not obvious. Here it is in C code, more or less:
4828 /*
4829 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4830 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4831 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesenfb019af2008-10-21 23:07:49 +00004832
Bill Wendling14a30ef2009-01-17 03:56:04 +00004833 // Copy ints to xmm registers.
4834 __m128i xh = _mm_cvtsi32_si128( hi );
4835 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004836
Bill Wendling14a30ef2009-01-17 03:56:04 +00004837 // Combine into low half of a single xmm register.
4838 __m128i x = _mm_unpacklo_epi32( xh, xl );
4839 __m128d d;
4840 double sd;
Dale Johannesenfb019af2008-10-21 23:07:49 +00004841
Bill Wendling14a30ef2009-01-17 03:56:04 +00004842 // Merge in appropriate exponents to give the integer bits the right
4843 // magnitude.
4844 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004845
Bill Wendling14a30ef2009-01-17 03:56:04 +00004846 // Subtract away the biases to deal with the IEEE-754 double precision
4847 // implicit 1.
4848 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004849
Bill Wendling14a30ef2009-01-17 03:56:04 +00004850 // All conversions up to here are exact. The correctly rounded result is
4851 // calculated using the current rounding mode using the following
4852 // horizontal add.
4853 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4854 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4855 // store doesn't really need to be here (except
4856 // maybe to zero the other double)
4857 return sd;
4858 }
4859 */
Dale Johannesenfb019af2008-10-21 23:07:49 +00004860
Dale Johannesence0805b2009-02-03 19:33:06 +00004861 DebugLoc dl = Op.getNode()->getDebugLoc();
4862
Dale Johannesena359b8b2008-10-21 20:50:01 +00004863 // Build some magic constants.
Bill Wendling14a30ef2009-01-17 03:56:04 +00004864 std::vector<Constant*> CV0;
Dale Johannesena359b8b2008-10-21 20:50:01 +00004865 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4866 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4867 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4868 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4869 Constant *C0 = ConstantVector::get(CV0);
4870 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 4);
4871
Bill Wendling14a30ef2009-01-17 03:56:04 +00004872 std::vector<Constant*> CV1;
Dale Johannesena359b8b2008-10-21 20:50:01 +00004873 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4874 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4875 Constant *C1 = ConstantVector::get(CV1);
4876 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 4);
4877
4878 SmallVector<SDValue, 4> MaskVec;
4879 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
4880 MaskVec.push_back(DAG.getConstant(4, MVT::i32));
4881 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
4882 MaskVec.push_back(DAG.getConstant(5, MVT::i32));
Dale Johannesence0805b2009-02-03 19:33:06 +00004883 SDValue UnpcklMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4884 &MaskVec[0], MaskVec.size());
Dale Johannesena359b8b2008-10-21 20:50:01 +00004885 SmallVector<SDValue, 4> MaskVec2;
Duncan Sandsca872ca2008-10-22 11:24:12 +00004886 MaskVec2.push_back(DAG.getConstant(1, MVT::i32));
4887 MaskVec2.push_back(DAG.getConstant(0, MVT::i32));
Dale Johannesence0805b2009-02-03 19:33:06 +00004888 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32,
4889 &MaskVec2[0], MaskVec2.size());
Dale Johannesena359b8b2008-10-21 20:50:01 +00004890
Dale Johannesence0805b2009-02-03 19:33:06 +00004891 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4892 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004893 Op.getOperand(0),
4894 DAG.getIntPtrConstant(1)));
Dale Johannesence0805b2009-02-03 19:33:06 +00004895 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4896 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004897 Op.getOperand(0),
4898 DAG.getIntPtrConstant(0)));
Dale Johannesence0805b2009-02-03 19:33:06 +00004899 SDValue Unpck1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v4i32,
Dale Johannesena359b8b2008-10-21 20:50:01 +00004900 XR1, XR2, UnpcklMask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004901 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004902 PseudoSourceValue::getConstantPool(), 0,
4903 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00004904 SDValue Unpck2 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v4i32,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004905 Unpck1, CLod0, UnpcklMask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004906 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4907 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004908 PseudoSourceValue::getConstantPool(), 0,
4909 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00004910 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling14a30ef2009-01-17 03:56:04 +00004911
Dale Johannesena359b8b2008-10-21 20:50:01 +00004912 // Add the halves; easiest way is to swap them into another reg first.
Dale Johannesence0805b2009-02-03 19:33:06 +00004913 SDValue Shuf = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v2f64,
Dale Johannesena359b8b2008-10-21 20:50:01 +00004914 Sub, Sub, ShufMask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004915 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4916 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesena359b8b2008-10-21 20:50:01 +00004917 DAG.getIntPtrConstant(0));
4918}
4919
Bill Wendling14a30ef2009-01-17 03:56:04 +00004920// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4921SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004922 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling14a30ef2009-01-17 03:56:04 +00004923 // FP constant to bias correct the final result.
4924 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4925 MVT::f64);
4926
4927 // Load the 32-bit value into an XMM register.
Dale Johannesence0805b2009-02-03 19:33:06 +00004928 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4929 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004930 Op.getOperand(0),
4931 DAG.getIntPtrConstant(0)));
4932
Dale Johannesence0805b2009-02-03 19:33:06 +00004933 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4934 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling14a30ef2009-01-17 03:56:04 +00004935 DAG.getIntPtrConstant(0));
4936
4937 // Or the load with the bias.
Dale Johannesence0805b2009-02-03 19:33:06 +00004938 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4939 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4940 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Chengc0ab5e52009-01-19 08:19:57 +00004941 MVT::v2f64, Load)),
Dale Johannesence0805b2009-02-03 19:33:06 +00004942 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4943 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Chengc0ab5e52009-01-19 08:19:57 +00004944 MVT::v2f64, Bias)));
Dale Johannesence0805b2009-02-03 19:33:06 +00004945 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4946 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling14a30ef2009-01-17 03:56:04 +00004947 DAG.getIntPtrConstant(0));
4948
4949 // Subtract the bias.
Dale Johannesence0805b2009-02-03 19:33:06 +00004950 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling14a30ef2009-01-17 03:56:04 +00004951
4952 // Handle final rounding.
Bill Wendlingdb547de2009-01-17 07:40:19 +00004953 MVT DestVT = Op.getValueType();
4954
4955 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004956 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendlingdb547de2009-01-17 07:40:19 +00004957 DAG.getIntPtrConstant(0));
4958 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004959 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendlingdb547de2009-01-17 07:40:19 +00004960 }
4961
4962 // Handle final rounding.
4963 return Sub;
Bill Wendling14a30ef2009-01-17 03:56:04 +00004964}
4965
4966SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Cheng44fd2392009-01-19 08:08:22 +00004967 SDValue N0 = Op.getOperand(0);
Dale Johannesence0805b2009-02-03 19:33:06 +00004968 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling14a30ef2009-01-17 03:56:04 +00004969
Evan Cheng44fd2392009-01-19 08:08:22 +00004970 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4971 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4972 // the optimization here.
4973 if (DAG.SignBitIsZero(N0))
Dale Johannesence0805b2009-02-03 19:33:06 +00004974 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Cheng44fd2392009-01-19 08:08:22 +00004975
4976 MVT SrcVT = N0.getValueType();
Bill Wendling14a30ef2009-01-17 03:56:04 +00004977 if (SrcVT == MVT::i64) {
4978 // We only handle SSE2 f64 target here; caller can handle the rest.
4979 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4980 return SDValue();
Bill Wendlingdb547de2009-01-17 07:40:19 +00004981
Bill Wendling14a30ef2009-01-17 03:56:04 +00004982 return LowerUINT_TO_FP_i64(Op, DAG);
4983 } else if (SrcVT == MVT::i32) {
Bill Wendling14a30ef2009-01-17 03:56:04 +00004984 return LowerUINT_TO_FP_i32(Op, DAG);
4985 }
4986
4987 assert(0 && "Unknown UINT_TO_FP to lower!");
4988 return SDValue();
4989}
4990
Dan Gohman8181bd12008-07-27 21:46:04 +00004991std::pair<SDValue,SDValue> X86TargetLowering::
4992FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004993 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004994 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4995 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004996 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004997
Dale Johannesen2fc20782007-09-14 22:26:36 +00004998 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004999 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005000 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00005001 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00005002 if (Subtarget->is64Bit() &&
5003 Op.getValueType() == MVT::i64 &&
5004 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00005005 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00005006
Evan Cheng05441e62007-10-15 20:11:21 +00005007 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5008 // stack slot.
5009 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00005010 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00005011 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00005012 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005013 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00005014 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005015 default: assert(0 && "Invalid FP_TO_SINT to lower!");
5016 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5017 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5018 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005019 }
5020
Dan Gohman8181bd12008-07-27 21:46:04 +00005021 SDValue Chain = DAG.getEntryNode();
5022 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00005023 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005024 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesence0805b2009-02-03 19:33:06 +00005025 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005026 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005027 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00005028 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005029 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5030 };
Dale Johannesence0805b2009-02-03 19:33:06 +00005031 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005032 Chain = Value.getValue(1);
5033 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
5034 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5035 }
5036
5037 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00005038 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesence0805b2009-02-03 19:33:06 +00005039 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005040
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005041 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005042}
5043
Dan Gohman8181bd12008-07-27 21:46:04 +00005044SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
5045 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
5046 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00005047 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005048
5049 // Load the result.
Dale Johannesence0805b2009-02-03 19:33:06 +00005050 return DAG.getLoad(Op.getValueType(), Op.getNode()->getDebugLoc(),
5051 FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005052}
5053
Dan Gohman8181bd12008-07-27 21:46:04 +00005054SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005055 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00005056 MVT VT = Op.getValueType();
5057 MVT EltVT = VT;
5058 if (VT.isVector())
5059 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005060 std::vector<Constant*> CV;
5061 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005062 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005063 CV.push_back(C);
5064 CV.push_back(C);
5065 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005066 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005067 CV.push_back(C);
5068 CV.push_back(C);
5069 CV.push_back(C);
5070 CV.push_back(C);
5071 }
Dan Gohman11821702007-07-27 17:16:43 +00005072 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005073 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00005074 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005075 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005076 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005077 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005078}
5079
Dan Gohman8181bd12008-07-27 21:46:04 +00005080SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005081 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00005082 MVT VT = Op.getValueType();
5083 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00005084 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00005085 if (VT.isVector()) {
5086 EltVT = VT.getVectorElementType();
5087 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00005088 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005089 std::vector<Constant*> CV;
5090 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005091 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005092 CV.push_back(C);
5093 CV.push_back(C);
5094 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005095 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005096 CV.push_back(C);
5097 CV.push_back(C);
5098 CV.push_back(C);
5099 CV.push_back(C);
5100 }
Dan Gohman11821702007-07-27 17:16:43 +00005101 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005102 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00005103 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005104 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005105 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00005106 if (VT.isVector()) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005107 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
5108 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5109 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
5110 Op.getOperand(0)),
5111 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Cheng92b8f782007-07-19 23:36:01 +00005112 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00005113 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng92b8f782007-07-19 23:36:01 +00005114 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005115}
5116
Dan Gohman8181bd12008-07-27 21:46:04 +00005117SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
5118 SDValue Op0 = Op.getOperand(0);
5119 SDValue Op1 = Op.getOperand(1);
Dale Johannesence0805b2009-02-03 19:33:06 +00005120 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00005121 MVT VT = Op.getValueType();
5122 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005123
5124 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005125 if (SrcVT.bitsLT(VT)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005126 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005127 SrcVT = VT;
5128 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005129 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005130 if (SrcVT.bitsGT(VT)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005131 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005132 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005133 }
5134
5135 // At this point the operands and the result should have the same
5136 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005137
5138 // First get the sign bit of second operand.
5139 std::vector<Constant*> CV;
5140 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005141 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
5142 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005143 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005144 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
5145 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5146 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5147 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005148 }
Dan Gohman11821702007-07-27 17:16:43 +00005149 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005150 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00005151 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005152 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005153 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005154 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005155
5156 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005157 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005158 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesence0805b2009-02-03 19:33:06 +00005159 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5160 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005161 DAG.getConstant(32, MVT::i32));
Dale Johannesence0805b2009-02-03 19:33:06 +00005162 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5163 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00005164 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005165 }
5166
5167 // Clear first operand sign bit.
5168 CV.clear();
5169 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005170 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
5171 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005172 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005173 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
5174 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5175 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5176 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005177 }
Dan Gohman11821702007-07-27 17:16:43 +00005178 C = ConstantVector::get(CV);
5179 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00005180 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005181 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005182 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005183 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005184
5185 // Or the value with the sign bit.
Dale Johannesence0805b2009-02-03 19:33:06 +00005186 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005187}
5188
Dan Gohman8181bd12008-07-27 21:46:04 +00005189SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00005190 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00005191 SDValue Op0 = Op.getOperand(0);
5192 SDValue Op1 = Op.getOperand(1);
Dale Johannesence0805b2009-02-03 19:33:06 +00005193 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattner77a62312008-12-25 05:34:37 +00005194 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5195
Dan Gohman22cefb02009-01-29 01:59:02 +00005196 // Lower (X & (1 << N)) == 0 to BT(X, N).
5197 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5198 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman13dd9522009-01-13 23:25:30 +00005199 if (Op0.getOpcode() == ISD::AND &&
5200 Op0.hasOneUse() &&
5201 Op1.getOpcode() == ISD::Constant &&
Dan Gohman22cefb02009-01-29 01:59:02 +00005202 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattner77a62312008-12-25 05:34:37 +00005203 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohman22cefb02009-01-29 01:59:02 +00005204 SDValue LHS, RHS;
5205 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5206 if (ConstantSDNode *Op010C =
5207 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5208 if (Op010C->getZExtValue() == 1) {
5209 LHS = Op0.getOperand(0);
5210 RHS = Op0.getOperand(1).getOperand(1);
5211 }
5212 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5213 if (ConstantSDNode *Op000C =
5214 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5215 if (Op000C->getZExtValue() == 1) {
5216 LHS = Op0.getOperand(1);
5217 RHS = Op0.getOperand(0).getOperand(1);
5218 }
5219 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5220 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5221 SDValue AndLHS = Op0.getOperand(0);
5222 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5223 LHS = AndLHS.getOperand(0);
5224 RHS = AndLHS.getOperand(1);
5225 }
5226 }
Evan Cheng950aac02007-09-25 01:57:46 +00005227
Dan Gohman22cefb02009-01-29 01:59:02 +00005228 if (LHS.getNode()) {
Chris Lattner77a62312008-12-25 05:34:37 +00005229 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5230 // instruction. Since the shift amount is in-range-or-undefined, we know
5231 // that doing a bittest on the i16 value is ok. We extend to i32 because
5232 // the encoding for the i16 version is larger than the i32 version.
5233 if (LHS.getValueType() == MVT::i8)
Dale Johannesence0805b2009-02-03 19:33:06 +00005234 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattner77a62312008-12-25 05:34:37 +00005235
5236 // If the operand types disagree, extend the shift amount to match. Since
5237 // BT ignores high bits (like shifts) we can use anyextend.
5238 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesence0805b2009-02-03 19:33:06 +00005239 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohman22cefb02009-01-29 01:59:02 +00005240
Dale Johannesence0805b2009-02-03 19:33:06 +00005241 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005242 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesence0805b2009-02-03 19:33:06 +00005243 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner77a62312008-12-25 05:34:37 +00005244 DAG.getConstant(Cond, MVT::i8), BT);
5245 }
5246 }
5247
5248 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5249 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Chris Lattner60435922008-12-24 00:11:37 +00005250
Dale Johannesence0805b2009-02-03 19:33:06 +00005251 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5252 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner60435922008-12-24 00:11:37 +00005253 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005254}
5255
Dan Gohman8181bd12008-07-27 21:46:04 +00005256SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5257 SDValue Cond;
5258 SDValue Op0 = Op.getOperand(0);
5259 SDValue Op1 = Op.getOperand(1);
5260 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00005261 MVT VT = Op.getValueType();
5262 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5263 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesence0805b2009-02-03 19:33:06 +00005264 DebugLoc dl = Op.getNode()->getDebugLoc();
Nate Begeman03605a02008-07-17 16:51:19 +00005265
5266 if (isFP) {
5267 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00005268 MVT VT0 = Op0.getValueType();
5269 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5270 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00005271 bool Swap = false;
5272
5273 switch (SetCCOpcode) {
5274 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005275 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00005276 case ISD::SETEQ: SSECC = 0; break;
5277 case ISD::SETOGT:
5278 case ISD::SETGT: Swap = true; // Fallthrough
5279 case ISD::SETLT:
5280 case ISD::SETOLT: SSECC = 1; break;
5281 case ISD::SETOGE:
5282 case ISD::SETGE: Swap = true; // Fallthrough
5283 case ISD::SETLE:
5284 case ISD::SETOLE: SSECC = 2; break;
5285 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005286 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00005287 case ISD::SETNE: SSECC = 4; break;
5288 case ISD::SETULE: Swap = true;
5289 case ISD::SETUGE: SSECC = 5; break;
5290 case ISD::SETULT: Swap = true;
5291 case ISD::SETUGT: SSECC = 6; break;
5292 case ISD::SETO: SSECC = 7; break;
5293 }
5294 if (Swap)
5295 std::swap(Op0, Op1);
5296
Nate Begeman6357f9d2008-07-25 19:05:58 +00005297 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00005298 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00005299 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005300 SDValue UNORD, EQ;
Dale Johannesence0805b2009-02-03 19:33:06 +00005301 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5302 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5303 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begeman6357f9d2008-07-25 19:05:58 +00005304 }
5305 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005306 SDValue ORD, NEQ;
Dale Johannesence0805b2009-02-03 19:33:06 +00005307 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5308 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5309 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begeman6357f9d2008-07-25 19:05:58 +00005310 }
5311 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00005312 }
5313 // Handle all other FP comparisons here.
Dale Johannesence0805b2009-02-03 19:33:06 +00005314 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman03605a02008-07-17 16:51:19 +00005315 }
5316
5317 // We are handling one of the integer comparisons here. Since SSE only has
5318 // GT and EQ comparisons for integer, swapping operands and multiple
5319 // operations may be required for some comparisons.
5320 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5321 bool Swap = false, Invert = false, FlipSigns = false;
5322
5323 switch (VT.getSimpleVT()) {
5324 default: break;
5325 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5326 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5327 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5328 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5329 }
5330
5331 switch (SetCCOpcode) {
5332 default: break;
5333 case ISD::SETNE: Invert = true;
5334 case ISD::SETEQ: Opc = EQOpc; break;
5335 case ISD::SETLT: Swap = true;
5336 case ISD::SETGT: Opc = GTOpc; break;
5337 case ISD::SETGE: Swap = true;
5338 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5339 case ISD::SETULT: Swap = true;
5340 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5341 case ISD::SETUGE: Swap = true;
5342 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5343 }
5344 if (Swap)
5345 std::swap(Op0, Op1);
5346
5347 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5348 // bits of the inputs before performing those operations.
5349 if (FlipSigns) {
5350 MVT EltVT = VT.getVectorElementType();
Duncan Sands505ba942009-02-01 18:06:53 +00005351 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5352 EltVT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005353 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Dale Johannesence0805b2009-02-03 19:33:06 +00005354 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005355 SignBits.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00005356 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5357 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman03605a02008-07-17 16:51:19 +00005358 }
5359
Dale Johannesence0805b2009-02-03 19:33:06 +00005360 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005361
5362 // If the logical-not of the result is required, perform that now.
Bob Wilson81a42cf2009-01-22 17:39:32 +00005363 if (Invert)
Dale Johannesence0805b2009-02-03 19:33:06 +00005364 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson81a42cf2009-01-22 17:39:32 +00005365
Nate Begeman03605a02008-07-17 16:51:19 +00005366 return Result;
5367}
Evan Cheng950aac02007-09-25 01:57:46 +00005368
Evan Chengd580f022008-12-03 08:38:43 +00005369// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
5370static bool isX86LogicalCmp(unsigned Opc) {
5371 return Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI;
5372}
5373
Dan Gohman8181bd12008-07-27 21:46:04 +00005374SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005375 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005376 SDValue Cond = Op.getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005377 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00005378 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005379
5380 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005381 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005382
Evan Cheng50d37ab2007-10-08 22:16:29 +00005383 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5384 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005385 if (Cond.getOpcode() == X86ISD::SETCC) {
5386 CC = Cond.getOperand(0);
5387
Dan Gohman8181bd12008-07-27 21:46:04 +00005388 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005389 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005390 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005391
Evan Cheng50d37ab2007-10-08 22:16:29 +00005392 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005393 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005394 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005395 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005396
Dan Gohman22cefb02009-01-29 01:59:02 +00005397 if ((isX86LogicalCmp(Opc) && !IllegalFPCMov) || Opc == X86ISD::BT) { // FIXME
Evan Cheng50d37ab2007-10-08 22:16:29 +00005398 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005399 addTest = false;
5400 }
5401 }
5402
5403 if (addTest) {
5404 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005405 Cond= DAG.getNode(X86ISD::CMP, dl, MVT::i32, Cond,
5406 DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005407 }
5408
Duncan Sands92c43912008-06-06 12:08:01 +00005409 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005410 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005411 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005412 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5413 // condition is true.
5414 Ops.push_back(Op.getOperand(2));
5415 Ops.push_back(Op.getOperand(1));
5416 Ops.push_back(CC);
5417 Ops.push_back(Cond);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005418 return DAG.getNode(X86ISD::CMOV, dl, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005419}
5420
Evan Chengd580f022008-12-03 08:38:43 +00005421// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5422// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5423// from the AND / OR.
5424static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5425 Opc = Op.getOpcode();
5426 if (Opc != ISD::OR && Opc != ISD::AND)
5427 return false;
5428 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5429 Op.getOperand(0).hasOneUse() &&
5430 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5431 Op.getOperand(1).hasOneUse());
5432}
5433
Evan Cheng67f98b12009-02-02 08:19:07 +00005434// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5435// 1 and that the SETCC node has a single use.
Evan Cheng8c3af2c2009-02-02 08:07:36 +00005436static bool isXor1OfSetCC(SDValue Op) {
5437 if (Op.getOpcode() != ISD::XOR)
5438 return false;
5439 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5440 if (N1C && N1C->getAPIntValue() == 1) {
5441 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5442 Op.getOperand(0).hasOneUse();
5443 }
5444 return false;
5445}
5446
Dan Gohman8181bd12008-07-27 21:46:04 +00005447SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005448 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005449 SDValue Chain = Op.getOperand(0);
5450 SDValue Cond = Op.getOperand(1);
5451 SDValue Dest = Op.getOperand(2);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005452 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00005453 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005454
5455 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005456 Cond = LowerSETCC(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005457#if 0
5458 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingf5399032008-12-12 21:15:41 +00005459 else if (Cond.getOpcode() == X86ISD::ADD ||
5460 Cond.getOpcode() == X86ISD::SUB ||
5461 Cond.getOpcode() == X86ISD::SMUL ||
5462 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling7e04be62008-12-09 22:08:41 +00005463 Cond = LowerXALUO(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005464#endif
5465
Evan Cheng50d37ab2007-10-08 22:16:29 +00005466 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5467 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005468 if (Cond.getOpcode() == X86ISD::SETCC) {
5469 CC = Cond.getOperand(0);
5470
Dan Gohman8181bd12008-07-27 21:46:04 +00005471 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005472 unsigned Opc = Cmp.getOpcode();
Chris Lattner77a62312008-12-25 05:34:37 +00005473 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
5474 if (isX86LogicalCmp(Opc) || Opc == X86ISD::BT) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005475 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005476 addTest = false;
Bill Wendlingd3511522008-12-02 01:06:39 +00005477 } else {
Evan Chengd580f022008-12-03 08:38:43 +00005478 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling809e7bd2008-12-03 08:32:02 +00005479 default: break;
5480 case X86::COND_O:
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005481 case X86::COND_B:
Chris Lattner77a62312008-12-25 05:34:37 +00005482 // These can only come from an arithmetic instruction with overflow,
5483 // e.g. SADDO, UADDO.
Bill Wendling809e7bd2008-12-03 08:32:02 +00005484 Cond = Cond.getNode()->getOperand(1);
5485 addTest = false;
5486 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00005487 }
Evan Cheng950aac02007-09-25 01:57:46 +00005488 }
Evan Chengd580f022008-12-03 08:38:43 +00005489 } else {
5490 unsigned CondOpc;
5491 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5492 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5493 unsigned Opc = Cmp.getOpcode();
5494 if (CondOpc == ISD::OR) {
5495 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5496 // two branches instead of an explicit OR instruction with a
5497 // separate test.
5498 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5499 isX86LogicalCmp(Opc)) {
5500 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005501 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Chengd580f022008-12-03 08:38:43 +00005502 Chain, Dest, CC, Cmp);
5503 CC = Cond.getOperand(1).getOperand(0);
5504 Cond = Cmp;
5505 addTest = false;
5506 }
5507 } else { // ISD::AND
5508 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5509 // two branches instead of an explicit AND instruction with a
5510 // separate test. However, we only do this if this block doesn't
5511 // have a fall-through edge, because this requires an explicit
5512 // jmp when the condition is false.
5513 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5514 isX86LogicalCmp(Opc) &&
5515 Op.getNode()->hasOneUse()) {
5516 X86::CondCode CCode =
5517 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5518 CCode = X86::GetOppositeBranchCondition(CCode);
5519 CC = DAG.getConstant(CCode, MVT::i8);
5520 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5521 // Look for an unconditional branch following this conditional branch.
5522 // We need this because we need to reverse the successors in order
5523 // to implement FCMP_OEQ.
5524 if (User.getOpcode() == ISD::BR) {
5525 SDValue FalseBB = User.getOperand(1);
5526 SDValue NewBR =
5527 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5528 assert(NewBR == User);
5529 Dest = FalseBB;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005530
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005531 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Chengd580f022008-12-03 08:38:43 +00005532 Chain, Dest, CC, Cmp);
5533 X86::CondCode CCode =
5534 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5535 CCode = X86::GetOppositeBranchCondition(CCode);
5536 CC = DAG.getConstant(CCode, MVT::i8);
5537 Cond = Cmp;
5538 addTest = false;
5539 }
5540 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005541 }
Evan Cheng8c3af2c2009-02-02 08:07:36 +00005542 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5543 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5544 // It should be transformed during dag combiner except when the condition
5545 // is set by a arithmetics with overflow node.
5546 X86::CondCode CCode =
5547 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5548 CCode = X86::GetOppositeBranchCondition(CCode);
5549 CC = DAG.getConstant(CCode, MVT::i8);
5550 Cond = Cond.getOperand(0).getOperand(1);
5551 addTest = false;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005552 }
Evan Cheng950aac02007-09-25 01:57:46 +00005553 }
5554
5555 if (addTest) {
5556 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005557 Cond= DAG.getNode(X86ISD::CMP, dl, MVT::i32, Cond,
5558 DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005559 }
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005560 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005561 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005562}
5563
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005564
5565// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5566// Calls to _alloca is needed to probe the stack when allocating more than 4k
5567// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5568// that the guard pages used by the OS virtual memory manager are allocated in
5569// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005570SDValue
5571X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005572 SelectionDAG &DAG) {
5573 assert(Subtarget->isTargetCygMing() &&
5574 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005575 DebugLoc dl = Op.getNode()->getDebugLoc();
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005576
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005577 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005578 SDValue Chain = Op.getOperand(0);
5579 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005580 // FIXME: Ensure alignment here
5581
Dan Gohman8181bd12008-07-27 21:46:04 +00005582 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005583
Duncan Sands92c43912008-06-06 12:08:01 +00005584 MVT IntPtr = getPointerTy();
5585 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005586
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005587 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005588
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005589 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5590 Flag = Chain.getValue(1);
5591
5592 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005593 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005594 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005595 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005596 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005597 Flag };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005598 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005599 Flag = Chain.getValue(1);
5600
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005601 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005602 DAG.getIntPtrConstant(0, true),
5603 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005604 Flag);
5605
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005606 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005607
Dan Gohman8181bd12008-07-27 21:46:04 +00005608 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005609 return DAG.getMergeValues(Ops1, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005610}
5611
Dan Gohman8181bd12008-07-27 21:46:04 +00005612SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005613X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005614 SDValue Chain,
5615 SDValue Dst, SDValue Src,
5616 SDValue Size, unsigned Align,
5617 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005618 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005619 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005620
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005621 // If not DWORD aligned or size is more than the threshold, call the library.
5622 // The libc version is likely to be faster for these cases. It can use the
5623 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005624 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005625 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005626 ConstantSize->getZExtValue() >
5627 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005628 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005629
5630 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005631 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005632
Bill Wendling4b2e3782008-10-01 00:59:58 +00005633 if (const char *bzeroEntry = V &&
5634 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5635 MVT IntPtr = getPointerTy();
5636 const Type *IntPtrTy = TD->getIntPtrType();
5637 TargetLowering::ArgListTy Args;
5638 TargetLowering::ArgListEntry Entry;
5639 Entry.Node = Dst;
5640 Entry.Ty = IntPtrTy;
5641 Args.push_back(Entry);
5642 Entry.Node = Size;
5643 Args.push_back(Entry);
Dale Johannesenca6237b2009-01-30 23:10:59 +00005644 // FIXME provide DebugLoc info
Bill Wendling4b2e3782008-10-01 00:59:58 +00005645 std::pair<SDValue,SDValue> CallResult =
5646 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5647 CallingConv::C, false,
Dale Johannesenca6237b2009-01-30 23:10:59 +00005648 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG,
5649 DebugLoc::getUnknownLoc());
Bill Wendling4b2e3782008-10-01 00:59:58 +00005650 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005651 }
5652
Dan Gohmane8b391e2008-04-12 04:36:06 +00005653 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005654 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005655 }
5656
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005657 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005658 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005659 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005660 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005661 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005662 unsigned BytesLeft = 0;
5663 bool TwoRepStos = false;
5664 if (ValC) {
5665 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005666 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005667
5668 // If the value is a constant, then we can potentially use larger sets.
5669 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005670 case 2: // WORD aligned
5671 AVT = MVT::i16;
5672 ValReg = X86::AX;
5673 Val = (Val << 8) | Val;
5674 break;
5675 case 0: // DWORD aligned
5676 AVT = MVT::i32;
5677 ValReg = X86::EAX;
5678 Val = (Val << 8) | Val;
5679 Val = (Val << 16) | Val;
5680 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5681 AVT = MVT::i64;
5682 ValReg = X86::RAX;
5683 Val = (Val << 32) | Val;
5684 }
5685 break;
5686 default: // Byte aligned
5687 AVT = MVT::i8;
5688 ValReg = X86::AL;
5689 Count = DAG.getIntPtrConstant(SizeVal);
5690 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005691 }
5692
Duncan Sandsec142ee2008-06-08 20:54:56 +00005693 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005694 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005695 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5696 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005697 }
5698
5699 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5700 InFlag);
5701 InFlag = Chain.getValue(1);
5702 } else {
5703 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005704 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005705 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005706 InFlag = Chain.getValue(1);
5707 }
5708
5709 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5710 Count, InFlag);
5711 InFlag = Chain.getValue(1);
5712 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005713 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005714 InFlag = Chain.getValue(1);
5715
5716 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005717 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005718 Ops.push_back(Chain);
5719 Ops.push_back(DAG.getValueType(AVT));
5720 Ops.push_back(InFlag);
5721 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5722
5723 if (TwoRepStos) {
5724 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005725 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005726 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005727 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005728 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5729 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5730 Left, InFlag);
5731 InFlag = Chain.getValue(1);
5732 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5733 Ops.clear();
5734 Ops.push_back(Chain);
5735 Ops.push_back(DAG.getValueType(MVT::i8));
5736 Ops.push_back(InFlag);
5737 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5738 } else 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 AddrVT = Dst.getValueType();
5742 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005743
5744 Chain = DAG.getMemset(Chain,
5745 DAG.getNode(ISD::ADD, AddrVT, Dst,
5746 DAG.getConstant(Offset, AddrVT)),
5747 Src,
5748 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005749 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005750 }
5751
Dan Gohmane8b391e2008-04-12 04:36:06 +00005752 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005753 return Chain;
5754}
5755
Dan Gohman8181bd12008-07-27 21:46:04 +00005756SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005757X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005758 SDValue Chain, SDValue Dst, SDValue Src,
5759 SDValue Size, unsigned Align,
5760 bool AlwaysInline,
5761 const Value *DstSV, uint64_t DstSVOff,
5762 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005763 // This requires the copy size to be a constant, preferrably
5764 // within a subtarget-specific limit.
5765 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5766 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005767 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005768 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005769 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005770 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005771
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005772 /// If not DWORD aligned, call the library.
5773 if ((Align & 3) != 0)
5774 return SDValue();
5775
5776 // DWORD aligned
5777 MVT AVT = MVT::i32;
5778 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005779 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005780
Duncan Sands92c43912008-06-06 12:08:01 +00005781 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005782 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005783 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005784 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005785
Dan Gohman8181bd12008-07-27 21:46:04 +00005786 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005787 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5788 Count, InFlag);
5789 InFlag = Chain.getValue(1);
5790 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005791 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005792 InFlag = Chain.getValue(1);
5793 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005794 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005795 InFlag = Chain.getValue(1);
5796
5797 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005798 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005799 Ops.push_back(Chain);
5800 Ops.push_back(DAG.getValueType(AVT));
5801 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005802 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005803
Dan Gohman8181bd12008-07-27 21:46:04 +00005804 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005805 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005806 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005807 // Handle the last 1 - 7 bytes.
5808 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005809 MVT DstVT = Dst.getValueType();
5810 MVT SrcVT = Src.getValueType();
5811 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005812 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005813 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005814 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005815 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005816 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005817 DAG.getConstant(BytesLeft, SizeVT),
5818 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005819 DstSV, DstSVOff + Offset,
5820 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005821 }
5822
Dan Gohmane8b391e2008-04-12 04:36:06 +00005823 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005824}
5825
Dan Gohman8181bd12008-07-27 21:46:04 +00005826SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005827 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005828 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005829
5830 if (!Subtarget->is64Bit()) {
5831 // vastart just stores the address of the VarArgsFrameIndex slot into the
5832 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005833 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005834 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005835 }
5836
5837 // __va_list_tag:
5838 // gp_offset (0 - 6 * 8)
5839 // fp_offset (48 - 48 + 8 * 16)
5840 // overflow_arg_area (point to parameters coming in memory).
5841 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005842 SmallVector<SDValue, 8> MemOps;
5843 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005844 // Store gp_offset
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005845 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005846 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005847 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005848 MemOps.push_back(Store);
5849
5850 // Store fp_offset
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005851 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5852 FIN, DAG.getIntPtrConstant(4));
5853 Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005854 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005855 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005856 MemOps.push_back(Store);
5857
5858 // Store ptr to overflow_arg_area
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005859 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5860 FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005861 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005862 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005863 MemOps.push_back(Store);
5864
5865 // Store ptr to reg_save_area.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005866 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5867 FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005868 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005869 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005870 MemOps.push_back(Store);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005871 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
5872 &MemOps[0], MemOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005873}
5874
Dan Gohman8181bd12008-07-27 21:46:04 +00005875SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005876 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5877 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005878 SDValue Chain = Op.getOperand(0);
5879 SDValue SrcPtr = Op.getOperand(1);
5880 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005881
5882 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5883 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005884 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005885}
5886
Dan Gohman8181bd12008-07-27 21:46:04 +00005887SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005888 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005889 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005890 SDValue Chain = Op.getOperand(0);
5891 SDValue DstPtr = Op.getOperand(1);
5892 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005893 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5894 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005895
Dan Gohman840ff5c2008-04-18 20:55:41 +00005896 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5897 DAG.getIntPtrConstant(24), 8, false,
5898 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005899}
5900
Dan Gohman8181bd12008-07-27 21:46:04 +00005901SDValue
5902X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005903 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005904 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005905 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005906 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005907 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005908 case Intrinsic::x86_sse_comieq_ss:
5909 case Intrinsic::x86_sse_comilt_ss:
5910 case Intrinsic::x86_sse_comile_ss:
5911 case Intrinsic::x86_sse_comigt_ss:
5912 case Intrinsic::x86_sse_comige_ss:
5913 case Intrinsic::x86_sse_comineq_ss:
5914 case Intrinsic::x86_sse_ucomieq_ss:
5915 case Intrinsic::x86_sse_ucomilt_ss:
5916 case Intrinsic::x86_sse_ucomile_ss:
5917 case Intrinsic::x86_sse_ucomigt_ss:
5918 case Intrinsic::x86_sse_ucomige_ss:
5919 case Intrinsic::x86_sse_ucomineq_ss:
5920 case Intrinsic::x86_sse2_comieq_sd:
5921 case Intrinsic::x86_sse2_comilt_sd:
5922 case Intrinsic::x86_sse2_comile_sd:
5923 case Intrinsic::x86_sse2_comigt_sd:
5924 case Intrinsic::x86_sse2_comige_sd:
5925 case Intrinsic::x86_sse2_comineq_sd:
5926 case Intrinsic::x86_sse2_ucomieq_sd:
5927 case Intrinsic::x86_sse2_ucomilt_sd:
5928 case Intrinsic::x86_sse2_ucomile_sd:
5929 case Intrinsic::x86_sse2_ucomigt_sd:
5930 case Intrinsic::x86_sse2_ucomige_sd:
5931 case Intrinsic::x86_sse2_ucomineq_sd: {
5932 unsigned Opc = 0;
5933 ISD::CondCode CC = ISD::SETCC_INVALID;
5934 switch (IntNo) {
5935 default: break;
5936 case Intrinsic::x86_sse_comieq_ss:
5937 case Intrinsic::x86_sse2_comieq_sd:
5938 Opc = X86ISD::COMI;
5939 CC = ISD::SETEQ;
5940 break;
5941 case Intrinsic::x86_sse_comilt_ss:
5942 case Intrinsic::x86_sse2_comilt_sd:
5943 Opc = X86ISD::COMI;
5944 CC = ISD::SETLT;
5945 break;
5946 case Intrinsic::x86_sse_comile_ss:
5947 case Intrinsic::x86_sse2_comile_sd:
5948 Opc = X86ISD::COMI;
5949 CC = ISD::SETLE;
5950 break;
5951 case Intrinsic::x86_sse_comigt_ss:
5952 case Intrinsic::x86_sse2_comigt_sd:
5953 Opc = X86ISD::COMI;
5954 CC = ISD::SETGT;
5955 break;
5956 case Intrinsic::x86_sse_comige_ss:
5957 case Intrinsic::x86_sse2_comige_sd:
5958 Opc = X86ISD::COMI;
5959 CC = ISD::SETGE;
5960 break;
5961 case Intrinsic::x86_sse_comineq_ss:
5962 case Intrinsic::x86_sse2_comineq_sd:
5963 Opc = X86ISD::COMI;
5964 CC = ISD::SETNE;
5965 break;
5966 case Intrinsic::x86_sse_ucomieq_ss:
5967 case Intrinsic::x86_sse2_ucomieq_sd:
5968 Opc = X86ISD::UCOMI;
5969 CC = ISD::SETEQ;
5970 break;
5971 case Intrinsic::x86_sse_ucomilt_ss:
5972 case Intrinsic::x86_sse2_ucomilt_sd:
5973 Opc = X86ISD::UCOMI;
5974 CC = ISD::SETLT;
5975 break;
5976 case Intrinsic::x86_sse_ucomile_ss:
5977 case Intrinsic::x86_sse2_ucomile_sd:
5978 Opc = X86ISD::UCOMI;
5979 CC = ISD::SETLE;
5980 break;
5981 case Intrinsic::x86_sse_ucomigt_ss:
5982 case Intrinsic::x86_sse2_ucomigt_sd:
5983 Opc = X86ISD::UCOMI;
5984 CC = ISD::SETGT;
5985 break;
5986 case Intrinsic::x86_sse_ucomige_ss:
5987 case Intrinsic::x86_sse2_ucomige_sd:
5988 Opc = X86ISD::UCOMI;
5989 CC = ISD::SETGE;
5990 break;
5991 case Intrinsic::x86_sse_ucomineq_ss:
5992 case Intrinsic::x86_sse2_ucomineq_sd:
5993 Opc = X86ISD::UCOMI;
5994 CC = ISD::SETNE;
5995 break;
5996 }
5997
Dan Gohman8181bd12008-07-27 21:46:04 +00005998 SDValue LHS = Op.getOperand(1);
5999 SDValue RHS = Op.getOperand(2);
Chris Lattnerebb91142008-12-24 23:53:05 +00006000 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006001 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6002 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00006003 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006004 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006005 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006006
6007 // Fix vector shift instructions where the last operand is a non-immediate
6008 // i32 value.
6009 case Intrinsic::x86_sse2_pslli_w:
6010 case Intrinsic::x86_sse2_pslli_d:
6011 case Intrinsic::x86_sse2_pslli_q:
6012 case Intrinsic::x86_sse2_psrli_w:
6013 case Intrinsic::x86_sse2_psrli_d:
6014 case Intrinsic::x86_sse2_psrli_q:
6015 case Intrinsic::x86_sse2_psrai_w:
6016 case Intrinsic::x86_sse2_psrai_d:
6017 case Intrinsic::x86_mmx_pslli_w:
6018 case Intrinsic::x86_mmx_pslli_d:
6019 case Intrinsic::x86_mmx_pslli_q:
6020 case Intrinsic::x86_mmx_psrli_w:
6021 case Intrinsic::x86_mmx_psrli_d:
6022 case Intrinsic::x86_mmx_psrli_q:
6023 case Intrinsic::x86_mmx_psrai_w:
6024 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006025 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006026 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006027 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006028
6029 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006030 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006031 switch (IntNo) {
6032 case Intrinsic::x86_sse2_pslli_w:
6033 NewIntNo = Intrinsic::x86_sse2_psll_w;
6034 break;
6035 case Intrinsic::x86_sse2_pslli_d:
6036 NewIntNo = Intrinsic::x86_sse2_psll_d;
6037 break;
6038 case Intrinsic::x86_sse2_pslli_q:
6039 NewIntNo = Intrinsic::x86_sse2_psll_q;
6040 break;
6041 case Intrinsic::x86_sse2_psrli_w:
6042 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6043 break;
6044 case Intrinsic::x86_sse2_psrli_d:
6045 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6046 break;
6047 case Intrinsic::x86_sse2_psrli_q:
6048 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6049 break;
6050 case Intrinsic::x86_sse2_psrai_w:
6051 NewIntNo = Intrinsic::x86_sse2_psra_w;
6052 break;
6053 case Intrinsic::x86_sse2_psrai_d:
6054 NewIntNo = Intrinsic::x86_sse2_psra_d;
6055 break;
6056 default: {
6057 ShAmtVT = MVT::v2i32;
6058 switch (IntNo) {
6059 case Intrinsic::x86_mmx_pslli_w:
6060 NewIntNo = Intrinsic::x86_mmx_psll_w;
6061 break;
6062 case Intrinsic::x86_mmx_pslli_d:
6063 NewIntNo = Intrinsic::x86_mmx_psll_d;
6064 break;
6065 case Intrinsic::x86_mmx_pslli_q:
6066 NewIntNo = Intrinsic::x86_mmx_psll_q;
6067 break;
6068 case Intrinsic::x86_mmx_psrli_w:
6069 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6070 break;
6071 case Intrinsic::x86_mmx_psrli_d:
6072 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6073 break;
6074 case Intrinsic::x86_mmx_psrli_q:
6075 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6076 break;
6077 case Intrinsic::x86_mmx_psrai_w:
6078 NewIntNo = Intrinsic::x86_mmx_psra_w;
6079 break;
6080 case Intrinsic::x86_mmx_psrai_d:
6081 NewIntNo = Intrinsic::x86_mmx_psra_d;
6082 break;
6083 default: abort(); // Can't reach here.
6084 }
6085 break;
6086 }
6087 }
Duncan Sands92c43912008-06-06 12:08:01 +00006088 MVT VT = Op.getValueType();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006089 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6090 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6091 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006092 DAG.getConstant(NewIntNo, MVT::i32),
6093 Op.getOperand(1), ShAmt);
6094 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006095 }
6096}
6097
Dan Gohman8181bd12008-07-27 21:46:04 +00006098SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006099 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006100 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006101
6102 if (Depth > 0) {
6103 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6104 SDValue Offset =
6105 DAG.getConstant(TD->getPointerSize(),
6106 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006107 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
6108 DAG.getNode(ISD::ADD, dl, getPointerTy(),
6109 FrameAddr, Offset),
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006110 NULL, 0);
6111 }
6112
6113 // Just load the return address.
Dan Gohman8181bd12008-07-27 21:46:04 +00006114 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006115 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
6116 RetAddrFI, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006117}
6118
Dan Gohman8181bd12008-07-27 21:46:04 +00006119SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00006120 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6121 MFI->setFrameAddressIsTaken(true);
6122 MVT VT = Op.getValueType();
6123 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6124 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
6125 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), FrameReg, VT);
6126 while (Depth--)
6127 FrameAddr = DAG.getLoad(VT, DAG.getEntryNode(), FrameAddr, NULL, 0);
6128 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006129}
6130
Dan Gohman8181bd12008-07-27 21:46:04 +00006131SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00006132 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006133 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006134}
6135
Dan Gohman8181bd12008-07-27 21:46:04 +00006136SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006137{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006138 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00006139 SDValue Chain = Op.getOperand(0);
6140 SDValue Offset = Op.getOperand(1);
6141 SDValue Handler = Op.getOperand(2);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006142 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006143
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006144 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6145 getPointerTy());
6146 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006147
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006148 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006149 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006150 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6151 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006152 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
6153 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006154
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006155 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006156 MVT::Other,
6157 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006158}
6159
Dan Gohman8181bd12008-07-27 21:46:04 +00006160SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006161 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006162 SDValue Root = Op.getOperand(0);
6163 SDValue Trmp = Op.getOperand(1); // trampoline
6164 SDValue FPtr = Op.getOperand(2); // nested function
6165 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006166 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006167
Dan Gohman12a9c082008-02-06 22:27:42 +00006168 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006169
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006170 const X86InstrInfo *TII =
6171 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6172
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006173 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006174 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006175
6176 // Large code-model.
6177
6178 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6179 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6180
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006181 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6182 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006183
6184 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6185
6186 // Load the pointer to the nested function into R11.
6187 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00006188 SDValue Addr = Trmp;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006189 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6190 Addr, TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006191
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006192 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6193 DAG.getConstant(2, MVT::i64));
6194 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006195
6196 // Load the 'nest' parameter value into R10.
6197 // R10 is specified in X86CallingConv.td
6198 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006199 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6200 DAG.getConstant(10, MVT::i64));
6201 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6202 Addr, TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006203
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006204 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6205 DAG.getConstant(12, MVT::i64));
6206 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006207
6208 // Jump to the nested function.
6209 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006210 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6211 DAG.getConstant(20, MVT::i64));
6212 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6213 Addr, TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006214
6215 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006216 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6217 DAG.getConstant(22, MVT::i64));
6218 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006219 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006220
Dan Gohman8181bd12008-07-27 21:46:04 +00006221 SDValue Ops[] =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006222 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6223 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006224 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00006225 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006226 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6227 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00006228 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006229
6230 switch (CC) {
6231 default:
6232 assert(0 && "Unsupported calling convention");
6233 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006234 case CallingConv::X86_StdCall: {
6235 // Pass 'nest' parameter in ECX.
6236 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006237 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006238
6239 // Check that ECX wasn't needed by an 'inreg' parameter.
6240 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00006241 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006242
Chris Lattner1c8733e2008-03-12 17:45:29 +00006243 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006244 unsigned InRegCount = 0;
6245 unsigned Idx = 1;
6246
6247 for (FunctionType::param_iterator I = FTy->param_begin(),
6248 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00006249 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006250 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006251 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006252
6253 if (InRegCount > 2) {
6254 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6255 abort();
6256 }
6257 }
6258 break;
6259 }
6260 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00006261 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006262 // Pass 'nest' parameter in EAX.
6263 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006264 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006265 break;
6266 }
6267
Dan Gohman8181bd12008-07-27 21:46:04 +00006268 SDValue OutChains[4];
6269 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006270
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006271 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6272 DAG.getConstant(10, MVT::i32));
6273 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006274
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006275 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006276 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006277 OutChains[0] = DAG.getStore(Root, dl,
6278 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00006279 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006280
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006281 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6282 DAG.getConstant(1, MVT::i32));
6283 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006284
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006285 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006286 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6287 DAG.getConstant(5, MVT::i32));
6288 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006289 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006290
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006291 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6292 DAG.getConstant(6, MVT::i32));
6293 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006294
Dan Gohman8181bd12008-07-27 21:46:04 +00006295 SDValue Ops[] =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006296 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6297 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006298 }
6299}
6300
Dan Gohman8181bd12008-07-27 21:46:04 +00006301SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006302 /*
6303 The rounding mode is in bits 11:10 of FPSR, and has the following
6304 settings:
6305 00 Round to nearest
6306 01 Round to -inf
6307 10 Round to +inf
6308 11 Round to 0
6309
6310 FLT_ROUNDS, on the other hand, expects the following:
6311 -1 Undefined
6312 0 Round to 0
6313 1 Round to nearest
6314 2 Round to +inf
6315 3 Round to -inf
6316
6317 To perform the conversion, we do:
6318 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6319 */
6320
6321 MachineFunction &MF = DAG.getMachineFunction();
6322 const TargetMachine &TM = MF.getTarget();
6323 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6324 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00006325 MVT VT = Op.getValueType();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006326 DebugLoc dl = Op.getNode()->getDebugLoc();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006327
6328 // Save FP Control Word to stack slot
6329 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00006330 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006331
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006332 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00006333 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006334
6335 // Load FP Control Word from stack slot
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006336 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006337
6338 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00006339 SDValue CWD1 =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006340 DAG.getNode(ISD::SRL, dl, MVT::i16,
6341 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006342 CWD, DAG.getConstant(0x800, MVT::i16)),
6343 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006344 SDValue CWD2 =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006345 DAG.getNode(ISD::SRL, dl, MVT::i16,
6346 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006347 CWD, DAG.getConstant(0x400, MVT::i16)),
6348 DAG.getConstant(9, MVT::i8));
6349
Dan Gohman8181bd12008-07-27 21:46:04 +00006350 SDValue RetVal =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006351 DAG.getNode(ISD::AND, dl, MVT::i16,
6352 DAG.getNode(ISD::ADD, dl, MVT::i16,
6353 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006354 DAG.getConstant(1, MVT::i16)),
6355 DAG.getConstant(3, MVT::i16));
6356
6357
Duncan Sands92c43912008-06-06 12:08:01 +00006358 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006359 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6360}
6361
Dan Gohman8181bd12008-07-27 21:46:04 +00006362SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006363 MVT VT = Op.getValueType();
6364 MVT OpVT = VT;
6365 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006366 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng48679f42007-12-14 02:13:44 +00006367
6368 Op = Op.getOperand(0);
6369 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006370 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00006371 OpVT = MVT::i32;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006372 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006373 }
Evan Cheng48679f42007-12-14 02:13:44 +00006374
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006375 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6376 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006377 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006378
6379 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006380 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006381 Ops.push_back(Op);
6382 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6383 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6384 Ops.push_back(Op.getValue(1));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006385 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006386
6387 // Finally xor with NumBits-1.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006388 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006389
Evan Cheng48679f42007-12-14 02:13:44 +00006390 if (VT == MVT::i8)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006391 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006392 return Op;
6393}
6394
Dan Gohman8181bd12008-07-27 21:46:04 +00006395SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006396 MVT VT = Op.getValueType();
6397 MVT OpVT = VT;
6398 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006399 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng48679f42007-12-14 02:13:44 +00006400
6401 Op = Op.getOperand(0);
6402 if (VT == MVT::i8) {
6403 OpVT = MVT::i32;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006404 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006405 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006406
6407 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6408 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006409 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006410
6411 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006412 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006413 Ops.push_back(Op);
6414 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6415 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6416 Ops.push_back(Op.getValue(1));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006417 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006418
Evan Cheng48679f42007-12-14 02:13:44 +00006419 if (VT == MVT::i8)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006420 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006421 return Op;
6422}
6423
Mon P Wang14edb092008-12-18 21:42:19 +00006424SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6425 MVT VT = Op.getValueType();
6426 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006427 DebugLoc dl = Op.getNode()->getDebugLoc();
Mon P Wang14edb092008-12-18 21:42:19 +00006428
6429 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6430 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6431 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6432 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6433 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6434 //
6435 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6436 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6437 // return AloBlo + AloBhi + AhiBlo;
6438
6439 SDValue A = Op.getOperand(0);
6440 SDValue B = Op.getOperand(1);
6441
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006442 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006443 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6444 A, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006445 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006446 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6447 B, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006448 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006449 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6450 A, B);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006451 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006452 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6453 A, Bhi);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006454 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006455 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6456 Ahi, B);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006457 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006458 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6459 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006460 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006461 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6462 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006463 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6464 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wang14edb092008-12-18 21:42:19 +00006465 return Res;
6466}
6467
6468
Bill Wendling7e04be62008-12-09 22:08:41 +00006469SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6470 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6471 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendlingd3511522008-12-02 01:06:39 +00006472 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6473 // has only one use.
Bill Wendlingd06b4202008-11-26 22:37:40 +00006474 SDNode *N = Op.getNode();
Bill Wendlingd3511522008-12-02 01:06:39 +00006475 SDValue LHS = N->getOperand(0);
6476 SDValue RHS = N->getOperand(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00006477 unsigned BaseOp = 0;
6478 unsigned Cond = 0;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006479 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling7e04be62008-12-09 22:08:41 +00006480
6481 switch (Op.getOpcode()) {
6482 default: assert(0 && "Unknown ovf instruction!");
6483 case ISD::SADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006484 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00006485 Cond = X86::COND_O;
6486 break;
6487 case ISD::UADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006488 BaseOp = X86ISD::ADD;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006489 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006490 break;
6491 case ISD::SSUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006492 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00006493 Cond = X86::COND_O;
6494 break;
6495 case ISD::USUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006496 BaseOp = X86ISD::SUB;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006497 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006498 break;
6499 case ISD::SMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006500 BaseOp = X86ISD::SMUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00006501 Cond = X86::COND_O;
6502 break;
6503 case ISD::UMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006504 BaseOp = X86ISD::UMUL;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006505 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006506 break;
6507 }
Bill Wendlingd06b4202008-11-26 22:37:40 +00006508
Bill Wendlingd3511522008-12-02 01:06:39 +00006509 // Also sets EFLAGS.
6510 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006511 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendlingd06b4202008-11-26 22:37:40 +00006512
Bill Wendlingd3511522008-12-02 01:06:39 +00006513 SDValue SetCC =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006514 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendling35f1a9d2008-12-10 02:01:32 +00006515 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendlingd06b4202008-11-26 22:37:40 +00006516
Bill Wendlingd3511522008-12-02 01:06:39 +00006517 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6518 return Sum;
Bill Wendling4c134df2008-11-24 19:21:46 +00006519}
6520
Dan Gohman8181bd12008-07-27 21:46:04 +00006521SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006522 MVT T = Op.getValueType();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006523 DebugLoc dl = Op.getNode()->getDebugLoc();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00006524 unsigned Reg = 0;
6525 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006526 switch(T.getSimpleVT()) {
6527 default:
6528 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006529 case MVT::i8: Reg = X86::AL; size = 1; break;
6530 case MVT::i16: Reg = X86::AX; size = 2; break;
6531 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00006532 case MVT::i64:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006533 assert(Subtarget->is64Bit() && "Node not type legal!");
6534 Reg = X86::RAX; size = 8;
Andrew Lenharth81580822008-03-05 01:15:49 +00006535 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00006536 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006537 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00006538 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00006539 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006540 Op.getOperand(1),
6541 Op.getOperand(3),
6542 DAG.getTargetConstant(size, MVT::i8),
6543 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006544 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006545 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Dan Gohman8181bd12008-07-27 21:46:04 +00006546 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006547 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
6548 return cpOut;
6549}
6550
Duncan Sands7d9834b2008-12-01 11:39:25 +00006551SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif825aa892008-08-28 23:19:51 +00006552 SelectionDAG &DAG) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006553 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharth81580822008-03-05 01:15:49 +00006554 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006555 SDValue TheChain = Op.getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006556 DebugLoc dl = Op.getNode()->getDebugLoc();
6557 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006558 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
6559 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX, MVT::i64,
6560 rax.getValue(2));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006561 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006562 DAG.getConstant(32, MVT::i8));
6563 SDValue Ops[] = {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006564 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006565 rdx.getValue(1)
6566 };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006567 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesenf160d802008-10-02 18:53:47 +00006568}
6569
Dale Johannesen9011d872008-09-29 22:25:26 +00006570SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6571 SDNode *Node = Op.getNode();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006572 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen9011d872008-09-29 22:25:26 +00006573 MVT T = Node->getValueType(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006574 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006575 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006576 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006577 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen9011d872008-09-29 22:25:26 +00006578 Node->getOperand(0),
6579 Node->getOperand(1), negOp,
6580 cast<AtomicSDNode>(Node)->getSrcValue(),
6581 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006582}
6583
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006584/// LowerOperation - Provide custom lowering hooks for some operations.
6585///
Dan Gohman8181bd12008-07-27 21:46:04 +00006586SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006587 switch (Op.getOpcode()) {
6588 default: assert(0 && "Should not custom lower this!");
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006589 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6590 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006591 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6592 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6593 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6594 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6595 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6596 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6597 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6598 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006599 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006600 case ISD::SHL_PARTS:
6601 case ISD::SRA_PARTS:
6602 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6603 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00006604 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006605 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6606 case ISD::FABS: return LowerFABS(Op, DAG);
6607 case ISD::FNEG: return LowerFNEG(Op, DAG);
6608 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006609 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006610 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006611 case ISD::SELECT: return LowerSELECT(Op, DAG);
6612 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006613 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6614 case ISD::CALL: return LowerCALL(Op, DAG);
6615 case ISD::RET: return LowerRET(Op, DAG);
6616 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006617 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006618 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006619 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6620 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6621 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6622 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6623 case ISD::FRAME_TO_ARGS_OFFSET:
6624 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6625 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6626 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006627 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006628 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006629 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6630 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wang14edb092008-12-18 21:42:19 +00006631 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling7e04be62008-12-09 22:08:41 +00006632 case ISD::SADDO:
6633 case ISD::UADDO:
6634 case ISD::SSUBO:
6635 case ISD::USUBO:
6636 case ISD::SMULO:
6637 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006638 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006639 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006640}
6641
Duncan Sands7d9834b2008-12-01 11:39:25 +00006642void X86TargetLowering::
6643ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6644 SelectionDAG &DAG, unsigned NewOp) {
6645 MVT T = Node->getValueType(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006646 DebugLoc dl = Node->getDebugLoc();
Duncan Sands7d9834b2008-12-01 11:39:25 +00006647 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6648
6649 SDValue Chain = Node->getOperand(0);
6650 SDValue In1 = Node->getOperand(1);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006651 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006652 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006653 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006654 Node->getOperand(2), DAG.getIntPtrConstant(1));
6655 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6656 // have a MemOperand. Pass the info through as a normal operand.
6657 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6658 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6659 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006660 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006661 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006662 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006663 Results.push_back(Result.getValue(2));
6664}
6665
Duncan Sandsac496a12008-07-04 11:47:58 +00006666/// ReplaceNodeResults - Replace a node with an illegal result type
6667/// with a new node built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00006668void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6669 SmallVectorImpl<SDValue>&Results,
6670 SelectionDAG &DAG) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006671 DebugLoc dl = N->getDebugLoc();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006672 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006673 default:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006674 assert(false && "Do not know how to custom type legalize this operation!");
6675 return;
6676 case ISD::FP_TO_SINT: {
6677 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
6678 SDValue FIST = Vals.first, StackSlot = Vals.second;
6679 if (FIST.getNode() != 0) {
6680 MVT VT = N->getValueType(0);
6681 // Return a load from the stack slot.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006682 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006683 }
6684 return;
6685 }
6686 case ISD::READCYCLECOUNTER: {
6687 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6688 SDValue TheChain = N->getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006689 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006690 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
6691 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX, MVT::i32,
6692 eax.getValue(2));
6693 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6694 SDValue Ops[] = { eax, edx };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006695 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006696 Results.push_back(edx.getValue(1));
6697 return;
6698 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006699 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006700 MVT T = N->getValueType(0);
6701 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6702 SDValue cpInL, cpInH;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006703 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006704 DAG.getConstant(0, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006705 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006706 DAG.getConstant(1, MVT::i32));
6707 cpInL = DAG.getCopyToReg(N->getOperand(0), X86::EAX, cpInL, SDValue());
6708 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX, cpInH,
6709 cpInL.getValue(1));
6710 SDValue swapInL, swapInH;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006711 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006712 DAG.getConstant(0, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006713 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006714 DAG.getConstant(1, MVT::i32));
6715 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX, swapInL,
6716 cpInH.getValue(1));
6717 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX, swapInH,
6718 swapInL.getValue(1));
6719 SDValue Ops[] = { swapInH.getValue(0),
6720 N->getOperand(1),
6721 swapInH.getValue(1) };
6722 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006723 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006724 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
6725 Result.getValue(1));
6726 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
6727 cpOutL.getValue(2));
6728 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006729 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006730 Results.push_back(cpOutH.getValue(1));
6731 return;
6732 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006733 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006734 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6735 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006736 case ISD::ATOMIC_LOAD_AND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006737 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6738 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006739 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006740 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6741 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006742 case ISD::ATOMIC_LOAD_OR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006743 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6744 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006745 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006746 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6747 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006748 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006749 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6750 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006751 case ISD::ATOMIC_SWAP:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006752 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6753 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006754 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006755}
6756
6757const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6758 switch (Opcode) {
6759 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006760 case X86ISD::BSF: return "X86ISD::BSF";
6761 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006762 case X86ISD::SHLD: return "X86ISD::SHLD";
6763 case X86ISD::SHRD: return "X86ISD::SHRD";
6764 case X86ISD::FAND: return "X86ISD::FAND";
6765 case X86ISD::FOR: return "X86ISD::FOR";
6766 case X86ISD::FXOR: return "X86ISD::FXOR";
6767 case X86ISD::FSRL: return "X86ISD::FSRL";
6768 case X86ISD::FILD: return "X86ISD::FILD";
6769 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6770 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6771 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6772 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6773 case X86ISD::FLD: return "X86ISD::FLD";
6774 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006775 case X86ISD::CALL: return "X86ISD::CALL";
6776 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6777 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohman7fe9b7f2008-12-23 22:45:23 +00006778 case X86ISD::BT: return "X86ISD::BT";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006779 case X86ISD::CMP: return "X86ISD::CMP";
6780 case X86ISD::COMI: return "X86ISD::COMI";
6781 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6782 case X86ISD::SETCC: return "X86ISD::SETCC";
6783 case X86ISD::CMOV: return "X86ISD::CMOV";
6784 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6785 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6786 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6787 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006788 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6789 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006790 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006791 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006792 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6793 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006794 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6795 case X86ISD::FMAX: return "X86ISD::FMAX";
6796 case X86ISD::FMIN: return "X86ISD::FMIN";
6797 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6798 case X86ISD::FRCP: return "X86ISD::FRCP";
6799 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6800 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6801 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006802 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006803 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006804 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6805 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006806 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6807 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6808 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6809 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6810 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6811 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006812 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6813 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006814 case X86ISD::VSHL: return "X86ISD::VSHL";
6815 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006816 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6817 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6818 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6819 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6820 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6821 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6822 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6823 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6824 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6825 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingae034ed2008-12-12 00:56:36 +00006826 case X86ISD::ADD: return "X86ISD::ADD";
6827 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingf5399032008-12-12 21:15:41 +00006828 case X86ISD::SMUL: return "X86ISD::SMUL";
6829 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006830 }
6831}
6832
6833// isLegalAddressingMode - Return true if the addressing mode represented
6834// by AM is legal for this target, for a load/store of the specified type.
6835bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6836 const Type *Ty) const {
6837 // X86 supports extremely general addressing modes.
6838
6839 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6840 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6841 return false;
6842
6843 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006844 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006845 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6846 return false;
Dale Johannesen64660e92008-12-05 21:47:27 +00006847 // If BaseGV requires a register, we cannot also have a BaseReg.
6848 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6849 AM.HasBaseReg)
6850 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006851
6852 // X86-64 only supports addr of globals in small code model.
6853 if (Subtarget->is64Bit()) {
6854 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6855 return false;
6856 // If lower 4G is not available, then we must use rip-relative addressing.
6857 if (AM.BaseOffs || AM.Scale > 1)
6858 return false;
6859 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006860 }
6861
6862 switch (AM.Scale) {
6863 case 0:
6864 case 1:
6865 case 2:
6866 case 4:
6867 case 8:
6868 // These scales always work.
6869 break;
6870 case 3:
6871 case 5:
6872 case 9:
6873 // These scales are formed with basereg+scalereg. Only accept if there is
6874 // no basereg yet.
6875 if (AM.HasBaseReg)
6876 return false;
6877 break;
6878 default: // Other stuff never works.
6879 return false;
6880 }
6881
6882 return true;
6883}
6884
6885
Evan Cheng27a820a2007-10-26 01:56:11 +00006886bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6887 if (!Ty1->isInteger() || !Ty2->isInteger())
6888 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006889 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6890 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006891 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006892 return false;
6893 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006894}
6895
Duncan Sands92c43912008-06-06 12:08:01 +00006896bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6897 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006898 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006899 unsigned NumBits1 = VT1.getSizeInBits();
6900 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006901 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006902 return false;
6903 return Subtarget->is64Bit() || NumBits1 < 64;
6904}
Evan Cheng27a820a2007-10-26 01:56:11 +00006905
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006906/// isShuffleMaskLegal - Targets can use this to indicate that they only
6907/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6908/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6909/// are assumed to be legal.
6910bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006911X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006912 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006913 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006914 return (Mask.getNode()->getNumOperands() <= 4 ||
6915 isIdentityMask(Mask.getNode()) ||
6916 isIdentityMask(Mask.getNode(), true) ||
6917 isSplatMask(Mask.getNode()) ||
6918 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6919 X86::isUNPCKLMask(Mask.getNode()) ||
6920 X86::isUNPCKHMask(Mask.getNode()) ||
6921 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6922 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006923}
6924
Dan Gohman48d5f062008-04-09 20:09:42 +00006925bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006926X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006927 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006928 unsigned NumElts = BVOps.size();
6929 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006930 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006931 if (NumElts == 2) return true;
6932 if (NumElts == 4) {
6933 return (isMOVLMask(&BVOps[0], 4) ||
6934 isCommutedMOVL(&BVOps[0], 4, true) ||
6935 isSHUFPMask(&BVOps[0], 4) ||
6936 isCommutedSHUFP(&BVOps[0], 4));
6937 }
6938 return false;
6939}
6940
6941//===----------------------------------------------------------------------===//
6942// X86 Scheduler Hooks
6943//===----------------------------------------------------------------------===//
6944
Mon P Wang078a62d2008-05-05 19:05:59 +00006945// private utility function
6946MachineBasicBlock *
6947X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6948 MachineBasicBlock *MBB,
6949 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006950 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006951 unsigned LoadOpc,
6952 unsigned CXchgOpc,
6953 unsigned copyOpc,
6954 unsigned notOpc,
6955 unsigned EAXreg,
6956 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006957 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006958 // For the atomic bitwise operator, we generate
6959 // thisMBB:
6960 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006961 // ld t1 = [bitinstr.addr]
6962 // op t2 = t1, [bitinstr.val]
6963 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006964 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6965 // bz newMBB
6966 // fallthrough -->nextMBB
6967 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6968 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006969 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006970 ++MBBIter;
6971
6972 /// First build the CFG
6973 MachineFunction *F = MBB->getParent();
6974 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006975 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6976 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6977 F->insert(MBBIter, newMBB);
6978 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006979
6980 // Move all successors to thisMBB to nextMBB
6981 nextMBB->transferSuccessors(thisMBB);
6982
6983 // Update thisMBB to fall through to newMBB
6984 thisMBB->addSuccessor(newMBB);
6985
6986 // newMBB jumps to itself and fall through to nextMBB
6987 newMBB->addSuccessor(nextMBB);
6988 newMBB->addSuccessor(newMBB);
6989
6990 // Insert instructions into newMBB based on incoming instruction
6991 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006992 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang078a62d2008-05-05 19:05:59 +00006993 MachineOperand& destOper = bInstr->getOperand(0);
6994 MachineOperand* argOpers[6];
6995 int numArgs = bInstr->getNumOperands() - 1;
6996 for (int i=0; i < numArgs; ++i)
6997 argOpers[i] = &bInstr->getOperand(i+1);
6998
6999 // x86 address has 4 operands: base, index, scale, and displacement
7000 int lastAddrIndx = 3; // [0,3]
7001 int valArgIndx = 4;
7002
Dale Johannesend20e4452008-08-19 18:47:28 +00007003 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007004 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00007005 for (int i=0; i <= lastAddrIndx; ++i)
7006 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007007
Dale Johannesend20e4452008-08-19 18:47:28 +00007008 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007009 if (invSrc) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007010 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007011 }
7012 else
7013 tt = t1;
7014
Dale Johannesend20e4452008-08-19 18:47:28 +00007015 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007016 assert((argOpers[valArgIndx]->isReg() ||
7017 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00007018 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007019 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007020 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00007021 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007022 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007023 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00007024 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007025
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007026 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00007027 MIB.addReg(t1);
7028
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007029 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00007030 for (int i=0; i <= lastAddrIndx; ++i)
7031 (*MIB).addOperand(*argOpers[i]);
7032 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00007033 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7034 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7035
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007036 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesend20e4452008-08-19 18:47:28 +00007037 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00007038
7039 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007040 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007041
Dan Gohman221a4372008-07-07 23:14:23 +00007042 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00007043 return nextMBB;
7044}
7045
Dale Johannesen44eb5372008-10-03 19:41:08 +00007046// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00007047MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00007048X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7049 MachineBasicBlock *MBB,
7050 unsigned regOpcL,
7051 unsigned regOpcH,
7052 unsigned immOpcL,
7053 unsigned immOpcH,
7054 bool invSrc) {
7055 // For the atomic bitwise operator, we generate
7056 // thisMBB (instructions are in pairs, except cmpxchg8b)
7057 // ld t1,t2 = [bitinstr.addr]
7058 // newMBB:
7059 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7060 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007061 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00007062 // mov ECX, EBX <- t5, t6
7063 // mov EAX, EDX <- t1, t2
7064 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7065 // mov t3, t4 <- EAX, EDX
7066 // bz newMBB
7067 // result in out1, out2
7068 // fallthrough -->nextMBB
7069
7070 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7071 const unsigned LoadOpc = X86::MOV32rm;
7072 const unsigned copyOpc = X86::MOV32rr;
7073 const unsigned NotOpc = X86::NOT32r;
7074 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7075 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7076 MachineFunction::iterator MBBIter = MBB;
7077 ++MBBIter;
7078
7079 /// First build the CFG
7080 MachineFunction *F = MBB->getParent();
7081 MachineBasicBlock *thisMBB = MBB;
7082 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7083 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7084 F->insert(MBBIter, newMBB);
7085 F->insert(MBBIter, nextMBB);
7086
7087 // Move all successors to thisMBB to nextMBB
7088 nextMBB->transferSuccessors(thisMBB);
7089
7090 // Update thisMBB to fall through to newMBB
7091 thisMBB->addSuccessor(newMBB);
7092
7093 // newMBB jumps to itself and fall through to nextMBB
7094 newMBB->addSuccessor(nextMBB);
7095 newMBB->addSuccessor(newMBB);
7096
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007097 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesenf160d802008-10-02 18:53:47 +00007098 // Insert instructions into newMBB based on incoming instruction
7099 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
7100 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
7101 MachineOperand& dest1Oper = bInstr->getOperand(0);
7102 MachineOperand& dest2Oper = bInstr->getOperand(1);
7103 MachineOperand* argOpers[6];
7104 for (int i=0; i < 6; ++i)
7105 argOpers[i] = &bInstr->getOperand(i+2);
7106
7107 // x86 address has 4 operands: base, index, scale, and displacement
7108 int lastAddrIndx = 3; // [0,3]
7109
7110 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007111 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesenf160d802008-10-02 18:53:47 +00007112 for (int i=0; i <= lastAddrIndx; ++i)
7113 (*MIB).addOperand(*argOpers[i]);
7114 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007115 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007116 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00007117 for (int i=0; i <= lastAddrIndx-1; ++i)
7118 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007119 MachineOperand newOp3 = *(argOpers[3]);
7120 if (newOp3.isImm())
7121 newOp3.setImm(newOp3.getImm()+4);
7122 else
7123 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00007124 (*MIB).addOperand(newOp3);
7125
7126 // t3/4 are defined later, at the bottom of the loop
7127 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7128 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007129 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007130 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007131 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007132 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7133
7134 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7135 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
7136 if (invSrc) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007137 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7138 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesenf160d802008-10-02 18:53:47 +00007139 } else {
7140 tt1 = t1;
7141 tt2 = t2;
7142 }
7143
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007144 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00007145 "invalid operand");
7146 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7147 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007148 if (argOpers[4]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007149 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesenf160d802008-10-02 18:53:47 +00007150 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007151 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007152 if (regOpcL != X86::MOV32rr)
7153 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00007154 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007155 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
7156 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
7157 if (argOpers[5]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007158 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesenf160d802008-10-02 18:53:47 +00007159 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007160 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007161 if (regOpcH != X86::MOV32rr)
7162 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00007163 (*MIB).addOperand(*argOpers[5]);
7164
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007165 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007166 MIB.addReg(t1);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007167 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007168 MIB.addReg(t2);
7169
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007170 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007171 MIB.addReg(t5);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007172 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007173 MIB.addReg(t6);
7174
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007175 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesenf160d802008-10-02 18:53:47 +00007176 for (int i=0; i <= lastAddrIndx; ++i)
7177 (*MIB).addOperand(*argOpers[i]);
7178
7179 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7180 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7181
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007182 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesenf160d802008-10-02 18:53:47 +00007183 MIB.addReg(X86::EAX);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007184 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesenf160d802008-10-02 18:53:47 +00007185 MIB.addReg(X86::EDX);
7186
7187 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007188 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesenf160d802008-10-02 18:53:47 +00007189
7190 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7191 return nextMBB;
7192}
7193
7194// private utility function
7195MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00007196X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7197 MachineBasicBlock *MBB,
7198 unsigned cmovOpc) {
7199 // For the atomic min/max operator, we generate
7200 // thisMBB:
7201 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00007202 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00007203 // mov t2 = [min/max.val]
7204 // cmp t1, t2
7205 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00007206 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00007207 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7208 // bz newMBB
7209 // fallthrough -->nextMBB
7210 //
7211 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7212 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007213 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00007214 ++MBBIter;
7215
7216 /// First build the CFG
7217 MachineFunction *F = MBB->getParent();
7218 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00007219 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7220 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7221 F->insert(MBBIter, newMBB);
7222 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007223
7224 // Move all successors to thisMBB to nextMBB
7225 nextMBB->transferSuccessors(thisMBB);
7226
7227 // Update thisMBB to fall through to newMBB
7228 thisMBB->addSuccessor(newMBB);
7229
7230 // newMBB jumps to newMBB and fall through to nextMBB
7231 newMBB->addSuccessor(nextMBB);
7232 newMBB->addSuccessor(newMBB);
7233
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007234 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang078a62d2008-05-05 19:05:59 +00007235 // Insert instructions into newMBB based on incoming instruction
7236 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
7237 MachineOperand& destOper = mInstr->getOperand(0);
7238 MachineOperand* argOpers[6];
7239 int numArgs = mInstr->getNumOperands() - 1;
7240 for (int i=0; i < numArgs; ++i)
7241 argOpers[i] = &mInstr->getOperand(i+1);
7242
7243 // x86 address has 4 operands: base, index, scale, and displacement
7244 int lastAddrIndx = 3; // [0,3]
7245 int valArgIndx = 4;
7246
Mon P Wang318b0372008-05-05 22:56:23 +00007247 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007248 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00007249 for (int i=0; i <= lastAddrIndx; ++i)
7250 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00007251
Mon P Wang078a62d2008-05-05 19:05:59 +00007252 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007253 assert((argOpers[valArgIndx]->isReg() ||
7254 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00007255 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00007256
7257 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007258 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007259 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00007260 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007261 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00007262 (*MIB).addOperand(*argOpers[valArgIndx]);
7263
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007264 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wang318b0372008-05-05 22:56:23 +00007265 MIB.addReg(t1);
7266
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007267 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang078a62d2008-05-05 19:05:59 +00007268 MIB.addReg(t1);
7269 MIB.addReg(t2);
7270
7271 // Generate movc
7272 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007273 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang078a62d2008-05-05 19:05:59 +00007274 MIB.addReg(t2);
7275 MIB.addReg(t1);
7276
7277 // Cmp and exchange if none has modified the memory location
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007278 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang078a62d2008-05-05 19:05:59 +00007279 for (int i=0; i <= lastAddrIndx; ++i)
7280 (*MIB).addOperand(*argOpers[i]);
7281 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00007282 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7283 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00007284
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007285 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang078a62d2008-05-05 19:05:59 +00007286 MIB.addReg(X86::EAX);
7287
7288 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007289 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007290
Dan Gohman221a4372008-07-07 23:14:23 +00007291 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00007292 return nextMBB;
7293}
7294
7295
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007296MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00007297X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
7298 MachineBasicBlock *BB) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007299 DebugLoc dl = MI->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007300 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7301 switch (MI->getOpcode()) {
7302 default: assert(false && "Unexpected instr type to insert");
Mon P Wang83edba52008-12-12 01:25:51 +00007303 case X86::CMOV_V1I64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007304 case X86::CMOV_FR32:
7305 case X86::CMOV_FR64:
7306 case X86::CMOV_V4F32:
7307 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00007308 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007309 // To "insert" a SELECT_CC instruction, we actually have to insert the
7310 // diamond control-flow pattern. The incoming instruction knows the
7311 // destination vreg to set, the condition code register to branch on, the
7312 // true/false values to select between, and a branch opcode to use.
7313 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007314 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007315 ++It;
7316
7317 // thisMBB:
7318 // ...
7319 // TrueVal = ...
7320 // cmpTY ccX, r1, r2
7321 // bCC copy1MBB
7322 // fallthrough --> copy0MBB
7323 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00007324 MachineFunction *F = BB->getParent();
7325 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7326 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007327 unsigned Opc =
7328 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007329 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00007330 F->insert(It, copy0MBB);
7331 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007332 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007333 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00007334 sinkMBB->transferSuccessors(BB);
7335
7336 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007337 BB->addSuccessor(copy0MBB);
7338 BB->addSuccessor(sinkMBB);
7339
7340 // copy0MBB:
7341 // %FalseValue = ...
7342 // # fallthrough to sinkMBB
7343 BB = copy0MBB;
7344
7345 // Update machine-CFG edges
7346 BB->addSuccessor(sinkMBB);
7347
7348 // sinkMBB:
7349 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7350 // ...
7351 BB = sinkMBB;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007352 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007353 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7354 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7355
Dan Gohman221a4372008-07-07 23:14:23 +00007356 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007357 return BB;
7358 }
7359
7360 case X86::FP32_TO_INT16_IN_MEM:
7361 case X86::FP32_TO_INT32_IN_MEM:
7362 case X86::FP32_TO_INT64_IN_MEM:
7363 case X86::FP64_TO_INT16_IN_MEM:
7364 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007365 case X86::FP64_TO_INT64_IN_MEM:
7366 case X86::FP80_TO_INT16_IN_MEM:
7367 case X86::FP80_TO_INT32_IN_MEM:
7368 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007369 // Change the floating point control register to use "round towards zero"
7370 // mode when truncating to an integer value.
7371 MachineFunction *F = BB->getParent();
7372 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007373 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007374
7375 // Load the old value of the high byte of the control word...
7376 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00007377 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007378 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
7379 CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007380
7381 // Set the high part to be round to zero...
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007382 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007383 .addImm(0xC7F);
7384
7385 // Reload the modified control word now...
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007386 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007387
7388 // Restore the memory image of control word to original value
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007389 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007390 .addReg(OldCW);
7391
7392 // Get the X86 opcode to use.
7393 unsigned Opc;
7394 switch (MI->getOpcode()) {
7395 default: assert(0 && "illegal opcode!");
7396 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7397 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7398 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7399 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7400 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7401 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007402 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7403 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7404 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007405 }
7406
7407 X86AddressMode AM;
7408 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007409 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007410 AM.BaseType = X86AddressMode::RegBase;
7411 AM.Base.Reg = Op.getReg();
7412 } else {
7413 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00007414 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007415 }
7416 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007417 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007418 AM.Scale = Op.getImm();
7419 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007420 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007421 AM.IndexReg = Op.getImm();
7422 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007423 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007424 AM.GV = Op.getGlobal();
7425 } else {
7426 AM.Disp = Op.getImm();
7427 }
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007428 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007429 .addReg(MI->getOperand(4).getReg());
7430
7431 // Reload the original control word now.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007432 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007433
Dan Gohman221a4372008-07-07 23:14:23 +00007434 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007435 return BB;
7436 }
Mon P Wang078a62d2008-05-05 19:05:59 +00007437 case X86::ATOMAND32:
7438 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007439 X86::AND32ri, X86::MOV32rm,
7440 X86::LCMPXCHG32, X86::MOV32rr,
7441 X86::NOT32r, X86::EAX,
7442 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007443 case X86::ATOMOR32:
7444 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007445 X86::OR32ri, X86::MOV32rm,
7446 X86::LCMPXCHG32, X86::MOV32rr,
7447 X86::NOT32r, X86::EAX,
7448 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007449 case X86::ATOMXOR32:
7450 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007451 X86::XOR32ri, X86::MOV32rm,
7452 X86::LCMPXCHG32, X86::MOV32rr,
7453 X86::NOT32r, X86::EAX,
7454 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007455 case X86::ATOMNAND32:
7456 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007457 X86::AND32ri, X86::MOV32rm,
7458 X86::LCMPXCHG32, X86::MOV32rr,
7459 X86::NOT32r, X86::EAX,
7460 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00007461 case X86::ATOMMIN32:
7462 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7463 case X86::ATOMMAX32:
7464 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7465 case X86::ATOMUMIN32:
7466 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7467 case X86::ATOMUMAX32:
7468 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00007469
7470 case X86::ATOMAND16:
7471 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7472 X86::AND16ri, X86::MOV16rm,
7473 X86::LCMPXCHG16, X86::MOV16rr,
7474 X86::NOT16r, X86::AX,
7475 X86::GR16RegisterClass);
7476 case X86::ATOMOR16:
7477 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7478 X86::OR16ri, X86::MOV16rm,
7479 X86::LCMPXCHG16, X86::MOV16rr,
7480 X86::NOT16r, X86::AX,
7481 X86::GR16RegisterClass);
7482 case X86::ATOMXOR16:
7483 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7484 X86::XOR16ri, X86::MOV16rm,
7485 X86::LCMPXCHG16, X86::MOV16rr,
7486 X86::NOT16r, X86::AX,
7487 X86::GR16RegisterClass);
7488 case X86::ATOMNAND16:
7489 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7490 X86::AND16ri, X86::MOV16rm,
7491 X86::LCMPXCHG16, X86::MOV16rr,
7492 X86::NOT16r, X86::AX,
7493 X86::GR16RegisterClass, true);
7494 case X86::ATOMMIN16:
7495 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7496 case X86::ATOMMAX16:
7497 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7498 case X86::ATOMUMIN16:
7499 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7500 case X86::ATOMUMAX16:
7501 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7502
7503 case X86::ATOMAND8:
7504 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7505 X86::AND8ri, X86::MOV8rm,
7506 X86::LCMPXCHG8, X86::MOV8rr,
7507 X86::NOT8r, X86::AL,
7508 X86::GR8RegisterClass);
7509 case X86::ATOMOR8:
7510 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7511 X86::OR8ri, X86::MOV8rm,
7512 X86::LCMPXCHG8, X86::MOV8rr,
7513 X86::NOT8r, X86::AL,
7514 X86::GR8RegisterClass);
7515 case X86::ATOMXOR8:
7516 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7517 X86::XOR8ri, X86::MOV8rm,
7518 X86::LCMPXCHG8, X86::MOV8rr,
7519 X86::NOT8r, X86::AL,
7520 X86::GR8RegisterClass);
7521 case X86::ATOMNAND8:
7522 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7523 X86::AND8ri, X86::MOV8rm,
7524 X86::LCMPXCHG8, X86::MOV8rr,
7525 X86::NOT8r, X86::AL,
7526 X86::GR8RegisterClass, true);
7527 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00007528 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007529 case X86::ATOMAND64:
7530 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7531 X86::AND64ri32, X86::MOV64rm,
7532 X86::LCMPXCHG64, X86::MOV64rr,
7533 X86::NOT64r, X86::RAX,
7534 X86::GR64RegisterClass);
7535 case X86::ATOMOR64:
7536 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7537 X86::OR64ri32, X86::MOV64rm,
7538 X86::LCMPXCHG64, X86::MOV64rr,
7539 X86::NOT64r, X86::RAX,
7540 X86::GR64RegisterClass);
7541 case X86::ATOMXOR64:
7542 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7543 X86::XOR64ri32, X86::MOV64rm,
7544 X86::LCMPXCHG64, X86::MOV64rr,
7545 X86::NOT64r, X86::RAX,
7546 X86::GR64RegisterClass);
7547 case X86::ATOMNAND64:
7548 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7549 X86::AND64ri32, X86::MOV64rm,
7550 X86::LCMPXCHG64, X86::MOV64rr,
7551 X86::NOT64r, X86::RAX,
7552 X86::GR64RegisterClass, true);
7553 case X86::ATOMMIN64:
7554 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7555 case X86::ATOMMAX64:
7556 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7557 case X86::ATOMUMIN64:
7558 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7559 case X86::ATOMUMAX64:
7560 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00007561
7562 // This group does 64-bit operations on a 32-bit host.
7563 case X86::ATOMAND6432:
7564 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7565 X86::AND32rr, X86::AND32rr,
7566 X86::AND32ri, X86::AND32ri,
7567 false);
7568 case X86::ATOMOR6432:
7569 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7570 X86::OR32rr, X86::OR32rr,
7571 X86::OR32ri, X86::OR32ri,
7572 false);
7573 case X86::ATOMXOR6432:
7574 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7575 X86::XOR32rr, X86::XOR32rr,
7576 X86::XOR32ri, X86::XOR32ri,
7577 false);
7578 case X86::ATOMNAND6432:
7579 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7580 X86::AND32rr, X86::AND32rr,
7581 X86::AND32ri, X86::AND32ri,
7582 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00007583 case X86::ATOMADD6432:
7584 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7585 X86::ADD32rr, X86::ADC32rr,
7586 X86::ADD32ri, X86::ADC32ri,
7587 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00007588 case X86::ATOMSUB6432:
7589 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7590 X86::SUB32rr, X86::SBB32rr,
7591 X86::SUB32ri, X86::SBB32ri,
7592 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007593 case X86::ATOMSWAP6432:
7594 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7595 X86::MOV32rr, X86::MOV32rr,
7596 X86::MOV32ri, X86::MOV32ri,
7597 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007598 }
7599}
7600
7601//===----------------------------------------------------------------------===//
7602// X86 Optimization Hooks
7603//===----------------------------------------------------------------------===//
7604
Dan Gohman8181bd12008-07-27 21:46:04 +00007605void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00007606 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00007607 APInt &KnownZero,
7608 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007609 const SelectionDAG &DAG,
7610 unsigned Depth) const {
7611 unsigned Opc = Op.getOpcode();
7612 assert((Opc >= ISD::BUILTIN_OP_END ||
7613 Opc == ISD::INTRINSIC_WO_CHAIN ||
7614 Opc == ISD::INTRINSIC_W_CHAIN ||
7615 Opc == ISD::INTRINSIC_VOID) &&
7616 "Should use MaskedValueIsZero if you don't know whether Op"
7617 " is a target node!");
7618
Dan Gohman1d79e432008-02-13 23:07:24 +00007619 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007620 switch (Opc) {
7621 default: break;
Evan Cheng8e9b21c2009-02-02 09:15:04 +00007622 case X86ISD::ADD:
7623 case X86ISD::SUB:
7624 case X86ISD::SMUL:
7625 case X86ISD::UMUL:
7626 // These nodes' second result is a boolean.
7627 if (Op.getResNo() == 0)
7628 break;
7629 // Fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007630 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007631 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7632 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007633 break;
7634 }
7635}
7636
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007637/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007638/// node is a GlobalAddress + offset.
7639bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7640 GlobalValue* &GA, int64_t &Offset) const{
7641 if (N->getOpcode() == X86ISD::Wrapper) {
7642 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007643 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00007644 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007645 return true;
7646 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007647 }
Evan Chengef7be082008-05-12 19:56:52 +00007648 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007649}
7650
Evan Chengef7be082008-05-12 19:56:52 +00007651static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7652 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007653 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007654 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007655 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007656 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007657 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007658 return false;
7659}
7660
Dan Gohman8181bd12008-07-27 21:46:04 +00007661static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007662 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007663 SDNode *&Base,
7664 SelectionDAG &DAG, MachineFrameInfo *MFI,
7665 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007666 Base = NULL;
7667 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007668 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007669 if (Idx.getOpcode() == ISD::UNDEF) {
7670 if (!Base)
7671 return false;
7672 continue;
7673 }
7674
Dan Gohman8181bd12008-07-27 21:46:04 +00007675 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007676 if (!Elt.getNode() ||
7677 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007678 return false;
7679 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007680 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007681 if (Base->getOpcode() == ISD::UNDEF)
7682 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007683 continue;
7684 }
7685 if (Elt.getOpcode() == ISD::UNDEF)
7686 continue;
7687
Gabor Greif1c80d112008-08-28 21:40:38 +00007688 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007689 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007690 return false;
7691 }
7692 return true;
7693}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007694
7695/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7696/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7697/// if the load addresses are consecutive, non-overlapping, and in the right
7698/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007699static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007700 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007701 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007702 DebugLoc dl = N->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00007703 MVT VT = N->getValueType(0);
7704 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007705 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007706 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007707 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007708 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7709 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007710 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007711
Dan Gohman11821702007-07-27 17:16:43 +00007712 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007713 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007714 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7715 LD->getSrcValue(), LD->getSrcValueOffset(),
7716 LD->isVolatile());
7717 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7718 LD->getSrcValue(), LD->getSrcValueOffset(),
7719 LD->isVolatile(), LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007720}
7721
Evan Chengb6290462008-05-12 23:04:07 +00007722/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007723static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohman22cefb02009-01-29 01:59:02 +00007724 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng6617eed2008-09-24 23:26:36 +00007725 const X86Subtarget *Subtarget,
7726 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007727 unsigned NumOps = N->getNumOperands();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007728 DebugLoc dl = N->getDebugLoc();
Evan Chengdea99362008-05-29 08:22:04 +00007729
Evan Chenge9b9c672008-05-09 21:53:03 +00007730 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007731 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007732 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007733
Duncan Sands92c43912008-06-06 12:08:01 +00007734 MVT VT = N->getValueType(0);
7735 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007736 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7737 // We are looking for load i64 and zero extend. We want to transform
7738 // it before legalizer has a chance to expand it. Also look for i64
7739 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007740 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007741 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007742 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007743 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007744 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007745
7746 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007747 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007748 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007749 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007750 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007751 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007752 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007753 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007754 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007755
7756 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007757 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007758
7759 // Load must not be an extload.
7760 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007761 return SDValue();
Mon P Wang28b36412009-01-30 07:07:40 +00007762
7763 // Load type should legal type so we don't have to legalize it.
7764 if (!TLI.isTypeLegal(VT))
7765 return SDValue();
7766
Evan Cheng6617eed2008-09-24 23:26:36 +00007767 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7768 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007769 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Dan Gohman22cefb02009-01-29 01:59:02 +00007770 TargetLowering::TargetLoweringOpt TLO(DAG);
7771 TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1));
7772 DCI.CommitTargetLoweringOpt(TLO);
Evan Cheng6617eed2008-09-24 23:26:36 +00007773 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007774}
7775
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007776/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007777static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007778 const X86Subtarget *Subtarget) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007779 DebugLoc dl = N->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00007780 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007781
7782 // If we have SSE[12] support, try to form min/max nodes.
7783 if (Subtarget->hasSSE2() &&
7784 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7785 if (Cond.getOpcode() == ISD::SETCC) {
7786 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007787 SDValue LHS = N->getOperand(1);
7788 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007789 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7790
7791 unsigned Opcode = 0;
7792 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7793 switch (CC) {
7794 default: break;
7795 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7796 case ISD::SETULE:
7797 case ISD::SETLE:
7798 if (!UnsafeFPMath) break;
7799 // FALL THROUGH.
7800 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7801 case ISD::SETLT:
7802 Opcode = X86ISD::FMIN;
7803 break;
7804
7805 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7806 case ISD::SETUGT:
7807 case ISD::SETGT:
7808 if (!UnsafeFPMath) break;
7809 // FALL THROUGH.
7810 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7811 case ISD::SETGE:
7812 Opcode = X86ISD::FMAX;
7813 break;
7814 }
7815 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7816 switch (CC) {
7817 default: break;
7818 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7819 case ISD::SETUGT:
7820 case ISD::SETGT:
7821 if (!UnsafeFPMath) break;
7822 // FALL THROUGH.
7823 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7824 case ISD::SETGE:
7825 Opcode = X86ISD::FMIN;
7826 break;
7827
7828 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7829 case ISD::SETULE:
7830 case ISD::SETLE:
7831 if (!UnsafeFPMath) break;
7832 // FALL THROUGH.
7833 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7834 case ISD::SETLT:
7835 Opcode = X86ISD::FMAX;
7836 break;
7837 }
7838 }
7839
7840 if (Opcode)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007841 return DAG.getNode(Opcode, dl, N->getValueType(0), LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007842 }
7843
7844 }
7845
Dan Gohman8181bd12008-07-27 21:46:04 +00007846 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007847}
7848
sampo025b75c2009-01-26 00:52:55 +00007849/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
7850/// when possible.
7851static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
7852 const X86Subtarget *Subtarget) {
7853 // On X86 with SSE2 support, we can transform this to a vector shift if
7854 // all elements are shifted by the same amount. We can't do this in legalize
7855 // because the a constant vector is typically transformed to a constant pool
7856 // so we have no knowledge of the shift amount.
sampo087d53c2009-01-26 03:15:31 +00007857 if (!Subtarget->hasSSE2())
7858 return SDValue();
7859
sampo025b75c2009-01-26 00:52:55 +00007860 MVT VT = N->getValueType(0);
sampo087d53c2009-01-26 03:15:31 +00007861 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
7862 return SDValue();
7863
Mon P Wanga91e9642009-01-28 08:12:05 +00007864 SDValue ShAmtOp = N->getOperand(1);
7865 MVT EltVT = VT.getVectorElementType();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007866 DebugLoc dl = N->getDebugLoc();
Mon P Wanga91e9642009-01-28 08:12:05 +00007867 SDValue BaseShAmt;
7868 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
7869 unsigned NumElts = VT.getVectorNumElements();
7870 unsigned i = 0;
7871 for (; i != NumElts; ++i) {
7872 SDValue Arg = ShAmtOp.getOperand(i);
7873 if (Arg.getOpcode() == ISD::UNDEF) continue;
7874 BaseShAmt = Arg;
7875 break;
7876 }
7877 for (; i != NumElts; ++i) {
7878 SDValue Arg = ShAmtOp.getOperand(i);
7879 if (Arg.getOpcode() == ISD::UNDEF) continue;
7880 if (Arg != BaseShAmt) {
7881 return SDValue();
7882 }
7883 }
7884 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
7885 isSplatMask(ShAmtOp.getOperand(2).getNode())) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007886 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, ShAmtOp,
Mon P Wanga91e9642009-01-28 08:12:05 +00007887 DAG.getIntPtrConstant(0));
7888 } else
sampo087d53c2009-01-26 03:15:31 +00007889 return SDValue();
sampo025b75c2009-01-26 00:52:55 +00007890
sampo087d53c2009-01-26 03:15:31 +00007891 if (EltVT.bitsGT(MVT::i32))
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007892 BaseShAmt = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007893 else if (EltVT.bitsLT(MVT::i32))
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007894 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BaseShAmt);
sampo025b75c2009-01-26 00:52:55 +00007895
sampo087d53c2009-01-26 03:15:31 +00007896 // The shift amount is identical so we can do a vector shift.
7897 SDValue ValOp = N->getOperand(0);
7898 switch (N->getOpcode()) {
7899 default:
7900 assert(0 && "Unknown shift opcode!");
7901 break;
7902 case ISD::SHL:
7903 if (VT == MVT::v2i64)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007904 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007905 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7906 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007907 if (VT == MVT::v4i32)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007908 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007909 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
7910 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007911 if (VT == MVT::v8i16)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007912 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007913 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
7914 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007915 break;
7916 case ISD::SRA:
7917 if (VT == MVT::v4i32)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007918 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007919 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
7920 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007921 if (VT == MVT::v8i16)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007922 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007923 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
7924 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007925 break;
7926 case ISD::SRL:
7927 if (VT == MVT::v2i64)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007928 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007929 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7930 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007931 if (VT == MVT::v4i32)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007932 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007933 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
7934 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007935 if (VT == MVT::v8i16)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007936 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007937 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
7938 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007939 break;
sampo025b75c2009-01-26 00:52:55 +00007940 }
7941 return SDValue();
7942}
7943
Chris Lattnerce84ae42008-02-22 02:09:43 +00007944/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007945static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007946 const X86Subtarget *Subtarget) {
7947 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7948 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007949 // A preferable solution to the general problem is to figure out the right
7950 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007951 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007952 if (St->getValue().getValueType().isVector() &&
7953 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007954 isa<LoadSDNode>(St->getValue()) &&
7955 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7956 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007957 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007958 LoadSDNode *Ld = 0;
7959 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007960 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007961 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007962 // Must be a store of a load. We currently handle two cases: the load
7963 // is a direct child, and it's under an intervening TokenFactor. It is
7964 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007965 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007966 Ld = cast<LoadSDNode>(St->getChain());
7967 else if (St->getValue().hasOneUse() &&
7968 ChainVal->getOpcode() == ISD::TokenFactor) {
7969 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007970 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007971 TokenFactorIndex = i;
7972 Ld = cast<LoadSDNode>(St->getValue());
7973 } else
7974 Ops.push_back(ChainVal->getOperand(i));
7975 }
7976 }
7977 if (Ld) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007978 DebugLoc dl = N->getDebugLoc();
Dale Johannesend112b802008-02-25 19:20:14 +00007979 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7980 if (Subtarget->is64Bit()) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007981 SDValue NewLd = DAG.getLoad(MVT::i64, dl, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007982 Ld->getBasePtr(), Ld->getSrcValue(),
7983 Ld->getSrcValueOffset(), Ld->isVolatile(),
7984 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007985 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007986 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00007987 Ops.push_back(NewChain);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007988 NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Ops[0],
Dale Johannesend112b802008-02-25 19:20:14 +00007989 Ops.size());
7990 }
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007991 return DAG.getStore(NewChain, dl, NewLd, St->getBasePtr(),
Dale Johannesend112b802008-02-25 19:20:14 +00007992 St->getSrcValue(), St->getSrcValueOffset(),
7993 St->isVolatile(), St->getAlignment());
7994 }
7995
7996 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00007997 SDValue LoAddr = Ld->getBasePtr();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007998 SDValue HiAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007999 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00008000
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008001 SDValue LoLd = DAG.getLoad(MVT::i32, dl, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00008002 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8003 Ld->isVolatile(), Ld->getAlignment());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008004 SDValue HiLd = DAG.getLoad(MVT::i32, dl, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00008005 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8006 Ld->isVolatile(),
8007 MinAlign(Ld->getAlignment(), 4));
8008
Dan Gohman8181bd12008-07-27 21:46:04 +00008009 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00008010 if (TokenFactorIndex != -1) {
8011 Ops.push_back(LoLd);
8012 Ops.push_back(HiLd);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008013 NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Ops[0],
Dale Johannesend112b802008-02-25 19:20:14 +00008014 Ops.size());
8015 }
8016
8017 LoAddr = St->getBasePtr();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008018 HiAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00008019 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00008020
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008021 SDValue LoSt = DAG.getStore(NewChain, dl, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00008022 St->getSrcValue(), St->getSrcValueOffset(),
8023 St->isVolatile(), St->getAlignment());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008024 SDValue HiSt = DAG.getStore(NewChain, dl, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00008025 St->getSrcValue(),
8026 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00008027 St->isVolatile(),
8028 MinAlign(St->getAlignment(), 4));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008029 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00008030 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00008031 }
Dan Gohman8181bd12008-07-27 21:46:04 +00008032 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00008033}
8034
Chris Lattner470d5dc2008-01-25 06:14:17 +00008035/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8036/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00008037static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00008038 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8039 // F[X]OR(0.0, x) -> x
8040 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00008041 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8042 if (C->getValueAPF().isPosZero())
8043 return N->getOperand(1);
8044 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8045 if (C->getValueAPF().isPosZero())
8046 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00008047 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00008048}
8049
8050/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00008051static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00008052 // FAND(0.0, x) -> 0.0
8053 // FAND(x, 0.0) -> 0.0
8054 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8055 if (C->getValueAPF().isPosZero())
8056 return N->getOperand(0);
8057 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8058 if (C->getValueAPF().isPosZero())
8059 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00008060 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00008061}
8062
Dan Gohman22cefb02009-01-29 01:59:02 +00008063static SDValue PerformBTCombine(SDNode *N,
8064 SelectionDAG &DAG,
8065 TargetLowering::DAGCombinerInfo &DCI) {
8066 // BT ignores high bits in the bit index operand.
8067 SDValue Op1 = N->getOperand(1);
8068 if (Op1.hasOneUse()) {
8069 unsigned BitWidth = Op1.getValueSizeInBits();
8070 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8071 APInt KnownZero, KnownOne;
8072 TargetLowering::TargetLoweringOpt TLO(DAG);
8073 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8074 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8075 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8076 DCI.CommitTargetLoweringOpt(TLO);
8077 }
8078 return SDValue();
8079}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008080
Dan Gohman8181bd12008-07-27 21:46:04 +00008081SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00008082 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008083 SelectionDAG &DAG = DCI.DAG;
8084 switch (N->getOpcode()) {
8085 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00008086 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
8087 case ISD::BUILD_VECTOR:
Dan Gohman22cefb02009-01-29 01:59:02 +00008088 return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00008089 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
sampo025b75c2009-01-26 00:52:55 +00008090 case ISD::SHL:
8091 case ISD::SRA:
8092 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00008093 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00008094 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00008095 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8096 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohman22cefb02009-01-29 01:59:02 +00008097 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008098 }
8099
Dan Gohman8181bd12008-07-27 21:46:04 +00008100 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008101}
8102
8103//===----------------------------------------------------------------------===//
8104// X86 Inline Assembly Support
8105//===----------------------------------------------------------------------===//
8106
8107/// getConstraintType - Given a constraint letter, return the type of
8108/// constraint it is for this target.
8109X86TargetLowering::ConstraintType
8110X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8111 if (Constraint.size() == 1) {
8112 switch (Constraint[0]) {
8113 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +00008114 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +00008115 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008116 case 'r':
8117 case 'R':
8118 case 'l':
8119 case 'q':
8120 case 'Q':
8121 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00008122 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008123 case 'Y':
8124 return C_RegisterClass;
8125 default:
8126 break;
8127 }
8128 }
8129 return TargetLowering::getConstraintType(Constraint);
8130}
8131
Dale Johannesene99fc902008-01-29 02:21:21 +00008132/// LowerXConstraint - try to replace an X constraint, which matches anything,
8133/// with another that has more specific requirements based on the type of the
8134/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00008135const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00008136LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00008137 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8138 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00008139 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00008140 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00008141 return "Y";
8142 if (Subtarget->hasSSE1())
8143 return "x";
8144 }
8145
8146 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00008147}
8148
Chris Lattnera531abc2007-08-25 00:47:38 +00008149/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8150/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00008151void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00008152 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00008153 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00008154 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00008155 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00008156 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00008157
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008158 switch (Constraint) {
8159 default: break;
8160 case 'I':
8161 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008162 if (C->getZExtValue() <= 31) {
8163 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008164 break;
8165 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008166 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008167 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00008168 case 'J':
8169 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8170 if (C->getZExtValue() <= 63) {
8171 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8172 break;
8173 }
8174 }
8175 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008176 case 'N':
8177 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008178 if (C->getZExtValue() <= 255) {
8179 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008180 break;
8181 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008182 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008183 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008184 case 'i': {
8185 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00008186 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008187 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008188 break;
8189 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008190
8191 // If we are in non-pic codegen mode, we allow the address of a global (with
8192 // an optional displacement) to be used with 'i'.
8193 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
8194 int64_t Offset = 0;
8195
8196 // Match either (GA) or (GA+C)
8197 if (GA) {
8198 Offset = GA->getOffset();
8199 } else if (Op.getOpcode() == ISD::ADD) {
8200 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8201 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8202 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008203 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008204 } else {
8205 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8206 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8207 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008208 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008209 else
8210 C = 0, GA = 0;
8211 }
8212 }
8213
8214 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00008215 if (hasMemory)
Dan Gohman36322c72008-10-18 02:06:02 +00008216 Op = LowerGlobalAddress(GA->getGlobal(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00008217 else
8218 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8219 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00008220 Result = Op;
8221 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008222 }
8223
8224 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00008225 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008226 }
8227 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008228
Gabor Greif1c80d112008-08-28 21:40:38 +00008229 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00008230 Ops.push_back(Result);
8231 return;
8232 }
Evan Cheng7f250d62008-09-24 00:05:32 +00008233 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8234 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008235}
8236
8237std::vector<unsigned> X86TargetLowering::
8238getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00008239 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008240 if (Constraint.size() == 1) {
8241 // FIXME: not handling fp-stack yet!
8242 switch (Constraint[0]) { // GCC X86 Constraint Letters
8243 default: break; // Unknown constraint letter
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008244 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8245 case 'Q': // Q_REGS
8246 if (VT == MVT::i32)
8247 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8248 else if (VT == MVT::i16)
8249 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8250 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00008251 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00008252 else if (VT == MVT::i64)
8253 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8254 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008255 }
8256 }
8257
8258 return std::vector<unsigned>();
8259}
8260
8261std::pair<unsigned, const TargetRegisterClass*>
8262X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00008263 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008264 // First, see if this is a constraint that directly corresponds to an LLVM
8265 // register class.
8266 if (Constraint.size() == 1) {
8267 // GCC Constraint Letters
8268 switch (Constraint[0]) {
8269 default: break;
8270 case 'r': // GENERAL_REGS
8271 case 'R': // LEGACY_REGS
8272 case 'l': // INDEX_REGS
Chris Lattnerbbfea052008-10-17 18:15:05 +00008273 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008274 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00008275 if (VT == MVT::i16)
8276 return std::make_pair(0U, X86::GR16RegisterClass);
8277 if (VT == MVT::i32 || !Subtarget->is64Bit())
8278 return std::make_pair(0U, X86::GR32RegisterClass);
8279 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00008280 case 'f': // FP Stack registers.
8281 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8282 // value to the correct fpstack register class.
8283 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8284 return std::make_pair(0U, X86::RFP32RegisterClass);
8285 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8286 return std::make_pair(0U, X86::RFP64RegisterClass);
8287 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008288 case 'y': // MMX_REGS if MMX allowed.
8289 if (!Subtarget->hasMMX()) break;
8290 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008291 case 'Y': // SSE_REGS if SSE2 allowed
8292 if (!Subtarget->hasSSE2()) break;
8293 // FALL THROUGH.
8294 case 'x': // SSE_REGS if SSE1 allowed
8295 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00008296
8297 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008298 default: break;
8299 // Scalar SSE types.
8300 case MVT::f32:
8301 case MVT::i32:
8302 return std::make_pair(0U, X86::FR32RegisterClass);
8303 case MVT::f64:
8304 case MVT::i64:
8305 return std::make_pair(0U, X86::FR64RegisterClass);
8306 // Vector types.
8307 case MVT::v16i8:
8308 case MVT::v8i16:
8309 case MVT::v4i32:
8310 case MVT::v2i64:
8311 case MVT::v4f32:
8312 case MVT::v2f64:
8313 return std::make_pair(0U, X86::VR128RegisterClass);
8314 }
8315 break;
8316 }
8317 }
8318
8319 // Use the default implementation in TargetLowering to convert the register
8320 // constraint into a member of a register class.
8321 std::pair<unsigned, const TargetRegisterClass*> Res;
8322 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8323
8324 // Not found as a standard register?
8325 if (Res.second == 0) {
8326 // GCC calls "st(0)" just plain "st".
8327 if (StringsEqualNoCase("{st}", Constraint)) {
8328 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00008329 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008330 }
Dale Johannesen73920c02008-11-13 21:52:36 +00008331 // 'A' means EAX + EDX.
8332 if (Constraint == "A") {
8333 Res.first = X86::EAX;
8334 Res.second = X86::GRADRegisterClass;
8335 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008336 return Res;
8337 }
8338
8339 // Otherwise, check to see if this is a register class of the wrong value
8340 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8341 // turn into {ax},{dx}.
8342 if (Res.second->hasType(VT))
8343 return Res; // Correct type already, nothing to do.
8344
8345 // All of the single-register GCC register classes map their values onto
8346 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8347 // really want an 8-bit or 32-bit register, map to the appropriate register
8348 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00008349 if (Res.second == X86::GR16RegisterClass) {
8350 if (VT == MVT::i8) {
8351 unsigned DestReg = 0;
8352 switch (Res.first) {
8353 default: break;
8354 case X86::AX: DestReg = X86::AL; break;
8355 case X86::DX: DestReg = X86::DL; break;
8356 case X86::CX: DestReg = X86::CL; break;
8357 case X86::BX: DestReg = X86::BL; break;
8358 }
8359 if (DestReg) {
8360 Res.first = DestReg;
8361 Res.second = Res.second = X86::GR8RegisterClass;
8362 }
8363 } else if (VT == MVT::i32) {
8364 unsigned DestReg = 0;
8365 switch (Res.first) {
8366 default: break;
8367 case X86::AX: DestReg = X86::EAX; break;
8368 case X86::DX: DestReg = X86::EDX; break;
8369 case X86::CX: DestReg = X86::ECX; break;
8370 case X86::BX: DestReg = X86::EBX; break;
8371 case X86::SI: DestReg = X86::ESI; break;
8372 case X86::DI: DestReg = X86::EDI; break;
8373 case X86::BP: DestReg = X86::EBP; break;
8374 case X86::SP: DestReg = X86::ESP; break;
8375 }
8376 if (DestReg) {
8377 Res.first = DestReg;
8378 Res.second = Res.second = X86::GR32RegisterClass;
8379 }
8380 } else if (VT == MVT::i64) {
8381 unsigned DestReg = 0;
8382 switch (Res.first) {
8383 default: break;
8384 case X86::AX: DestReg = X86::RAX; break;
8385 case X86::DX: DestReg = X86::RDX; break;
8386 case X86::CX: DestReg = X86::RCX; break;
8387 case X86::BX: DestReg = X86::RBX; break;
8388 case X86::SI: DestReg = X86::RSI; break;
8389 case X86::DI: DestReg = X86::RDI; break;
8390 case X86::BP: DestReg = X86::RBP; break;
8391 case X86::SP: DestReg = X86::RSP; break;
8392 }
8393 if (DestReg) {
8394 Res.first = DestReg;
8395 Res.second = Res.second = X86::GR64RegisterClass;
8396 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008397 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00008398 } else if (Res.second == X86::FR32RegisterClass ||
8399 Res.second == X86::FR64RegisterClass ||
8400 Res.second == X86::VR128RegisterClass) {
8401 // Handle references to XMM physical registers that got mapped into the
8402 // wrong class. This can happen with constraints like {xmm0} where the
8403 // target independent register mapper will just pick the first match it can
8404 // find, ignoring the required type.
8405 if (VT == MVT::f32)
8406 Res.second = X86::FR32RegisterClass;
8407 else if (VT == MVT::f64)
8408 Res.second = X86::FR64RegisterClass;
8409 else if (X86::VR128RegisterClass->hasType(VT))
8410 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008411 }
8412
8413 return Res;
8414}
Mon P Wang1448aad2008-10-30 08:01:45 +00008415
8416//===----------------------------------------------------------------------===//
8417// X86 Widen vector type
8418//===----------------------------------------------------------------------===//
8419
8420/// getWidenVectorType: given a vector type, returns the type to widen
8421/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8422/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +00008423/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +00008424/// scalarizing vs using the wider vector type.
8425
Dan Gohman0fe66c92009-01-15 17:34:08 +00008426MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang1448aad2008-10-30 08:01:45 +00008427 assert(VT.isVector());
8428 if (isTypeLegal(VT))
8429 return VT;
8430
8431 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8432 // type based on element type. This would speed up our search (though
8433 // it may not be worth it since the size of the list is relatively
8434 // small).
8435 MVT EltVT = VT.getVectorElementType();
8436 unsigned NElts = VT.getVectorNumElements();
8437
8438 // On X86, it make sense to widen any vector wider than 1
8439 if (NElts <= 1)
8440 return MVT::Other;
8441
8442 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
8443 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8444 MVT SVT = (MVT::SimpleValueType)nVT;
8445
8446 if (isTypeLegal(SVT) &&
8447 SVT.getVectorElementType() == EltVT &&
8448 SVT.getVectorNumElements() > NElts)
8449 return SVT;
8450 }
8451 return MVT::Other;
8452}