blob: b2ed3b8617267cb672acff38ed802298be9b3d1c [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.
Nate Begeman543d2142009-04-27 18:41:29 +000048static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
49 SDValue V2);
Evan Cheng2aea0b42008-04-25 19:11:04 +000050
Dan Gohmanb41dfba2008-05-14 01:58:56 +000051X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 : TargetLowering(TM) {
53 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000054 X86ScalarSSEf64 = Subtarget->hasSSE2();
55 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000057
Chris Lattnerdec9cb52008-01-24 08:07:48 +000058 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059
60 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000061 TD = getTargetData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062
63 // Set up the TargetLowering object.
64
65 // X86 is weird, it always uses i8 for shift amounts and setcc results.
66 setShiftAmountType(MVT::i8);
Duncan Sands8cf4a822008-11-23 15:47:28 +000067 setBooleanContents(ZeroOrOneBooleanContent);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 setSchedulingPreference(SchedulingForRegPressure);
69 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
70 setStackPointerRegisterToSaveRestore(X86StackPtr);
71
72 if (Subtarget->isTargetDarwin()) {
73 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
74 setUseUnderscoreSetJmp(false);
75 setUseUnderscoreLongJmp(false);
76 } else if (Subtarget->isTargetMingw()) {
77 // MS runtime is weird: it exports _setjmp, but longjmp!
78 setUseUnderscoreSetJmp(true);
79 setUseUnderscoreLongJmp(false);
80 } else {
81 setUseUnderscoreSetJmp(true);
82 setUseUnderscoreLongJmp(true);
83 }
Scott Michel91099d62009-02-17 22:15:04 +000084
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 // Set up the register classes.
86 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
87 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
88 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
89 if (Subtarget->is64Bit())
90 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
91
Evan Cheng08c171a2008-10-14 21:26:46 +000092 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093
Scott Michel91099d62009-02-17 22:15:04 +000094 // We don't accept any truncstore of integer registers.
Chris Lattner3bc08502008-01-17 19:59:44 +000095 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
96 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
97 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
98 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
99 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
Evan Cheng71343822008-10-15 02:05:31 +0000100 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
101
102 // SETOEQ and SETUNE require checking two conditions.
103 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
104 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
105 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
106 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
107 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
108 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattner3bc08502008-01-17 19:59:44 +0000109
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
111 // operation.
112 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
113 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
114 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
115
116 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling6b42d012009-03-13 08:41:47 +0000118 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 } else {
Bill Wendling6b42d012009-03-13 08:41:47 +0000120 if (!UseSoftFloat && !NoImplicitFloat && X86ScalarSSEf64) {
Dale Johannesena359b8b2008-10-21 20:50:01 +0000121 // We have an impenetrably clever algorithm for ui64->double only.
122 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling14a30ef2009-01-17 03:56:04 +0000123
124 // We have faster algorithm for ui32->single only.
125 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Bill Wendling6b42d012009-03-13 08:41:47 +0000126 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling6b42d012009-03-13 08:41:47 +0000128 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 }
130
131 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
132 // this operation.
133 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
134 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling6b42d012009-03-13 08:41:47 +0000135
136 if (!UseSoftFloat && !NoImplicitFloat) {
137 // SSE has no i16 to fp conversion, only i32
138 if (X86ScalarSSEf32) {
139 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
140 // f32 and f64 cases are Legal, f80 case is not
141 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
142 } else {
143 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
144 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
145 }
Dale Johannesen2fc20782007-09-14 22:26:36 +0000146 } else {
Bill Wendling6b42d012009-03-13 08:41:47 +0000147 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
148 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 }
150
Dale Johannesen958b08b2007-09-19 23:55:34 +0000151 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
152 // are Legal, f80 is custom lowered.
153 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
154 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155
156 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
157 // this operation.
158 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
159 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
160
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000161 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000163 // f32 and f64 cases are Legal, f80 case is not
164 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 } else {
166 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
167 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
168 }
169
170 // Handle FP_TO_UINT by promoting the destination to a larger signed
171 // conversion.
172 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
173 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
174 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
175
176 if (Subtarget->is64Bit()) {
177 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
178 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
179 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000180 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 // Expand FP_TO_UINT into a select.
182 // FIXME: We would like to use a Custom expander here eventually to do
183 // the optimal thing for SSE vs. the default expansion in the legalizer.
184 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
185 else
186 // With SSE3 we can use fisttpll to convert to a signed i64.
187 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
188 }
189
190 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000191 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
193 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
194 }
195
Dan Gohman8450d862008-02-18 19:34:53 +0000196 // Scalar integer divide and remainder are lowered to use operations that
197 // produce two results, to match the available instructions. This exposes
198 // the two-result form to trivial CSE, which is able to combine x/y and x%y
199 // into a single instruction.
200 //
201 // Scalar integer multiply-high is also lowered to use two-result
202 // operations, to match the available instructions. However, plain multiply
203 // (low) operations are left as Legal, as there are single-result
204 // instructions for this in x86. Using the two-result multiply instructions
205 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000206 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
207 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
208 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
209 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
210 setOperationAction(ISD::SREM , MVT::i8 , Expand);
211 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000212 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
213 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
214 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
215 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
216 setOperationAction(ISD::SREM , MVT::i16 , Expand);
217 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000218 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
219 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
220 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
221 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
222 setOperationAction(ISD::SREM , MVT::i32 , Expand);
223 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000224 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
225 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
226 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
227 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
228 setOperationAction(ISD::SREM , MVT::i64 , Expand);
229 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000230
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
232 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
233 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
234 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000236 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
237 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
238 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
240 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000241 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000243 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000244 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michel91099d62009-02-17 22:15:04 +0000245
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000247 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
248 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000250 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
251 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000253 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
254 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 if (Subtarget->is64Bit()) {
256 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000257 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
258 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 }
260
261 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
262 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
263
264 // These should be promoted to a larger select which is supported.
265 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
266 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
267 // X86 wants to expand cmov itself.
268 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
269 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
270 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
271 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000272 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
274 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
275 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
276 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
277 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000278 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 if (Subtarget->is64Bit()) {
280 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
281 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
282 }
283 // X86 ret instruction may pop stack.
284 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000285 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286
287 // Darwin ABI issue.
288 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
289 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
290 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
291 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000292 if (Subtarget->is64Bit())
293 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000294 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 if (Subtarget->is64Bit()) {
296 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
297 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
298 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000299 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 }
301 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
302 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
303 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
304 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000305 if (Subtarget->is64Bit()) {
306 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
307 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
308 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
309 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310
Evan Cheng8d51ab32008-03-10 19:38:10 +0000311 if (Subtarget->hasSSE1())
312 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000313
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000314 if (!Subtarget->hasSSE2())
315 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
316
Mon P Wang078a62d2008-05-05 19:05:59 +0000317 // Expand certain atomics
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000318 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
319 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
320 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
321 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000322
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000323 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
324 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
325 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
326 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000327
Dale Johannesenf160d802008-10-02 18:53:47 +0000328 if (!Subtarget->is64Bit()) {
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000329 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
330 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
331 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
332 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
333 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
334 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
335 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesenf160d802008-10-02 18:53:47 +0000336 }
337
Dan Gohman472d12c2008-06-30 20:59:49 +0000338 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
339 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 // FIXME - use subtarget debug flags
341 if (!Subtarget->isTargetDarwin() &&
342 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000343 !Subtarget->isTargetCygMing()) {
344 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
345 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
346 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347
348 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
349 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
350 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
351 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
352 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 setExceptionPointerRegister(X86::RAX);
354 setExceptionSelectorRegister(X86::RDX);
355 } else {
356 setExceptionPointerRegister(X86::EAX);
357 setExceptionSelectorRegister(X86::EDX);
358 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000359 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000360 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
361
Duncan Sands7407a9f2007-09-11 14:10:23 +0000362 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000363
Chris Lattner56b941f2008-01-15 21:58:22 +0000364 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000365
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
367 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000369 if (Subtarget->is64Bit()) {
370 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000372 } else {
373 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000375 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376
377 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
378 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
379 if (Subtarget->is64Bit())
380 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
381 if (Subtarget->isTargetCygMing())
382 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
383 else
384 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
385
Evan Cheng0b84fe12009-02-13 22:36:38 +0000386 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000387 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388 // Set up the FP register classes.
389 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
390 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
391
392 // Use ANDPD to simulate FABS.
393 setOperationAction(ISD::FABS , MVT::f64, Custom);
394 setOperationAction(ISD::FABS , MVT::f32, Custom);
395
396 // Use XORP to simulate FNEG.
397 setOperationAction(ISD::FNEG , MVT::f64, Custom);
398 setOperationAction(ISD::FNEG , MVT::f32, Custom);
399
400 // Use ANDPD and ORPD to simulate FCOPYSIGN.
401 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
402 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
403
404 // We don't support sin/cos/fmod
405 setOperationAction(ISD::FSIN , MVT::f64, Expand);
406 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 setOperationAction(ISD::FSIN , MVT::f32, Expand);
408 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409
410 // Expand FP immediates into loads from the stack, except for the special
411 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000412 addLegalFPImmediate(APFloat(+0.0)); // xorpd
413 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000414
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000415 // Floating truncations from f80 and extensions to f80 go through memory.
416 // If optimizing, we lie about this though and handle it in
417 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
418 if (Fast) {
419 setConvertAction(MVT::f32, MVT::f80, Expand);
420 setConvertAction(MVT::f64, MVT::f80, Expand);
421 setConvertAction(MVT::f80, MVT::f32, Expand);
422 setConvertAction(MVT::f80, MVT::f64, Expand);
423 }
Evan Cheng0b84fe12009-02-13 22:36:38 +0000424 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000425 // Use SSE for f32, x87 for f64.
426 // Set up the FP register classes.
427 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
428 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
429
430 // Use ANDPS to simulate FABS.
431 setOperationAction(ISD::FABS , MVT::f32, Custom);
432
433 // Use XORP to simulate FNEG.
434 setOperationAction(ISD::FNEG , MVT::f32, Custom);
435
436 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
437
438 // Use ANDPS and ORPS to simulate FCOPYSIGN.
439 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
440 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
441
442 // We don't support sin/cos/fmod
443 setOperationAction(ISD::FSIN , MVT::f32, Expand);
444 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000445
Nate Begemane2ba64f2008-02-14 08:57:00 +0000446 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000447 addLegalFPImmediate(APFloat(+0.0f)); // xorps
448 addLegalFPImmediate(APFloat(+0.0)); // FLD0
449 addLegalFPImmediate(APFloat(+1.0)); // FLD1
450 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
451 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
452
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000453 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
454 // this though and handle it in InstructionSelectPreprocess so that
455 // dagcombine2 can hack on these.
456 if (Fast) {
457 setConvertAction(MVT::f32, MVT::f64, Expand);
458 setConvertAction(MVT::f32, MVT::f80, Expand);
Scott Michel91099d62009-02-17 22:15:04 +0000459 setConvertAction(MVT::f80, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000460 setConvertAction(MVT::f64, MVT::f32, Expand);
461 // And x87->x87 truncations also.
462 setConvertAction(MVT::f80, MVT::f64, Expand);
463 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000464
465 if (!UnsafeFPMath) {
466 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
467 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
468 }
Evan Cheng0b84fe12009-02-13 22:36:38 +0000469 } else if (!UseSoftFloat) {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000470 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 // Set up the FP register classes.
472 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
473 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
474
475 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
476 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
477 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
478 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000479
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000480 // Floating truncations go through memory. If optimizing, we lie about
481 // this though and handle it in InstructionSelectPreprocess so that
482 // dagcombine2 can hack on these.
483 if (Fast) {
Scott Michel91099d62009-02-17 22:15:04 +0000484 setConvertAction(MVT::f80, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000485 setConvertAction(MVT::f64, MVT::f32, Expand);
486 setConvertAction(MVT::f80, MVT::f64, Expand);
487 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488
489 if (!UnsafeFPMath) {
490 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
491 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
492 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000493 addLegalFPImmediate(APFloat(+0.0)); // FLD0
494 addLegalFPImmediate(APFloat(+1.0)); // FLD1
495 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
496 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000497 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
498 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
499 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
500 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 }
502
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000503 // Long double always uses X87.
Evan Chenge738dc32009-03-26 23:06:32 +0000504 if (!UseSoftFloat) {
Evan Cheng0b84fe12009-02-13 22:36:38 +0000505 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
506 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
507 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
508 {
509 bool ignored;
510 APFloat TmpFlt(+0.0);
511 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
512 &ignored);
513 addLegalFPImmediate(TmpFlt); // FLD0
514 TmpFlt.changeSign();
515 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
516 APFloat TmpFlt2(+1.0);
517 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
518 &ignored);
519 addLegalFPImmediate(TmpFlt2); // FLD1
520 TmpFlt2.changeSign();
521 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
522 }
Scott Michel91099d62009-02-17 22:15:04 +0000523
Evan Cheng0b84fe12009-02-13 22:36:38 +0000524 if (!UnsafeFPMath) {
525 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
526 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
527 }
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000528 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000529
Dan Gohman2f7b1982007-10-11 23:21:31 +0000530 // Always use a library call for pow.
531 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
532 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
533 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
534
Dale Johannesen92b33082008-09-04 00:47:13 +0000535 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000536 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000537 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000538 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000539 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
540
Mon P Wanga5a239f2008-11-06 05:31:54 +0000541 // First set operation action for all vector types to either promote
Mon P Wang1448aad2008-10-30 08:01:45 +0000542 // (for widening) or expand (for scalarization). Then we will selectively
543 // turn on ones that can be effectively codegen'd.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
545 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000546 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif825aa892008-08-28 23:19:51 +0000559 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
561 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands92c43912008-06-06 12:08:01 +0000562 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesen177edff2008-09-10 17:31:40 +0000584 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 }
590
Evan Cheng0b84fe12009-02-13 22:36:38 +0000591 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
592 // with -msoft-float, disable use of MMX as well.
Evan Chenge738dc32009-03-26 23:06:32 +0000593 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
595 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
596 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000597 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
599
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
601 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
602 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
603 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
604
605 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
606 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
607 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000608 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609
610 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
611 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
612
613 setOperationAction(ISD::AND, MVT::v8i8, Promote);
614 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
615 setOperationAction(ISD::AND, MVT::v4i16, Promote);
616 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
617 setOperationAction(ISD::AND, MVT::v2i32, Promote);
618 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
619 setOperationAction(ISD::AND, MVT::v1i64, Legal);
620
621 setOperationAction(ISD::OR, MVT::v8i8, Promote);
622 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
623 setOperationAction(ISD::OR, MVT::v4i16, Promote);
624 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
625 setOperationAction(ISD::OR, MVT::v2i32, Promote);
626 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
627 setOperationAction(ISD::OR, MVT::v1i64, Legal);
628
629 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
630 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
631 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
632 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
633 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
634 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
635 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
636
637 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
638 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
639 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
640 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
641 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
642 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000643 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
644 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
646
647 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
648 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
649 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000650 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
652
653 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
654 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
655 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
656 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
657
Evan Cheng759fe022008-07-22 18:39:19 +0000658 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
660 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000662
663 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang83edba52008-12-12 01:25:51 +0000664
Bill Wendling042eda32009-03-11 22:30:01 +0000665 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
Mon P Wang83edba52008-12-12 01:25:51 +0000666 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
667 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
668 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
669 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
670 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671 }
672
Evan Chenge738dc32009-03-26 23:06:32 +0000673 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
675
676 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
677 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
678 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
679 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
680 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
681 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
683 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
684 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
685 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
686 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000687 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688 }
689
Evan Chenge738dc32009-03-26 23:06:32 +0000690 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Cheng0b84fe12009-02-13 22:36:38 +0000692
Bill Wendling042eda32009-03-11 22:30:01 +0000693 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
694 // registers cannot be used even for integer operations.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
696 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
697 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
698 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
699
700 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
701 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
702 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
703 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wang14edb092008-12-18 21:42:19 +0000704 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
706 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
707 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
708 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
709 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
710 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
711 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
712 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
713 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
714 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
715 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716
Nate Begeman03605a02008-07-17 16:51:19 +0000717 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
718 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
719 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
720 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000721
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000722 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
723 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
724 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
725 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
727
728 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000729 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
730 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000731 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000732 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000733 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000734 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
735 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
736 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737 }
Bill Wendling042eda32009-03-11 22:30:01 +0000738
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
740 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
741 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
742 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000743 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendling042eda32009-03-11 22:30:01 +0000745
Nate Begeman4294c1f2008-02-12 22:51:28 +0000746 if (Subtarget->is64Bit()) {
747 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000748 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000749 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750
751 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
752 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000753 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
754 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
755 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
756 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
757 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
758 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
759 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
760 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
761 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
762 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 }
764
Chris Lattner3bc08502008-01-17 19:59:44 +0000765 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000766
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000767 // Custom lower v2i64 and v2f64 selects.
768 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
769 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
770 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
771 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michel91099d62009-02-17 22:15:04 +0000772
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773 }
Evan Cheng0b84fe12009-02-13 22:36:38 +0000774
Nate Begemand77e59e2008-02-11 04:19:36 +0000775 if (Subtarget->hasSSE41()) {
776 // FIXME: Do we need to handle scalar-to-vector here?
777 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
778
779 // i8 and i16 vectors are custom , because the source register and source
780 // source memory operand types are not the same width. f32 vectors are
781 // custom since the immediate controlling the insert encodes additional
782 // information.
783 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
784 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangac2a3c52009-01-15 21:10:20 +0000785 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000786 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
787
788 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
789 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangac2a3c52009-01-15 21:10:20 +0000790 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng6c249332008-03-24 21:52:23 +0000791 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000792
793 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000794 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
795 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000796 }
797 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798
Nate Begeman03605a02008-07-17 16:51:19 +0000799 if (Subtarget->hasSSE42()) {
800 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
801 }
Scott Michel91099d62009-02-17 22:15:04 +0000802
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803 // We want to custom lower some of our intrinsics.
804 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
805
Bill Wendling7e04be62008-12-09 22:08:41 +0000806 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling4c134df2008-11-24 19:21:46 +0000807 setOperationAction(ISD::SADDO, MVT::i32, Custom);
808 setOperationAction(ISD::SADDO, MVT::i64, Custom);
809 setOperationAction(ISD::UADDO, MVT::i32, Custom);
810 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling7e04be62008-12-09 22:08:41 +0000811 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
812 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
813 setOperationAction(ISD::USUBO, MVT::i32, Custom);
814 setOperationAction(ISD::USUBO, MVT::i64, Custom);
815 setOperationAction(ISD::SMULO, MVT::i32, Custom);
816 setOperationAction(ISD::SMULO, MVT::i64, Custom);
817 setOperationAction(ISD::UMULO, MVT::i32, Custom);
818 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Bill Wendling4c134df2008-11-24 19:21:46 +0000819
Evan Cheng9c215602009-03-31 19:38:51 +0000820 if (!Subtarget->is64Bit()) {
821 // These libcalls are not available in 32-bit.
822 setLibcallName(RTLIB::SHL_I128, 0);
823 setLibcallName(RTLIB::SRL_I128, 0);
824 setLibcallName(RTLIB::SRA_I128, 0);
825 }
826
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827 // We have target-specific dag combine patterns for the following nodes:
828 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000829 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830 setTargetDAGCombine(ISD::SELECT);
sampo025b75c2009-01-26 00:52:55 +0000831 setTargetDAGCombine(ISD::SHL);
832 setTargetDAGCombine(ISD::SRA);
833 setTargetDAGCombine(ISD::SRL);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000834 setTargetDAGCombine(ISD::STORE);
Evan Cheng04ecee12009-03-28 05:57:29 +0000835 if (Subtarget->is64Bit())
836 setTargetDAGCombine(ISD::MUL);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000837
838 computeRegisterProperties();
839
840 // FIXME: These should be based on subtarget info. Plus, the values should
841 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000842 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
843 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
844 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000845 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000846 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847}
848
Scott Michel502151f2008-03-10 15:42:14 +0000849
Duncan Sands4a361272009-01-01 15:52:00 +0000850MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel502151f2008-03-10 15:42:14 +0000851 return MVT::i8;
852}
853
854
Evan Cheng5a67b812008-01-23 23:17:41 +0000855/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
856/// the desired ByVal argument alignment.
857static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
858 if (MaxAlign == 16)
859 return;
860 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
861 if (VTy->getBitWidth() == 128)
862 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000863 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
864 unsigned EltAlign = 0;
865 getMaxByValAlign(ATy->getElementType(), EltAlign);
866 if (EltAlign > MaxAlign)
867 MaxAlign = EltAlign;
868 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
869 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
870 unsigned EltAlign = 0;
871 getMaxByValAlign(STy->getElementType(i), EltAlign);
872 if (EltAlign > MaxAlign)
873 MaxAlign = EltAlign;
874 if (MaxAlign == 16)
875 break;
876 }
877 }
878 return;
879}
880
881/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
882/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000883/// that contain SSE vectors are placed at 16-byte boundaries while the rest
884/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000885unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000886 if (Subtarget->is64Bit()) {
887 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000888 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000889 if (TyAlign > 8)
890 return TyAlign;
891 return 8;
892 }
893
Evan Cheng5a67b812008-01-23 23:17:41 +0000894 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000895 if (Subtarget->hasSSE1())
896 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000897 return Align;
898}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000899
Evan Cheng8c590372008-05-15 08:39:06 +0000900/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000901/// and store operations as a result of memset, memcpy, and memmove
902/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000903/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000904MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000905X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
906 bool isSrcConst, bool isSrcStr) const {
Chris Lattnerf0bf1062008-10-28 05:49:35 +0000907 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
908 // linux. This is because the stack realignment code can't handle certain
909 // cases like PR2962. This should be removed when PR2962 is fixed.
Bill Wendling042eda32009-03-11 22:30:01 +0000910 if (!NoImplicitFloat && Subtarget->getStackAlignment() >= 16) {
Chris Lattnerf0bf1062008-10-28 05:49:35 +0000911 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
912 return MVT::v4i32;
913 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
914 return MVT::v4f32;
915 }
Evan Cheng8c590372008-05-15 08:39:06 +0000916 if (Subtarget->is64Bit() && Size >= 8)
917 return MVT::i64;
918 return MVT::i32;
919}
920
Evan Cheng6fb06762007-11-09 01:32:10 +0000921/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
922/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000923SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000924 SelectionDAG &DAG) const {
925 if (usesGlobalOffsetTable())
Dale Johannesen24dd9a52009-02-07 00:55:49 +0000926 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
Evan Cheng6fb06762007-11-09 01:32:10 +0000927 if (!Subtarget->isPICStyleRIPRel())
Dale Johannesen24dd9a52009-02-07 00:55:49 +0000928 // This doesn't have DebugLoc associated with it, but is not really the
929 // same as a Register.
930 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
931 getPointerTy());
Evan Cheng6fb06762007-11-09 01:32:10 +0000932 return Table;
933}
934
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000935//===----------------------------------------------------------------------===//
936// Return Value Calling Convention Implementation
937//===----------------------------------------------------------------------===//
938
939#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000940
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000941/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000942SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +0000943 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000944 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
Scott Michel91099d62009-02-17 22:15:04 +0000945
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946 SmallVector<CCValAssign, 16> RVLocs;
947 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
948 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
949 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000950 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Scott Michel91099d62009-02-17 22:15:04 +0000951
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000952 // If this is the first return lowered for this function, add the regs to the
953 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000954 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000955 for (unsigned i = 0; i != RVLocs.size(); ++i)
956 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000957 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000958 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000959 SDValue Chain = Op.getOperand(0);
Scott Michel91099d62009-02-17 22:15:04 +0000960
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000961 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000962 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000963 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000964 SDValue TailCall = Chain;
965 SDValue TargetAddress = TailCall.getOperand(1);
966 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000967 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000968 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000969 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000970 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Scott Michel91099d62009-02-17 22:15:04 +0000971 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000972 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000973 assert(StackAdjustment.getOpcode() == ISD::Constant &&
974 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000975
Dan Gohman8181bd12008-07-27 21:46:04 +0000976 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000977 Operands.push_back(Chain.getOperand(0));
978 Operands.push_back(TargetAddress);
979 Operands.push_back(StackAdjustment);
980 // Copy registers used by the call. Last operand is a flag so it is not
981 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000982 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000983 Operands.push_back(Chain.getOperand(i));
984 }
Scott Michel91099d62009-02-17 22:15:04 +0000985 return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000986 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000987 }
Scott Michel91099d62009-02-17 22:15:04 +0000988
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000989 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000990 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000991
Dan Gohman8181bd12008-07-27 21:46:04 +0000992 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000993 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
994 // Operand #1 = Bytes To Pop
995 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michel91099d62009-02-17 22:15:04 +0000996
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000998 for (unsigned i = 0; i != RVLocs.size(); ++i) {
999 CCValAssign &VA = RVLocs[i];
1000 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001001 SDValue ValToCopy = Op.getOperand(i*2+1);
Scott Michel91099d62009-02-17 22:15:04 +00001002
Chris Lattnerb56cc342008-03-11 03:23:40 +00001003 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1004 // the RET instruction and handled by the FP Stackifier.
Dan Gohman6c4be722009-02-04 17:28:58 +00001005 if (VA.getLocReg() == X86::ST0 ||
1006 VA.getLocReg() == X86::ST1) {
Chris Lattnerb56cc342008-03-11 03:23:40 +00001007 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1008 // change the value to the FP stack register class.
Dan Gohman6c4be722009-02-04 17:28:58 +00001009 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Dale Johannesence0805b2009-02-03 19:33:06 +00001010 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattnerb56cc342008-03-11 03:23:40 +00001011 RetOps.push_back(ValToCopy);
1012 // Don't emit a copytoreg.
1013 continue;
1014 }
Dale Johannesena585daf2008-06-24 22:01:44 +00001015
Evan Chengef356282009-02-23 09:03:22 +00001016 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1017 // which is returned in RAX / RDX.
Evan Chenge8db6e02009-02-22 08:05:12 +00001018 if (Subtarget->is64Bit()) {
1019 MVT ValVT = ValToCopy.getValueType();
Evan Chengef356282009-02-23 09:03:22 +00001020 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Evan Chenge8db6e02009-02-22 08:05:12 +00001021 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Chengef356282009-02-23 09:03:22 +00001022 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
1023 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
1024 }
Evan Chenge8db6e02009-02-22 08:05:12 +00001025 }
1026
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001027 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001028 Flag = Chain.getValue(1);
1029 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001030
1031 // The x86-64 ABI for returning structs by value requires that we copy
1032 // the sret argument into %rax for the return. We saved the argument into
1033 // a virtual register in the entry block, so now we copy the value out
1034 // and into %rax.
1035 if (Subtarget->is64Bit() &&
1036 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1037 MachineFunction &MF = DAG.getMachineFunction();
1038 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1039 unsigned Reg = FuncInfo->getSRetReturnReg();
1040 if (!Reg) {
1041 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1042 FuncInfo->setSRetReturnReg(Reg);
1043 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001044 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001045
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001046 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001047 Flag = Chain.getValue(1);
1048 }
Scott Michel91099d62009-02-17 22:15:04 +00001049
Chris Lattnerb56cc342008-03-11 03:23:40 +00001050 RetOps[0] = Chain; // Update chain.
1051
1052 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +00001053 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +00001054 RetOps.push_back(Flag);
Scott Michel91099d62009-02-17 22:15:04 +00001055
1056 return DAG.getNode(X86ISD::RET_FLAG, dl,
Dale Johannesence0805b2009-02-03 19:33:06 +00001057 MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058}
1059
1060
1061/// LowerCallResult - Lower the result values of an ISD::CALL into the
1062/// appropriate copies out of appropriate physical registers. This assumes that
1063/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1064/// being lowered. The returns a SDNode with the same number of values as the
1065/// ISD::CALL.
1066SDNode *X86TargetLowering::
Scott Michel91099d62009-02-17 22:15:04 +00001067LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068 unsigned CallingConv, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00001069
Scott Michel91099d62009-02-17 22:15:04 +00001070 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001071 // Assign locations to each value returned by this call.
1072 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +00001073 bool isVarArg = TheCall->isVarArg();
Edwin Törökaf8e1332009-02-01 18:15:56 +00001074 bool Is64Bit = Subtarget->is64Bit();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
1076 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1077
Dan Gohman8181bd12008-07-27 21:46:04 +00001078 SmallVector<SDValue, 8> ResultVals;
Scott Michel91099d62009-02-17 22:15:04 +00001079
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001080 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001081 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman6c4be722009-02-04 17:28:58 +00001082 CCValAssign &VA = RVLocs[i];
1083 MVT CopyVT = VA.getValVT();
Scott Michel91099d62009-02-17 22:15:04 +00001084
Edwin Törökaf8e1332009-02-01 18:15:56 +00001085 // If this is x86-64, and we disabled SSE, we can't return FP values
Scott Michel91099d62009-02-17 22:15:04 +00001086 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Edwin Törökaf8e1332009-02-01 18:15:56 +00001087 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
1088 cerr << "SSE register return with SSE disabled\n";
1089 exit(1);
1090 }
1091
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001092 // If this is a call to a function that returns an fp value on the floating
1093 // point stack, but where we prefer to use the value in xmm registers, copy
1094 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Dan Gohman6c4be722009-02-04 17:28:58 +00001095 if ((VA.getLocReg() == X86::ST0 ||
1096 VA.getLocReg() == X86::ST1) &&
1097 isScalarFPTypeInSSEReg(VA.getValVT())) {
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001098 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001099 }
Scott Michel91099d62009-02-17 22:15:04 +00001100
Evan Cheng9cc600e2009-02-20 20:43:02 +00001101 SDValue Val;
1102 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Chengef356282009-02-23 09:03:22 +00001103 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1104 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1105 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1106 MVT::v2i64, InFlag).getValue(1);
1107 Val = Chain.getValue(0);
1108 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1109 Val, DAG.getConstant(0, MVT::i64));
1110 } else {
1111 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1112 MVT::i64, InFlag).getValue(1);
1113 Val = Chain.getValue(0);
1114 }
Evan Cheng9cc600e2009-02-20 20:43:02 +00001115 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1116 } else {
1117 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1118 CopyVT, InFlag).getValue(1);
1119 Val = Chain.getValue(0);
1120 }
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001121 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001122
Dan Gohman6c4be722009-02-04 17:28:58 +00001123 if (CopyVT != VA.getValVT()) {
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001124 // Round the F80 the right size, which also moves to the appropriate xmm
1125 // register.
Dan Gohman6c4be722009-02-04 17:28:58 +00001126 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001127 // This truncation won't change the value.
1128 DAG.getIntPtrConstant(1));
1129 }
Scott Michel91099d62009-02-17 22:15:04 +00001130
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001131 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001132 }
Duncan Sands698842f2008-07-02 17:40:58 +00001133
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001134 // Merge everything together with a MERGE_VALUES node.
1135 ResultVals.push_back(Chain);
Dale Johannesence0805b2009-02-03 19:33:06 +00001136 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
1137 &ResultVals[0], ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001138}
1139
1140
1141//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001142// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143//===----------------------------------------------------------------------===//
1144// StdCall calling convention seems to be standard for many Windows' API
1145// routines and around. It differs from C calling convention just a little:
1146// callee should clean up the stack, not caller. Symbols should be also
1147// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001148// For info on fast calling convention see Fast Calling Convention (tail call)
1149// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001150
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001151/// CallIsStructReturn - Determines whether a CALL node uses struct return
1152/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001153static bool CallIsStructReturn(CallSDNode *TheCall) {
1154 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001155 if (!NumOps)
1156 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001157
Dan Gohman705e3f72008-09-13 01:54:27 +00001158 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001159}
1160
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001161/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1162/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001163static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001164 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001165 if (!NumArgs)
1166 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001167
1168 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001169}
1170
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001171/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1172/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001173/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001174bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001175 if (IsVarArg)
1176 return false;
1177
Dan Gohman705e3f72008-09-13 01:54:27 +00001178 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001179 default:
1180 return false;
1181 case CallingConv::X86_StdCall:
1182 return !Subtarget->is64Bit();
1183 case CallingConv::X86_FastCall:
1184 return !Subtarget->is64Bit();
1185 case CallingConv::Fast:
1186 return PerformTailCallOpt;
1187 }
1188}
1189
Dan Gohman705e3f72008-09-13 01:54:27 +00001190/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1191/// given CallingConvention value.
1192CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001193 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001194 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001195 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001196 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1197 return CC_X86_64_TailCall;
1198 else
1199 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001200 }
1201
Gordon Henriksen18ace102008-01-05 16:56:59 +00001202 if (CC == CallingConv::X86_FastCall)
1203 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001204 else if (CC == CallingConv::Fast)
1205 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001206 else
1207 return CC_X86_32_C;
1208}
1209
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001210/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1211/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001212NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001213X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001214 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001215 if (CC == CallingConv::X86_FastCall)
1216 return FastCall;
1217 else if (CC == CallingConv::X86_StdCall)
1218 return StdCall;
1219 return None;
1220}
1221
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001222
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001223/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1224/// in a register before calling.
1225bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1226 return !IsTailCall && !Is64Bit &&
1227 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1228 Subtarget->isPICStyleGOT();
1229}
1230
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001231/// CallRequiresFnAddressInReg - Check whether the call requires the function
1232/// address to be loaded in a register.
Scott Michel91099d62009-02-17 22:15:04 +00001233bool
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001234X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
Scott Michel91099d62009-02-17 22:15:04 +00001235 return !Is64Bit && IsTailCall &&
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001236 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1237 Subtarget->isPICStyleGOT();
1238}
1239
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001240/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1241/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001242/// the specific parameter attribute. The copy will be passed as a byval
1243/// function parameter.
Scott Michel91099d62009-02-17 22:15:04 +00001244static SDValue
Dan Gohman8181bd12008-07-27 21:46:04 +00001245CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001246 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1247 DebugLoc dl) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001248 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001249 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001250 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001251}
1252
Dan Gohman8181bd12008-07-27 21:46:04 +00001253SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001254 const CCValAssign &VA,
1255 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001256 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001257 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001258 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001259 ISD::ArgFlagsTy Flags =
1260 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001261 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001262 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001263
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001264 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michel91099d62009-02-17 22:15:04 +00001265 // changed with more analysis.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001266 // In case of tail call optimization mark all arguments mutable. Since they
1267 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001268 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001269 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001270 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001271 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001272 return FIN;
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00001273 return DAG.getLoad(VA.getValVT(), Op.getDebugLoc(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001274 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001275}
1276
Dan Gohman8181bd12008-07-27 21:46:04 +00001277SDValue
1278X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001279 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001280 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00001281 DebugLoc dl = Op.getDebugLoc();
Scott Michel91099d62009-02-17 22:15:04 +00001282
Gordon Henriksen18ace102008-01-05 16:56:59 +00001283 const Function* Fn = MF.getFunction();
1284 if (Fn->hasExternalLinkage() &&
1285 Subtarget->isTargetCygMing() &&
1286 Fn->getName() == "main")
1287 FuncInfo->setForceFramePointer(true);
1288
1289 // Decorate the function name.
1290 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
Scott Michel91099d62009-02-17 22:15:04 +00001291
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001292 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001293 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001294 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001295 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001296 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001297 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001298
1299 assert(!(isVarArg && CC == CallingConv::Fast) &&
1300 "Var args not supported with calling convention fastcc");
1301
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302 // Assign locations to all of the incoming arguments.
1303 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001304 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001305 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Scott Michel91099d62009-02-17 22:15:04 +00001306
Dan Gohman8181bd12008-07-27 21:46:04 +00001307 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001308 unsigned LastVal = ~0U;
1309 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1310 CCValAssign &VA = ArgLocs[i];
1311 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1312 // places.
1313 assert(VA.getValNo() != LastVal &&
1314 "Don't support value assigned to multiple locs yet");
1315 LastVal = VA.getValNo();
Scott Michel91099d62009-02-17 22:15:04 +00001316
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001317 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001318 MVT RegVT = VA.getLocVT();
Devang Patelf3707e82009-01-05 17:31:22 +00001319 TargetRegisterClass *RC = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320 if (RegVT == MVT::i32)
1321 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001322 else if (Is64Bit && RegVT == MVT::i64)
1323 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001324 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001325 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001326 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001327 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001328 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001329 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001330 else if (RegVT.isVector()) {
1331 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001332 if (!Is64Bit)
1333 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1334 else {
1335 // Darwin calling convention passes MMX values in either GPRs or
1336 // XMMs in x86-64. Other targets pass them in memory.
1337 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1338 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1339 RegVT = MVT::v2i64;
1340 } else {
1341 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1342 RegVT = MVT::i64;
1343 }
1344 }
1345 } else {
1346 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001347 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001348
Bob Wilsonb6737aa2009-04-20 18:36:57 +00001349 unsigned Reg = DAG.getMachineFunction().addLiveIn(VA.getLocReg(), RC);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001350 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Scott Michel91099d62009-02-17 22:15:04 +00001351
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001352 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1353 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1354 // right size.
1355 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesence0805b2009-02-03 19:33:06 +00001356 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001357 DAG.getValueType(VA.getValVT()));
1358 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesence0805b2009-02-03 19:33:06 +00001359 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001360 DAG.getValueType(VA.getValVT()));
Scott Michel91099d62009-02-17 22:15:04 +00001361
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001362 if (VA.getLocInfo() != CCValAssign::Full)
Dale Johannesence0805b2009-02-03 19:33:06 +00001363 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Scott Michel91099d62009-02-17 22:15:04 +00001364
Gordon Henriksen18ace102008-01-05 16:56:59 +00001365 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001366 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001367 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Dale Johannesence0805b2009-02-03 19:33:06 +00001368 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Chengad6980b2008-04-25 20:13:28 +00001369 else if (RC == X86::VR128RegisterClass) {
Dale Johannesence0805b2009-02-03 19:33:06 +00001370 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1371 ArgValue, DAG.getConstant(0, MVT::i64));
1372 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Chengad6980b2008-04-25 20:13:28 +00001373 }
1374 }
Scott Michel91099d62009-02-17 22:15:04 +00001375
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001376 ArgValues.push_back(ArgValue);
1377 } else {
1378 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001379 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001380 }
1381 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001382
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001383 // The x86-64 ABI for returning structs by value requires that we copy
1384 // the sret argument into %rax for the return. Save the argument into
1385 // a virtual register so that we can access it from the return points.
1386 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1387 MachineFunction &MF = DAG.getMachineFunction();
1388 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1389 unsigned Reg = FuncInfo->getSRetReturnReg();
1390 if (!Reg) {
1391 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1392 FuncInfo->setSRetReturnReg(Reg);
1393 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001394 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
Dale Johannesence0805b2009-02-03 19:33:06 +00001395 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001396 }
1397
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001398 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001399 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001400 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001401 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001402
1403 // If the function takes variable number of arguments, make a frame index for
1404 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001405 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001406 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1407 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1408 }
1409 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001410 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1411
1412 // FIXME: We should really autogenerate these arrays
1413 static const unsigned GPR64ArgRegsWin64[] = {
1414 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001415 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001416 static const unsigned XMMArgRegsWin64[] = {
1417 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1418 };
1419 static const unsigned GPR64ArgRegs64Bit[] = {
1420 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1421 };
1422 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001423 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1424 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1425 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001426 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1427
1428 if (IsWin64) {
1429 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1430 GPR64ArgRegs = GPR64ArgRegsWin64;
1431 XMMArgRegs = XMMArgRegsWin64;
1432 } else {
1433 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1434 GPR64ArgRegs = GPR64ArgRegs64Bit;
1435 XMMArgRegs = XMMArgRegs64Bit;
1436 }
1437 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1438 TotalNumIntRegs);
1439 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1440 TotalNumXMMRegs);
1441
Evan Cheng0b84fe12009-02-13 22:36:38 +00001442 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Edwin Törökaf8e1332009-02-01 18:15:56 +00001443 "SSE register cannot be used when SSE is disabled!");
Bill Wendling042eda32009-03-11 22:30:01 +00001444 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloat) &&
Evan Cheng0b84fe12009-02-13 22:36:38 +00001445 "SSE register cannot be used when SSE is disabled!");
Bill Wendling042eda32009-03-11 22:30:01 +00001446 if (UseSoftFloat || NoImplicitFloat || !Subtarget->hasSSE1())
Edwin Törökaf8e1332009-02-01 18:15:56 +00001447 // Kernel mode asks for SSE to be disabled, so don't push them
1448 // on the stack.
1449 TotalNumXMMRegs = 0;
Bill Wendling042eda32009-03-11 22:30:01 +00001450
Gordon Henriksen18ace102008-01-05 16:56:59 +00001451 // For X86-64, if there are vararg parameters that are passed via
1452 // registers, then we must store them to their spots on the stack so they
1453 // may be loaded by deferencing the result of va_next.
1454 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001455 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1456 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1457 TotalNumXMMRegs * 16, 16);
1458
Gordon Henriksen18ace102008-01-05 16:56:59 +00001459 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001460 SmallVector<SDValue, 8> MemOps;
1461 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00001462 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001463 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001464 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Bob Wilsonb6737aa2009-04-20 18:36:57 +00001465 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1466 X86::GR64RegisterClass);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001467 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64);
Dan Gohman8181bd12008-07-27 21:46:04 +00001468 SDValue Store =
Dale Johannesence0805b2009-02-03 19:33:06 +00001469 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001470 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001471 MemOps.push_back(Store);
Dale Johannesence0805b2009-02-03 19:33:06 +00001472 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001473 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001474 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001475
Gordon Henriksen18ace102008-01-05 16:56:59 +00001476 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesence0805b2009-02-03 19:33:06 +00001477 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001478 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001479 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Bob Wilsonb6737aa2009-04-20 18:36:57 +00001480 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1481 X86::VR128RegisterClass);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001482 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::v4f32);
Dan Gohman8181bd12008-07-27 21:46:04 +00001483 SDValue Store =
Dale Johannesence0805b2009-02-03 19:33:06 +00001484 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001485 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001486 MemOps.push_back(Store);
Dale Johannesence0805b2009-02-03 19:33:06 +00001487 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001488 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001489 }
1490 if (!MemOps.empty())
Dale Johannesence0805b2009-02-03 19:33:06 +00001491 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Gordon Henriksen18ace102008-01-05 16:56:59 +00001492 &MemOps[0], MemOps.size());
1493 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001494 }
Scott Michel91099d62009-02-17 22:15:04 +00001495
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001496 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001497
Gordon Henriksen18ace102008-01-05 16:56:59 +00001498 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001499 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001500 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501 BytesCallerReserves = 0;
1502 } else {
1503 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001505 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Scott Michel91099d62009-02-17 22:15:04 +00001506 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001507 BytesCallerReserves = StackSize;
1508 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001509
Gordon Henriksen18ace102008-01-05 16:56:59 +00001510 if (!Is64Bit) {
1511 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1512 if (CC == CallingConv::X86_FastCall)
1513 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1514 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001515
Anton Korobeynikove844e472007-08-15 17:12:32 +00001516 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001517
1518 // Return the new list of results.
Dale Johannesence0805b2009-02-03 19:33:06 +00001519 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sands42d7bb82008-12-01 11:41:29 +00001520 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001521}
1522
Dan Gohman8181bd12008-07-27 21:46:04 +00001523SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001524X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001525 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001526 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001527 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001528 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dale Johannesence0805b2009-02-03 19:33:06 +00001529 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohman1190f3a2008-02-07 16:28:05 +00001530 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001531 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesence0805b2009-02-03 19:33:06 +00001532 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001533 if (Flags.isByVal()) {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001534 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengbc077bf2008-01-10 00:09:10 +00001535 }
Dale Johannesence0805b2009-02-03 19:33:06 +00001536 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001537 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001538}
1539
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001540/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001541/// optimization is performed and it is required.
Scott Michel91099d62009-02-17 22:15:04 +00001542SDValue
1543X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001544 SDValue &OutRetAddr,
Scott Michel91099d62009-02-17 22:15:04 +00001545 SDValue Chain,
1546 bool IsTailCall,
1547 bool Is64Bit,
Dale Johannesence0805b2009-02-03 19:33:06 +00001548 int FPDiff,
1549 DebugLoc dl) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001550 if (!IsTailCall || FPDiff==0) return Chain;
1551
1552 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001553 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001554 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001555
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001556 // Load the "old" Return address.
Dale Johannesence0805b2009-02-03 19:33:06 +00001557 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001558 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001559}
1560
1561/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1562/// optimization is performed and it is required (FPDiff!=0).
Scott Michel91099d62009-02-17 22:15:04 +00001563static SDValue
1564EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001565 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesence0805b2009-02-03 19:33:06 +00001566 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001567 // Store the return address to the appropriate stack slot.
1568 if (!FPDiff) return Chain;
1569 // Calculate the new stack slot for the return address.
1570 int SlotSize = Is64Bit ? 8 : 4;
Scott Michel91099d62009-02-17 22:15:04 +00001571 int NewReturnAddrFI =
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001572 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001573 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001574 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001575 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001576 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001577 return Chain;
1578}
1579
Dan Gohman8181bd12008-07-27 21:46:04 +00001580SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001581 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001582 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1583 SDValue Chain = TheCall->getChain();
1584 unsigned CC = TheCall->getCallingConv();
1585 bool isVarArg = TheCall->isVarArg();
1586 bool IsTailCall = TheCall->isTailCall() &&
1587 CC == CallingConv::Fast && PerformTailCallOpt;
1588 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001589 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001590 bool IsStructRet = CallIsStructReturn(TheCall);
Dale Johannesence0805b2009-02-03 19:33:06 +00001591 DebugLoc dl = TheCall->getDebugLoc();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001592
1593 assert(!(isVarArg && CC == CallingConv::Fast) &&
1594 "Var args not supported with calling convention fastcc");
1595
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001596 // Analyze operands of the call, assigning locations to each operand.
1597 SmallVector<CCValAssign, 16> ArgLocs;
1598 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001599 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Scott Michel91099d62009-02-17 22:15:04 +00001600
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001601 // Get a count of how many bytes are to be pushed on the stack.
1602 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001603 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001604 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001605
Gordon Henriksen18ace102008-01-05 16:56:59 +00001606 int FPDiff = 0;
1607 if (IsTailCall) {
1608 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michel91099d62009-02-17 22:15:04 +00001609 unsigned NumBytesCallerPushed =
Gordon Henriksen18ace102008-01-05 16:56:59 +00001610 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1611 FPDiff = NumBytesCallerPushed - NumBytes;
1612
1613 // Set the delta of movement of the returnaddr stackslot.
1614 // But only set if delta is greater than previous delta.
1615 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1616 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1617 }
1618
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001619 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001620
Dan Gohman8181bd12008-07-27 21:46:04 +00001621 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001622 // Load return adress for tail calls.
1623 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
Dale Johannesence0805b2009-02-03 19:33:06 +00001624 FPDiff, dl);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001625
Dan Gohman8181bd12008-07-27 21:46:04 +00001626 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1627 SmallVector<SDValue, 8> MemOpChains;
1628 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001629
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001630 // Walk the register/memloc assignments, inserting copies/loads. In the case
1631 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001632 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1633 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001634 SDValue Arg = TheCall->getArg(i);
1635 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1636 bool isByVal = Flags.isByVal();
Scott Michel91099d62009-02-17 22:15:04 +00001637
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001638 // Promote the value if needed.
1639 switch (VA.getLocInfo()) {
1640 default: assert(0 && "Unknown loc info!");
1641 case CCValAssign::Full: break;
1642 case CCValAssign::SExt:
Dale Johannesence0805b2009-02-03 19:33:06 +00001643 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001644 break;
1645 case CCValAssign::ZExt:
Dale Johannesence0805b2009-02-03 19:33:06 +00001646 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001647 break;
1648 case CCValAssign::AExt:
Dale Johannesence0805b2009-02-03 19:33:06 +00001649 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001650 break;
1651 }
Scott Michel91099d62009-02-17 22:15:04 +00001652
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001653 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001654 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001655 MVT RegVT = VA.getLocVT();
1656 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001657 switch (VA.getLocReg()) {
1658 default:
1659 break;
1660 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1661 case X86::R8: {
1662 // Special case: passing MMX values in GPR registers.
Dale Johannesence0805b2009-02-03 19:33:06 +00001663 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
Evan Cheng2aea0b42008-04-25 19:11:04 +00001664 break;
1665 }
1666 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1667 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1668 // Special case: passing MMX values in XMM registers.
Dale Johannesence0805b2009-02-03 19:33:06 +00001669 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1670 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
Nate Begeman543d2142009-04-27 18:41:29 +00001671 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Evan Cheng2aea0b42008-04-25 19:11:04 +00001672 break;
1673 }
1674 }
1675 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001676 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1677 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001678 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001679 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001680 if (StackPtr.getNode() == 0)
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001681 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michel91099d62009-02-17 22:15:04 +00001682
Dan Gohman705e3f72008-09-13 01:54:27 +00001683 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1684 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001685 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001686 }
1687 }
Scott Michel91099d62009-02-17 22:15:04 +00001688
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001689 if (!MemOpChains.empty())
Dale Johannesence0805b2009-02-03 19:33:06 +00001690 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001691 &MemOpChains[0], MemOpChains.size());
1692
1693 // Build a sequence of copy-to-reg nodes chained together with token chain
1694 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001695 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001696 // Tail call byval lowering might overwrite argument registers so in case of
1697 // tail call optimization the copies to registers are lowered later.
1698 if (!IsTailCall)
1699 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michel91099d62009-02-17 22:15:04 +00001700 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001701 RegsToPass[i].second, InFlag);
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001702 InFlag = Chain.getValue(1);
1703 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001704
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001705 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Scott Michel91099d62009-02-17 22:15:04 +00001706 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001707 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001708 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
Scott Michel91099d62009-02-17 22:15:04 +00001709 DAG.getNode(X86ISD::GlobalBaseReg,
1710 DebugLoc::getUnknownLoc(),
Dale Johannesen24dd9a52009-02-07 00:55:49 +00001711 getPointerTy()),
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001712 InFlag);
1713 InFlag = Chain.getValue(1);
1714 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001715 // If we are tail calling and generating PIC/GOT style code load the address
1716 // of the callee into ecx. The value in ecx is used as target of the tail
1717 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1718 // calls on PIC/GOT architectures. Normally we would just put the address of
1719 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1720 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001721 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001722 // Note: The actual moving to ecx is done further down.
1723 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001724 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001725 !G->getGlobal()->hasProtectedVisibility())
1726 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001727 else if (isa<ExternalSymbolSDNode>(Callee))
1728 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001729 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001730
Gordon Henriksen18ace102008-01-05 16:56:59 +00001731 if (Is64Bit && isVarArg) {
1732 // From AMD64 ABI document:
1733 // For calls that may call functions that use varargs or stdargs
1734 // (prototype-less calls or calls to functions containing ellipsis (...) in
1735 // the declaration) %al is used as hidden argument to specify the number
1736 // of SSE registers used. The contents of %al do not need to match exactly
1737 // the number of registers, but must be an ubound on the number of SSE
1738 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001739
1740 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001741 // Count the number of XMM registers allocated.
1742 static const unsigned XMMArgRegs[] = {
1743 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1744 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1745 };
1746 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michel91099d62009-02-17 22:15:04 +00001747 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Edwin Törökaf8e1332009-02-01 18:15:56 +00001748 && "SSE registers cannot be used when SSE is disabled");
Scott Michel91099d62009-02-17 22:15:04 +00001749
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001750 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Gordon Henriksen18ace102008-01-05 16:56:59 +00001751 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1752 InFlag = Chain.getValue(1);
1753 }
1754
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001755
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001756 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001757 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001758 SmallVector<SDValue, 8> MemOpChains2;
1759 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001760 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001761 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001762 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001763 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1764 CCValAssign &VA = ArgLocs[i];
1765 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001766 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001767 SDValue Arg = TheCall->getArg(i);
1768 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001769 // Create frame index.
1770 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001771 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001772 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001773 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001774
Duncan Sandsc93fae32008-03-21 09:14:45 +00001775 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001776 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001777 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001778 if (StackPtr.getNode() == 0)
Scott Michel91099d62009-02-17 22:15:04 +00001779 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001780 getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00001781 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001782
1783 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001784 Flags, DAG, dl));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001785 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001786 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001787 MemOpChains2.push_back(
Dale Johannesence0805b2009-02-03 19:33:06 +00001788 DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001789 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michel91099d62009-02-17 22:15:04 +00001790 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001791 }
1792 }
1793
1794 if (!MemOpChains2.empty())
Dale Johannesence0805b2009-02-03 19:33:06 +00001795 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001796 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001797
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001798 // Copy arguments to their registers.
1799 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michel91099d62009-02-17 22:15:04 +00001800 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001801 RegsToPass[i].second, InFlag);
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001802 InFlag = Chain.getValue(1);
1803 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001804 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001805
Gordon Henriksen18ace102008-01-05 16:56:59 +00001806 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001807 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesence0805b2009-02-03 19:33:06 +00001808 FPDiff, dl);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001809 }
1810
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001811 // If the callee is a GlobalAddress node (quite common, every direct call is)
1812 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1813 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1814 // We should use extra load for direct calls to dllimported functions in
1815 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001816 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1817 getTargetMachine(), true))
Dan Gohman36322c72008-10-18 02:06:02 +00001818 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1819 G->getOffset());
Bill Wendlingfef06052008-09-16 21:48:12 +00001820 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1821 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001822 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001823 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001824
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001825 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michel91099d62009-02-17 22:15:04 +00001826 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001827 Callee,InFlag);
1828 Callee = DAG.getRegister(Opc, getPointerTy());
1829 // Add register as live out.
1830 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001831 }
Scott Michel91099d62009-02-17 22:15:04 +00001832
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001833 // Returns a chain & a flag for retval copy to use.
1834 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001835 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001836
1837 if (IsTailCall) {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001838 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1839 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001840 InFlag = Chain.getValue(1);
Scott Michel91099d62009-02-17 22:15:04 +00001841
Gordon Henriksen18ace102008-01-05 16:56:59 +00001842 // Returns a chain & a flag for retval copy to use.
1843 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1844 Ops.clear();
1845 }
Scott Michel91099d62009-02-17 22:15:04 +00001846
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001847 Ops.push_back(Chain);
1848 Ops.push_back(Callee);
1849
Gordon Henriksen18ace102008-01-05 16:56:59 +00001850 if (IsTailCall)
1851 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001852
Gordon Henriksen18ace102008-01-05 16:56:59 +00001853 // Add argument registers to the end of the list so that they are known live
1854 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001855 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1856 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1857 RegsToPass[i].second.getValueType()));
Scott Michel91099d62009-02-17 22:15:04 +00001858
Evan Cheng8ba45e62008-03-18 23:36:35 +00001859 // Add an implicit use GOT pointer in EBX.
1860 if (!IsTailCall && !Is64Bit &&
1861 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1862 Subtarget->isPICStyleGOT())
1863 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1864
1865 // Add an implicit use of AL for x86 vararg functions.
1866 if (Is64Bit && isVarArg)
1867 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1868
Gabor Greif1c80d112008-08-28 21:40:38 +00001869 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001870 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001871
Gordon Henriksen18ace102008-01-05 16:56:59 +00001872 if (IsTailCall) {
Scott Michel91099d62009-02-17 22:15:04 +00001873 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001874 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesence0805b2009-02-03 19:33:06 +00001875 Chain = DAG.getNode(X86ISD::TAILCALL, dl,
Dan Gohman705e3f72008-09-13 01:54:27 +00001876 TheCall->getVTList(), &Ops[0], Ops.size());
Scott Michel91099d62009-02-17 22:15:04 +00001877
Gabor Greif1c80d112008-08-28 21:40:38 +00001878 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001879 }
1880
Dale Johannesence0805b2009-02-03 19:33:06 +00001881 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001882 InFlag = Chain.getValue(1);
1883
1884 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001885 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001886 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001887 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001888 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001889 // If this is is a call to a struct-return function, the callee
1890 // pops the hidden struct pointer, so we have to push it back.
1891 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001892 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001893 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001894 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michel91099d62009-02-17 22:15:04 +00001895
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001896 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001897 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001898 DAG.getIntPtrConstant(NumBytes, true),
1899 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1900 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001901 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001902 InFlag = Chain.getValue(1);
1903
1904 // Handle result values, copying them out of physregs into vregs that we
1905 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001906 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001907 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001908}
1909
1910
1911//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001912// Fast Calling Convention (tail call) implementation
1913//===----------------------------------------------------------------------===//
1914
1915// Like std call, callee cleans arguments, convention except that ECX is
1916// reserved for storing the tail called function address. Only 2 registers are
1917// free for argument passing (inreg). Tail call optimization is performed
1918// provided:
1919// * tailcallopt is enabled
1920// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001921// On X86_64 architecture with GOT-style position independent code only local
1922// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001923// To keep the stack aligned according to platform abi the function
1924// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1925// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001926// If a tail called function callee has more arguments than the caller the
1927// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001928// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001929// original REtADDR, but before the saved framepointer or the spilled registers
1930// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1931// stack layout:
1932// arg1
1933// arg2
1934// RETADDR
Scott Michel91099d62009-02-17 22:15:04 +00001935// [ new RETADDR
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001936// move area ]
1937// (possible EBP)
1938// ESI
1939// EDI
1940// local1 ..
1941
1942/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1943/// for a 16 byte align requirement.
Scott Michel91099d62009-02-17 22:15:04 +00001944unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001945 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001946 MachineFunction &MF = DAG.getMachineFunction();
1947 const TargetMachine &TM = MF.getTarget();
1948 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1949 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michel91099d62009-02-17 22:15:04 +00001950 uint64_t AlignMask = StackAlignment - 1;
Evan Chengded8f902008-09-07 09:07:23 +00001951 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001952 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001953 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1954 // Number smaller than 12 so just add the difference.
1955 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1956 } else {
1957 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michel91099d62009-02-17 22:15:04 +00001958 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chengded8f902008-09-07 09:07:23 +00001959 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001960 }
Evan Chengded8f902008-09-07 09:07:23 +00001961 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001962}
1963
1964/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001965/// following the call is a return. A function is eligible if caller/callee
1966/// calling conventions match, currently only fastcc supports tail calls, and
1967/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001968bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001969 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001970 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001971 if (!PerformTailCallOpt)
1972 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001973
Dan Gohman705e3f72008-09-13 01:54:27 +00001974 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001975 MachineFunction &MF = DAG.getMachineFunction();
1976 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001977 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001978 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001979 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001980 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001981 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001982 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001983 return true;
1984
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001985 // Can only do local tail calls (in same module, hidden or protected) on
1986 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001987 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1988 return G->getGlobal()->hasHiddenVisibility()
1989 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001990 }
1991 }
Evan Chenge7a87392007-11-02 01:26:22 +00001992
1993 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001994}
1995
Dan Gohmanca4857a2008-09-03 23:12:08 +00001996FastISel *
1997X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001998 MachineModuleInfo *mmo,
Devang Patelfcf1c752009-01-13 00:35:13 +00001999 DwarfWriter *dw,
Dan Gohmanca4857a2008-09-03 23:12:08 +00002000 DenseMap<const Value *, unsigned> &vm,
2001 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00002002 MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00002003 DenseMap<const AllocaInst *, int> &am
2004#ifndef NDEBUG
2005 , SmallSet<Instruction*, 8> &cil
2006#endif
2007 ) {
Devang Patelfcf1c752009-01-13 00:35:13 +00002008 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohman9dd43582008-10-14 23:54:11 +00002009#ifndef NDEBUG
2010 , cil
2011#endif
2012 );
Dan Gohman97805ee2008-08-19 21:32:53 +00002013}
2014
2015
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002016//===----------------------------------------------------------------------===//
2017// Other Lowering Hooks
2018//===----------------------------------------------------------------------===//
2019
2020
Dan Gohman8181bd12008-07-27 21:46:04 +00002021SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00002022 MachineFunction &MF = DAG.getMachineFunction();
2023 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2024 int ReturnAddrIndex = FuncInfo->getRAIndex();
2025
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002026 if (ReturnAddrIndex == 0) {
2027 // Set up a frame object for the return address.
Bill Wendling6ddc87b2009-01-16 19:25:27 +00002028 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00002029 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00002030 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002031 }
2032
2033 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
2034}
2035
2036
Chris Lattnerebb91142008-12-24 23:53:05 +00002037/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2038/// specific condition code, returning the condition code and the LHS/RHS of the
2039/// comparison to make.
2040static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2041 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002042 if (!isFP) {
2043 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2044 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2045 // X > -1 -> X == 0, jump !sign.
2046 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00002047 return X86::COND_NS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002048 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2049 // X < 0 -> X == 0, jump on sign.
Chris Lattnerebb91142008-12-24 23:53:05 +00002050 return X86::COND_S;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002051 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00002052 // X < 1 -> X <= 0
2053 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00002054 return X86::COND_LE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002055 }
2056 }
2057
2058 switch (SetCCOpcode) {
Chris Lattnerb8397512008-12-23 23:42:27 +00002059 default: assert(0 && "Invalid integer condition!");
Chris Lattnerebb91142008-12-24 23:53:05 +00002060 case ISD::SETEQ: return X86::COND_E;
2061 case ISD::SETGT: return X86::COND_G;
2062 case ISD::SETGE: return X86::COND_GE;
2063 case ISD::SETLT: return X86::COND_L;
2064 case ISD::SETLE: return X86::COND_LE;
2065 case ISD::SETNE: return X86::COND_NE;
2066 case ISD::SETULT: return X86::COND_B;
2067 case ISD::SETUGT: return X86::COND_A;
2068 case ISD::SETULE: return X86::COND_BE;
2069 case ISD::SETUGE: return X86::COND_AE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002070 }
Chris Lattnerb8397512008-12-23 23:42:27 +00002071 }
Scott Michel91099d62009-02-17 22:15:04 +00002072
Chris Lattnerb8397512008-12-23 23:42:27 +00002073 // First determine if it is required or is profitable to flip the operands.
Duncan Sandsc2a04622008-10-24 13:03:10 +00002074
Chris Lattnerb8397512008-12-23 23:42:27 +00002075 // If LHS is a foldable load, but RHS is not, flip the condition.
2076 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2077 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2078 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2079 std::swap(LHS, RHS);
Evan Chengfc937c92008-08-28 23:48:31 +00002080 }
2081
Chris Lattnerb8397512008-12-23 23:42:27 +00002082 switch (SetCCOpcode) {
2083 default: break;
2084 case ISD::SETOLT:
2085 case ISD::SETOLE:
2086 case ISD::SETUGT:
2087 case ISD::SETUGE:
2088 std::swap(LHS, RHS);
2089 break;
2090 }
2091
2092 // On a floating point condition, the flags are set as follows:
2093 // ZF PF CF op
2094 // 0 | 0 | 0 | X > Y
2095 // 0 | 0 | 1 | X < Y
2096 // 1 | 0 | 0 | X == Y
2097 // 1 | 1 | 1 | unordered
2098 switch (SetCCOpcode) {
Chris Lattnerebb91142008-12-24 23:53:05 +00002099 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattnerb8397512008-12-23 23:42:27 +00002100 case ISD::SETUEQ:
Chris Lattnerebb91142008-12-24 23:53:05 +00002101 case ISD::SETEQ: return X86::COND_E;
Chris Lattnerb8397512008-12-23 23:42:27 +00002102 case ISD::SETOLT: // flipped
2103 case ISD::SETOGT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002104 case ISD::SETGT: return X86::COND_A;
Chris Lattnerb8397512008-12-23 23:42:27 +00002105 case ISD::SETOLE: // flipped
2106 case ISD::SETOGE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002107 case ISD::SETGE: return X86::COND_AE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002108 case ISD::SETUGT: // flipped
2109 case ISD::SETULT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002110 case ISD::SETLT: return X86::COND_B;
Chris Lattnerb8397512008-12-23 23:42:27 +00002111 case ISD::SETUGE: // flipped
2112 case ISD::SETULE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002113 case ISD::SETLE: return X86::COND_BE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002114 case ISD::SETONE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002115 case ISD::SETNE: return X86::COND_NE;
2116 case ISD::SETUO: return X86::COND_P;
2117 case ISD::SETO: return X86::COND_NP;
Chris Lattnerb8397512008-12-23 23:42:27 +00002118 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002119}
2120
2121/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2122/// code. Current x86 isa includes the following FP cmov instructions:
2123/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2124static bool hasFPCMov(unsigned X86CC) {
2125 switch (X86CC) {
2126 default:
2127 return false;
2128 case X86::COND_B:
2129 case X86::COND_BE:
2130 case X86::COND_E:
2131 case X86::COND_P:
2132 case X86::COND_A:
2133 case X86::COND_AE:
2134 case X86::COND_NE:
2135 case X86::COND_NP:
2136 return true;
2137 }
2138}
2139
Nate Begeman543d2142009-04-27 18:41:29 +00002140/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2141/// the specified range (L, H].
2142static bool isUndefOrInRange(int Val, int Low, int Hi) {
2143 return (Val < 0) || (Val >= Low && Val < Hi);
2144}
2145
2146/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2147/// specified value.
2148static bool isUndefOrEqual(int Val, int CmpVal) {
2149 if (Val < 0 || Val == CmpVal)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002150 return true;
Nate Begeman543d2142009-04-27 18:41:29 +00002151 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002152}
2153
Nate Begeman543d2142009-04-27 18:41:29 +00002154/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2155/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2156/// the second operand.
Nate Begemane8f61cb2009-04-29 05:20:52 +00002157static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002158 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
2159 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
2160 if (VT == MVT::v2f64 || VT == MVT::v2i64)
2161 return (Mask[0] < 2 && Mask[1] < 2);
2162 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002163}
2164
Nate Begeman543d2142009-04-27 18:41:29 +00002165bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
2166 SmallVector<int, 8> M;
2167 N->getMask(M);
2168 return ::isPSHUFDMask(M, N->getValueType(0));
2169}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002170
Nate Begeman543d2142009-04-27 18:41:29 +00002171/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2172/// is suitable for input to PSHUFHW.
Nate Begemane8f61cb2009-04-29 05:20:52 +00002173static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002174 if (VT != MVT::v8i16)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002175 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002176
2177 // Lower quadword copied in order or undef.
2178 for (int i = 0; i != 4; ++i)
2179 if (Mask[i] >= 0 && Mask[i] != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002180 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002181
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002182 // Upper quadword shuffled.
Nate Begeman543d2142009-04-27 18:41:29 +00002183 for (int i = 4; i != 8; ++i)
2184 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002185 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002186
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002187 return true;
2188}
2189
Nate Begeman543d2142009-04-27 18:41:29 +00002190bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
2191 SmallVector<int, 8> M;
2192 N->getMask(M);
2193 return ::isPSHUFHWMask(M, N->getValueType(0));
2194}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002195
Nate Begeman543d2142009-04-27 18:41:29 +00002196/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2197/// is suitable for input to PSHUFLW.
Nate Begemane8f61cb2009-04-29 05:20:52 +00002198static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002199 if (VT != MVT::v8i16)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002200 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002201
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002202 // Upper quadword copied in order.
Nate Begeman543d2142009-04-27 18:41:29 +00002203 for (int i = 4; i != 8; ++i)
2204 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002205 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002206
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002207 // Lower quadword shuffled.
Nate Begeman543d2142009-04-27 18:41:29 +00002208 for (int i = 0; i != 4; ++i)
2209 if (Mask[i] >= 4)
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002210 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002211
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002212 return true;
Nate Begemanda17a812009-04-24 03:42:54 +00002213}
2214
Nate Begeman543d2142009-04-27 18:41:29 +00002215bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
2216 SmallVector<int, 8> M;
2217 N->getMask(M);
2218 return ::isPSHUFLWMask(M, N->getValueType(0));
2219}
2220
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002221/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2222/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Nate Begemane8f61cb2009-04-29 05:20:52 +00002223static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002224 int NumElems = VT.getVectorNumElements();
2225 if (NumElems != 2 && NumElems != 4)
2226 return false;
2227
2228 int Half = NumElems / 2;
2229 for (int i = 0; i < Half; ++i)
2230 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002231 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002232 for (int i = Half; i < NumElems; ++i)
2233 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002234 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002235
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002236 return true;
2237}
2238
Nate Begeman543d2142009-04-27 18:41:29 +00002239bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2240 SmallVector<int, 8> M;
2241 N->getMask(M);
2242 return ::isSHUFPMask(M, N->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002243}
2244
2245/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2246/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2247/// half elements to come from vector 1 (which would equal the dest.) and
2248/// the upper half to come from vector 2.
Nate Begemane8f61cb2009-04-29 05:20:52 +00002249static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002250 int NumElems = VT.getVectorNumElements();
2251
2252 if (NumElems != 2 && NumElems != 4)
2253 return false;
2254
2255 int Half = NumElems / 2;
2256 for (int i = 0; i < Half; ++i)
2257 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002258 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002259 for (int i = Half; i < NumElems; ++i)
2260 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002261 return false;
2262 return true;
2263}
2264
Nate Begeman543d2142009-04-27 18:41:29 +00002265static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2266 SmallVector<int, 8> M;
2267 N->getMask(M);
2268 return isCommutedSHUFPMask(M, N->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002269}
2270
2271/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2272/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman543d2142009-04-27 18:41:29 +00002273bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2274 if (N->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002275 return false;
2276
2277 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman543d2142009-04-27 18:41:29 +00002278 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2279 isUndefOrEqual(N->getMaskElt(1), 7) &&
2280 isUndefOrEqual(N->getMaskElt(2), 2) &&
2281 isUndefOrEqual(N->getMaskElt(3), 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002282}
2283
2284/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2285/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman543d2142009-04-27 18:41:29 +00002286bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2287 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002288
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002289 if (NumElems != 2 && NumElems != 4)
2290 return false;
2291
2292 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002293 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002294 return false;
2295
2296 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002297 if (!isUndefOrEqual(N->getMaskElt(i), i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002298 return false;
2299
2300 return true;
2301}
2302
2303/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2304/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2305/// and MOVLHPS.
Nate Begeman543d2142009-04-27 18:41:29 +00002306bool X86::isMOVHPMask(ShuffleVectorSDNode *N) {
2307 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002308
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002309 if (NumElems != 2 && NumElems != 4)
2310 return false;
2311
2312 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002313 if (!isUndefOrEqual(N->getMaskElt(i), i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002314 return false;
2315
Nate Begeman543d2142009-04-27 18:41:29 +00002316 for (unsigned i = 0; i < NumElems/2; ++i)
2317 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002318 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002319
2320 return true;
2321}
2322
Nate Begeman543d2142009-04-27 18:41:29 +00002323/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2324/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2325/// <2, 3, 2, 3>
2326bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2327 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2328
2329 if (NumElems != 4)
2330 return false;
2331
2332 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2333 isUndefOrEqual(N->getMaskElt(1), 3) &&
2334 isUndefOrEqual(N->getMaskElt(2), 2) &&
2335 isUndefOrEqual(N->getMaskElt(3), 3);
2336}
2337
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002338/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2339/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Nate Begemane8f61cb2009-04-29 05:20:52 +00002340static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002341 bool V2IsSplat = false) {
Nate Begeman543d2142009-04-27 18:41:29 +00002342 int NumElts = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002343 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2344 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002345
2346 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2347 int BitI = Mask[i];
2348 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002349 if (!isUndefOrEqual(BitI, j))
2350 return false;
2351 if (V2IsSplat) {
Mon P Wang56d91642009-02-04 01:16:59 +00002352 if (!isUndefOrEqual(BitI1, NumElts))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002353 return false;
2354 } else {
2355 if (!isUndefOrEqual(BitI1, j + NumElts))
2356 return false;
2357 }
2358 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002359 return true;
2360}
2361
Nate Begeman543d2142009-04-27 18:41:29 +00002362bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2363 SmallVector<int, 8> M;
2364 N->getMask(M);
2365 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002366}
2367
2368/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2369/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Nate Begemane8f61cb2009-04-29 05:20:52 +00002370static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002371 bool V2IsSplat = false) {
Nate Begeman543d2142009-04-27 18:41:29 +00002372 int NumElts = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002373 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2374 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002375
2376 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2377 int BitI = Mask[i];
2378 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002379 if (!isUndefOrEqual(BitI, j + NumElts/2))
2380 return false;
2381 if (V2IsSplat) {
2382 if (isUndefOrEqual(BitI1, NumElts))
2383 return false;
2384 } else {
2385 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2386 return false;
2387 }
2388 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002389 return true;
2390}
2391
Nate Begeman543d2142009-04-27 18:41:29 +00002392bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2393 SmallVector<int, 8> M;
2394 N->getMask(M);
2395 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002396}
2397
2398/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2399/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2400/// <0, 0, 1, 1>
Nate Begemane8f61cb2009-04-29 05:20:52 +00002401static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002402 int NumElems = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002403 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2404 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002405
2406 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2407 int BitI = Mask[i];
2408 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002409 if (!isUndefOrEqual(BitI, j))
2410 return false;
2411 if (!isUndefOrEqual(BitI1, j))
2412 return false;
2413 }
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002414 return true;
Nate Begemanda17a812009-04-24 03:42:54 +00002415}
2416
Nate Begeman543d2142009-04-27 18:41:29 +00002417bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2418 SmallVector<int, 8> M;
2419 N->getMask(M);
2420 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2421}
2422
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002423/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2424/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2425/// <2, 2, 3, 3>
Nate Begemane8f61cb2009-04-29 05:20:52 +00002426static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002427 int NumElems = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002428 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2429 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002430
2431 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2432 int BitI = Mask[i];
2433 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002434 if (!isUndefOrEqual(BitI, j))
2435 return false;
2436 if (!isUndefOrEqual(BitI1, j))
2437 return false;
2438 }
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002439 return true;
Nate Begemanda17a812009-04-24 03:42:54 +00002440}
2441
Nate Begeman543d2142009-04-27 18:41:29 +00002442bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2443 SmallVector<int, 8> M;
2444 N->getMask(M);
2445 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2446}
2447
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002448/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2449/// specifies a shuffle of elements that is suitable for input to MOVSS,
2450/// MOVSD, and MOVD, i.e. setting the lowest element.
Nate Begemane8f61cb2009-04-29 05:20:52 +00002451static bool isMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002452 int NumElts = VT.getVectorNumElements();
Evan Cheng62cdc642007-12-06 22:14:22 +00002453 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002454 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002455
2456 if (!isUndefOrEqual(Mask[0], NumElts))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002457 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002458
2459 for (int i = 1; i < NumElts; ++i)
2460 if (!isUndefOrEqual(Mask[i], i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002461 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002462
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002463 return true;
2464}
2465
Nate Begeman543d2142009-04-27 18:41:29 +00002466bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2467 SmallVector<int, 8> M;
2468 N->getMask(M);
2469 return ::isMOVLMask(M, N->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002470}
2471
2472/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2473/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2474/// element of vector 2 and the other elements to come from vector 1 in order.
Nate Begemane8f61cb2009-04-29 05:20:52 +00002475static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Nate Begeman543d2142009-04-27 18:41:29 +00002476 bool V2IsSplat = false, bool V2IsUndef = false) {
2477 int NumOps = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002478 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2479 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002480
2481 if (!isUndefOrEqual(Mask[0], 0))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002482 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002483
2484 for (int i = 1; i < NumOps; ++i)
2485 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2486 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2487 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002488 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002489
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002490 return true;
2491}
2492
Nate Begeman543d2142009-04-27 18:41:29 +00002493static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002494 bool V2IsUndef = false) {
Nate Begeman543d2142009-04-27 18:41:29 +00002495 SmallVector<int, 8> M;
2496 N->getMask(M);
2497 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002498}
2499
2500/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2501/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman543d2142009-04-27 18:41:29 +00002502bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2503 if (N->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002504 return false;
2505
2506 // Expect 1, 1, 3, 3
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002507 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002508 int Elt = N->getMaskElt(i);
2509 if (Elt >= 0 && Elt != 1)
2510 return false;
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002511 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002512
2513 bool HasHi = false;
2514 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002515 int Elt = N->getMaskElt(i);
2516 if (Elt >= 0 && Elt != 3)
2517 return false;
2518 if (Elt == 3)
2519 HasHi = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002520 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002521 // Don't use movshdup if it can be done with a shufps.
Nate Begeman543d2142009-04-27 18:41:29 +00002522 // FIXME: verify that matching u, u, 3, 3 is what we want.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002523 return HasHi;
2524}
2525
2526/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2527/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman543d2142009-04-27 18:41:29 +00002528bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2529 if (N->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002530 return false;
2531
2532 // Expect 0, 0, 2, 2
Nate Begeman543d2142009-04-27 18:41:29 +00002533 for (unsigned i = 0; i < 2; ++i)
2534 if (N->getMaskElt(i) > 0)
2535 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002536
2537 bool HasHi = false;
2538 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002539 int Elt = N->getMaskElt(i);
2540 if (Elt >= 0 && Elt != 2)
2541 return false;
2542 if (Elt == 2)
2543 HasHi = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002544 }
Nate Begeman543d2142009-04-27 18:41:29 +00002545 // Don't use movsldup if it can be done with a shufps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002546 return HasHi;
2547}
2548
Evan Chenga2497eb2008-09-25 20:50:48 +00002549/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2550/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman543d2142009-04-27 18:41:29 +00002551bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2552 int e = N->getValueType(0).getVectorNumElements() / 2;
2553
2554 for (int i = 0; i < e; ++i)
2555 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chenga2497eb2008-09-25 20:50:48 +00002556 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002557 for (int i = 0; i < e; ++i)
2558 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Chenga2497eb2008-09-25 20:50:48 +00002559 return false;
2560 return true;
2561}
2562
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002563/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2564/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2565/// instructions.
2566unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00002567 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2568 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2569
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002570 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2571 unsigned Mask = 0;
Nate Begeman543d2142009-04-27 18:41:29 +00002572 for (int i = 0; i < NumOperands; ++i) {
2573 int Val = SVOp->getMaskElt(NumOperands-i-1);
2574 if (Val < 0) Val = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002575 if (Val >= NumOperands) Val -= NumOperands;
2576 Mask |= Val;
2577 if (i != NumOperands - 1)
2578 Mask <<= Shift;
2579 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002580 return Mask;
2581}
2582
2583/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2584/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2585/// instructions.
2586unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00002587 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002588 unsigned Mask = 0;
2589 // 8 nodes, but we only care about the last 4.
2590 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002591 int Val = SVOp->getMaskElt(i);
2592 if (Val >= 0)
Mon P Wang56d91642009-02-04 01:16:59 +00002593 Mask |= (Val - 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002594 if (i != 4)
2595 Mask <<= 2;
2596 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002597 return Mask;
2598}
2599
2600/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2601/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2602/// instructions.
2603unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00002604 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002605 unsigned Mask = 0;
2606 // 8 nodes, but we only care about the first 4.
2607 for (int i = 3; i >= 0; --i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002608 int Val = SVOp->getMaskElt(i);
2609 if (Val >= 0)
2610 Mask |= Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002611 if (i != 0)
2612 Mask <<= 2;
2613 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002614 return Mask;
2615}
2616
Nate Begeman543d2142009-04-27 18:41:29 +00002617/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2618/// their permute mask.
2619static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2620 SelectionDAG &DAG) {
2621 MVT VT = SVOp->getValueType(0);
Nate Begemane8f61cb2009-04-29 05:20:52 +00002622 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman543d2142009-04-27 18:41:29 +00002623 SmallVector<int, 8> MaskVec;
2624
Nate Begemane8f61cb2009-04-29 05:20:52 +00002625 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002626 int idx = SVOp->getMaskElt(i);
2627 if (idx < 0)
2628 MaskVec.push_back(idx);
Nate Begemane8f61cb2009-04-29 05:20:52 +00002629 else if (idx < (int)NumElems)
Nate Begeman543d2142009-04-27 18:41:29 +00002630 MaskVec.push_back(idx + NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002631 else
Nate Begeman543d2142009-04-27 18:41:29 +00002632 MaskVec.push_back(idx - NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002633 }
Nate Begeman543d2142009-04-27 18:41:29 +00002634 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2635 SVOp->getOperand(0), &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002636}
2637
Evan Chenga6769df2007-12-07 21:30:01 +00002638/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2639/// the two vector operands have swapped position.
Nate Begeman543d2142009-04-27 18:41:29 +00002640static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begemane8f61cb2009-04-29 05:20:52 +00002641 unsigned NumElems = VT.getVectorNumElements();
2642 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002643 int idx = Mask[i];
2644 if (idx < 0)
Evan Chengfca29242007-12-07 08:07:39 +00002645 continue;
Nate Begemane8f61cb2009-04-29 05:20:52 +00002646 else if (idx < (int)NumElems)
Nate Begeman543d2142009-04-27 18:41:29 +00002647 Mask[i] = idx + NumElems;
Evan Chengfca29242007-12-07 08:07:39 +00002648 else
Nate Begeman543d2142009-04-27 18:41:29 +00002649 Mask[i] = idx - NumElems;
Evan Chengfca29242007-12-07 08:07:39 +00002650 }
Evan Chengfca29242007-12-07 08:07:39 +00002651}
2652
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002653/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2654/// match movhlps. The lower half elements should come from upper half of
2655/// V1 (and in order), and the upper half elements should come from the upper
2656/// half of V2 (and in order).
Nate Begeman543d2142009-04-27 18:41:29 +00002657static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2658 if (Op->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002659 return false;
2660 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002661 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002662 return false;
2663 for (unsigned i = 2; i != 4; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002664 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665 return false;
2666 return true;
2667}
2668
2669/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002670/// is promoted to a vector. It also returns the LoadSDNode by reference if
2671/// required.
2672static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002673 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2674 return false;
2675 N = N->getOperand(0).getNode();
2676 if (!ISD::isNON_EXTLoad(N))
2677 return false;
2678 if (LD)
2679 *LD = cast<LoadSDNode>(N);
2680 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002681}
2682
2683/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2684/// match movlp{s|d}. The lower half elements should come from lower half of
2685/// V1 (and in order), and the upper half elements should come from the upper
2686/// half of V2 (and in order). And since V1 will become the source of the
2687/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman543d2142009-04-27 18:41:29 +00002688static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
2689 ShuffleVectorSDNode *Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002690 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2691 return false;
2692 // Is V2 is a vector load, don't do this transformation. We will try to use
2693 // load folding shufps op.
2694 if (ISD::isNON_EXTLoad(V2))
2695 return false;
2696
Nate Begemane8f61cb2009-04-29 05:20:52 +00002697 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Nate Begeman543d2142009-04-27 18:41:29 +00002698
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002699 if (NumElems != 2 && NumElems != 4)
2700 return false;
Nate Begemane8f61cb2009-04-29 05:20:52 +00002701 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002702 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002703 return false;
Nate Begemane8f61cb2009-04-29 05:20:52 +00002704 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002705 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002706 return false;
2707 return true;
2708}
2709
2710/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2711/// all the same.
2712static bool isSplatVector(SDNode *N) {
2713 if (N->getOpcode() != ISD::BUILD_VECTOR)
2714 return false;
2715
Dan Gohman8181bd12008-07-27 21:46:04 +00002716 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002717 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2718 if (N->getOperand(i) != SplatValue)
2719 return false;
2720 return true;
2721}
2722
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002723/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2724/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002725static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002726 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002727 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002728 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002729 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002730}
2731
2732/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Nate Begeman543d2142009-04-27 18:41:29 +00002733/// to an zero vector.
Nate Begemane8f61cb2009-04-29 05:20:52 +00002734/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman543d2142009-04-27 18:41:29 +00002735static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002736 SDValue V1 = N->getOperand(0);
2737 SDValue V2 = N->getOperand(1);
Nate Begemane8f61cb2009-04-29 05:20:52 +00002738 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2739 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002740 int Idx = N->getMaskElt(i);
Nate Begemane8f61cb2009-04-29 05:20:52 +00002741 if (Idx >= (int)NumElems) {
Nate Begeman543d2142009-04-27 18:41:29 +00002742 unsigned Opc = V2.getOpcode();
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002743 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
2744 continue;
Nate Begeman543d2142009-04-27 18:41:29 +00002745 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V2.getOperand(Idx-NumElems)))
2746 return false;
2747 } else if (Idx >= 0) {
2748 unsigned Opc = V1.getOpcode();
2749 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
2750 continue;
2751 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V1.getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002752 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002753 }
2754 }
2755 return true;
2756}
2757
2758/// getZeroVector - Returns a vector of specified type with all zero elements.
2759///
Dale Johannesence0805b2009-02-03 19:33:06 +00002760static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2761 DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002762 assert(VT.isVector() && "Expected a vector type");
Scott Michel91099d62009-02-17 22:15:04 +00002763
Chris Lattnere6aa3862007-11-25 00:24:49 +00002764 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2765 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002766 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002767 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002768 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Cheng907a2d22009-02-25 22:49:59 +00002769 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002770 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002771 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Cheng907a2d22009-02-25 22:49:59 +00002772 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002773 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002774 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng907a2d22009-02-25 22:49:59 +00002775 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002776 }
Dale Johannesence0805b2009-02-03 19:33:06 +00002777 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002778}
2779
Chris Lattnere6aa3862007-11-25 00:24:49 +00002780/// getOnesVector - Returns a vector of specified type with all bits set.
2781///
Dale Johannesence0805b2009-02-03 19:33:06 +00002782static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002783 assert(VT.isVector() && "Expected a vector type");
Scott Michel91099d62009-02-17 22:15:04 +00002784
Chris Lattnere6aa3862007-11-25 00:24:49 +00002785 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2786 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002787 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2788 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002789 if (VT.getSizeInBits() == 64) // MMX
Evan Cheng907a2d22009-02-25 22:49:59 +00002790 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002791 else // SSE
Evan Cheng907a2d22009-02-25 22:49:59 +00002792 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesence0805b2009-02-03 19:33:06 +00002793 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002794}
2795
2796
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002797/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2798/// that point to V2 points to its first element.
Nate Begeman543d2142009-04-27 18:41:29 +00002799static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
2800 MVT VT = SVOp->getValueType(0);
Nate Begemane8f61cb2009-04-29 05:20:52 +00002801 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman543d2142009-04-27 18:41:29 +00002802
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002803 bool Changed = false;
Nate Begeman543d2142009-04-27 18:41:29 +00002804 SmallVector<int, 8> MaskVec;
2805 SVOp->getMask(MaskVec);
2806
Nate Begemane8f61cb2009-04-29 05:20:52 +00002807 for (unsigned i = 0; i != NumElems; ++i) {
2808 if (MaskVec[i] > (int)NumElems) {
Nate Begeman543d2142009-04-27 18:41:29 +00002809 MaskVec[i] = NumElems;
2810 Changed = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002811 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002812 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002813 if (Changed)
Nate Begeman543d2142009-04-27 18:41:29 +00002814 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
2815 SVOp->getOperand(1), &MaskVec[0]);
2816 return SDValue(SVOp, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002817}
2818
2819/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2820/// operation of specified width.
Nate Begeman543d2142009-04-27 18:41:29 +00002821static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2822 SDValue V2) {
2823 unsigned NumElems = VT.getVectorNumElements();
2824 SmallVector<int, 8> Mask;
2825 Mask.push_back(NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002826 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002827 Mask.push_back(i);
2828 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002829}
2830
Nate Begeman543d2142009-04-27 18:41:29 +00002831/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
2832static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2833 SDValue V2) {
2834 unsigned NumElems = VT.getVectorNumElements();
2835 SmallVector<int, 8> Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002836 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002837 Mask.push_back(i);
2838 Mask.push_back(i + NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002839 }
Nate Begeman543d2142009-04-27 18:41:29 +00002840 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002841}
2842
Nate Begeman543d2142009-04-27 18:41:29 +00002843/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
2844static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2845 SDValue V2) {
2846 unsigned NumElems = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002847 unsigned Half = NumElems/2;
Nate Begeman543d2142009-04-27 18:41:29 +00002848 SmallVector<int, 8> Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002849 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002850 Mask.push_back(i + Half);
2851 Mask.push_back(i + NumElems + Half);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002852 }
Nate Begeman543d2142009-04-27 18:41:29 +00002853 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner2d91b962008-03-09 01:05:04 +00002854}
2855
Evan Chengbf8b2c52008-04-05 00:30:36 +00002856/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Nate Begeman543d2142009-04-27 18:41:29 +00002857static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
2858 bool HasSSE2) {
2859 if (SV->getValueType(0).getVectorNumElements() <= 4)
2860 return SDValue(SV, 0);
2861
2862 MVT PVT = MVT::v4f32;
2863 MVT VT = SV->getValueType(0);
2864 DebugLoc dl = SV->getDebugLoc();
2865 SDValue V1 = SV->getOperand(0);
2866 int NumElems = VT.getVectorNumElements();
2867 int EltNo = SV->getSplatIndex();
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002868
Nate Begeman543d2142009-04-27 18:41:29 +00002869 // unpack elements to the correct location
2870 while (NumElems > 4) {
2871 if (EltNo < NumElems/2) {
2872 V1 = getUnpackl(DAG, dl, VT, V1, V1);
2873 } else {
2874 V1 = getUnpackh(DAG, dl, VT, V1, V1);
2875 EltNo -= NumElems/2;
2876 }
2877 NumElems >>= 1;
2878 }
2879
2880 // Perform the splat.
2881 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesence0805b2009-02-03 19:33:06 +00002882 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman543d2142009-04-27 18:41:29 +00002883 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
2884 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002885}
2886
Evan Chenga2497eb2008-09-25 20:50:48 +00002887/// isVectorLoad - Returns true if the node is a vector load, a scalar
2888/// load that's promoted to vector, or a load bitcasted.
2889static bool isVectorLoad(SDValue Op) {
2890 assert(Op.getValueType().isVector() && "Expected a vector type");
2891 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
2892 Op.getOpcode() == ISD::BIT_CONVERT) {
2893 return isa<LoadSDNode>(Op.getOperand(0));
2894 }
2895 return isa<LoadSDNode>(Op);
2896}
2897
2898
2899/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
2900///
Nate Begeman543d2142009-04-27 18:41:29 +00002901static SDValue CanonicalizeMovddup(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
2902 bool HasSSE3) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002903 // If we have sse3 and shuffle has more than one use or input is a load, then
2904 // use movddup. Otherwise, use movlhps.
Nate Begeman543d2142009-04-27 18:41:29 +00002905 SDValue V1 = SV->getOperand(0);
2906
2907 bool UseMovddup = HasSSE3 && (!SV->hasOneUse() || isVectorLoad(V1));
Evan Chenga2497eb2008-09-25 20:50:48 +00002908 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
Nate Begeman543d2142009-04-27 18:41:29 +00002909 MVT VT = SV->getValueType(0);
Evan Chenga2497eb2008-09-25 20:50:48 +00002910 if (VT == PVT)
Nate Begeman543d2142009-04-27 18:41:29 +00002911 return SDValue(SV, 0);
2912
2913 DebugLoc dl = SV->getDebugLoc();
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002914 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman543d2142009-04-27 18:41:29 +00002915 if (PVT.getVectorNumElements() == 2) {
2916 int Mask[2] = { 0, 0 };
2917 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), Mask);
2918 } else {
2919 int Mask[4] = { 0, 1, 0, 1 };
2920 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), Mask);
2921 }
2922 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chenga2497eb2008-09-25 20:50:48 +00002923}
2924
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002925/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002926/// vector of zero or undef vector. This produces a shuffle where the low
2927/// element of V2 is swizzled into the zero/undef vector, landing at element
2928/// 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 +00002929static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002930 bool isZero, bool HasSSE2,
2931 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002932 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002933 SDValue V1 = isZero
Nate Begeman543d2142009-04-27 18:41:29 +00002934 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
2935 unsigned NumElems = VT.getVectorNumElements();
2936 SmallVector<int, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00002937 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002938 // If this is the insertion idx, put the low elt of V2 here.
2939 MaskVec.push_back(i == Idx ? NumElems : i);
2940 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002941}
2942
Evan Chengdea99362008-05-29 08:22:04 +00002943/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2944/// a shuffle that is zero.
2945static
Nate Begeman543d2142009-04-27 18:41:29 +00002946unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
2947 bool Low, SelectionDAG &DAG) {
Evan Chengdea99362008-05-29 08:22:04 +00002948 unsigned NumZeros = 0;
Nate Begeman543d2142009-04-27 18:41:29 +00002949 for (int i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002950 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman543d2142009-04-27 18:41:29 +00002951 int Idx = SVOp->getMaskElt(Index);
2952 if (Idx < 0) {
Evan Chengdea99362008-05-29 08:22:04 +00002953 ++NumZeros;
2954 continue;
2955 }
Nate Begeman543d2142009-04-27 18:41:29 +00002956 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Gabor Greif1c80d112008-08-28 21:40:38 +00002957 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00002958 ++NumZeros;
2959 else
2960 break;
2961 }
2962 return NumZeros;
2963}
2964
2965/// isVectorShift - Returns true if the shuffle can be implemented as a
2966/// logical left or right shift of a vector.
Nate Begeman543d2142009-04-27 18:41:29 +00002967/// FIXME: split into pslldqi, psrldqi, palignr variants.
2968static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00002969 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman543d2142009-04-27 18:41:29 +00002970 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengdea99362008-05-29 08:22:04 +00002971
2972 isLeft = true;
Nate Begeman543d2142009-04-27 18:41:29 +00002973 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengdea99362008-05-29 08:22:04 +00002974 if (!NumZeros) {
2975 isLeft = false;
Nate Begeman543d2142009-04-27 18:41:29 +00002976 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengdea99362008-05-29 08:22:04 +00002977 if (!NumZeros)
2978 return false;
2979 }
Evan Chengdea99362008-05-29 08:22:04 +00002980 bool SeenV1 = false;
2981 bool SeenV2 = false;
Nate Begeman543d2142009-04-27 18:41:29 +00002982 for (int i = NumZeros; i < NumElems; ++i) {
2983 int Val = isLeft ? (i - NumZeros) : i;
2984 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
2985 if (Idx < 0)
Evan Chengdea99362008-05-29 08:22:04 +00002986 continue;
Nate Begeman543d2142009-04-27 18:41:29 +00002987 if (Idx < NumElems)
Evan Chengdea99362008-05-29 08:22:04 +00002988 SeenV1 = true;
2989 else {
Nate Begeman543d2142009-04-27 18:41:29 +00002990 Idx -= NumElems;
Evan Chengdea99362008-05-29 08:22:04 +00002991 SeenV2 = true;
2992 }
Nate Begeman543d2142009-04-27 18:41:29 +00002993 if (Idx != Val)
Evan Chengdea99362008-05-29 08:22:04 +00002994 return false;
2995 }
2996 if (SeenV1 && SeenV2)
2997 return false;
2998
Nate Begeman543d2142009-04-27 18:41:29 +00002999 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengdea99362008-05-29 08:22:04 +00003000 ShAmt = NumZeros;
3001 return true;
3002}
3003
3004
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003005/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3006///
Dan Gohman8181bd12008-07-27 21:46:04 +00003007static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003008 unsigned NumNonZero, unsigned NumZero,
3009 SelectionDAG &DAG, TargetLowering &TLI) {
3010 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003011 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003012
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00003013 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00003014 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003015 bool First = true;
3016 for (unsigned i = 0; i < 16; ++i) {
3017 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3018 if (ThisIsNonZero && First) {
3019 if (NumZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003020 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003021 else
Dale Johannesen9bfc0172009-02-06 23:05:02 +00003022 V = DAG.getUNDEF(MVT::v8i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003023 First = false;
3024 }
3025
3026 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003027 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003028 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3029 if (LastIsNonZero) {
Scott Michel91099d62009-02-17 22:15:04 +00003030 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Dale Johannesence0805b2009-02-03 19:33:06 +00003031 MVT::i16, Op.getOperand(i-1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003032 }
3033 if (ThisIsNonZero) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003034 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3035 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003036 ThisElt, DAG.getConstant(8, MVT::i8));
3037 if (LastIsNonZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003038 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003039 } else
3040 ThisElt = LastElt;
3041
Gabor Greif1c80d112008-08-28 21:40:38 +00003042 if (ThisElt.getNode())
Dale Johannesence0805b2009-02-03 19:33:06 +00003043 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003044 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003045 }
3046 }
3047
Dale Johannesence0805b2009-02-03 19:33:06 +00003048 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003049}
3050
3051/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3052///
Dan Gohman8181bd12008-07-27 21:46:04 +00003053static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003054 unsigned NumNonZero, unsigned NumZero,
3055 SelectionDAG &DAG, TargetLowering &TLI) {
3056 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003057 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003058
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00003059 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00003060 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003061 bool First = true;
3062 for (unsigned i = 0; i < 8; ++i) {
3063 bool isNonZero = (NonZeros & (1 << i)) != 0;
3064 if (isNonZero) {
3065 if (First) {
3066 if (NumZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003067 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003068 else
Dale Johannesen9bfc0172009-02-06 23:05:02 +00003069 V = DAG.getUNDEF(MVT::v8i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003070 First = false;
3071 }
Scott Michel91099d62009-02-17 22:15:04 +00003072 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Dale Johannesence0805b2009-02-03 19:33:06 +00003073 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003074 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003075 }
3076 }
3077
3078 return V;
3079}
3080
Evan Chengdea99362008-05-29 08:22:04 +00003081/// getVShift - Return a vector logical shift node.
3082///
Dan Gohman8181bd12008-07-27 21:46:04 +00003083static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Nate Begeman543d2142009-04-27 18:41:29 +00003084 unsigned NumBits, SelectionDAG &DAG,
3085 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003086 bool isMMX = VT.getSizeInBits() == 64;
3087 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003088 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesence0805b2009-02-03 19:33:06 +00003089 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3090 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3091 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003092 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003093}
3094
Dan Gohman8181bd12008-07-27 21:46:04 +00003095SDValue
3096X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00003097 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere6aa3862007-11-25 00:24:49 +00003098 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003099 if (ISD::isBuildVectorAllZeros(Op.getNode())
3100 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003101 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3102 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3103 // eliminated on x86-32 hosts.
3104 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3105 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003106
Gabor Greif1c80d112008-08-28 21:40:38 +00003107 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00003108 return getOnesVector(Op.getValueType(), DAG, dl);
3109 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003110 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003111
Duncan Sands92c43912008-06-06 12:08:01 +00003112 MVT VT = Op.getValueType();
3113 MVT EVT = VT.getVectorElementType();
3114 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003115
3116 unsigned NumElems = Op.getNumOperands();
3117 unsigned NumZero = 0;
3118 unsigned NumNonZero = 0;
3119 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003120 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003121 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003122 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003123 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003124 if (Elt.getOpcode() == ISD::UNDEF)
3125 continue;
3126 Values.insert(Elt);
3127 if (Elt.getOpcode() != ISD::Constant &&
3128 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003129 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003130 if (isZeroNode(Elt))
3131 NumZero++;
3132 else {
3133 NonZeros |= (1 << i);
3134 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003135 }
3136 }
3137
3138 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003139 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesen9bfc0172009-02-06 23:05:02 +00003140 return DAG.getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003141 }
3142
Chris Lattner66a4dda2008-03-09 05:42:06 +00003143 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003144 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003145 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003146 SDValue Item = Op.getOperand(Idx);
Scott Michel91099d62009-02-17 22:15:04 +00003147
Chris Lattner2d91b962008-03-09 01:05:04 +00003148 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3149 // the value are obviously zero, truncate the value to i32 and do the
3150 // insertion that way. Only do this if the value is non-constant or if the
3151 // value is a constant being inserted into element 0. It is cheaper to do
3152 // a constant pool load than it is to do a movd + shuffle.
3153 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3154 (!IsAllConstants || Idx == 0)) {
3155 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3156 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003157 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3158 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michel91099d62009-02-17 22:15:04 +00003159
Chris Lattner2d91b962008-03-09 01:05:04 +00003160 // Truncate the value (which may itself be a constant) to i32, and
3161 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesence0805b2009-02-03 19:33:06 +00003162 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3163 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003164 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3165 Subtarget->hasSSE2(), DAG);
Scott Michel91099d62009-02-17 22:15:04 +00003166
Chris Lattner2d91b962008-03-09 01:05:04 +00003167 // Now we have our 32-bit value zero extended in the low element of
3168 // a vector. If Idx != 0, swizzle it into place.
3169 if (Idx != 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00003170 SmallVector<int, 4> Mask;
3171 Mask.push_back(Idx);
3172 for (unsigned i = 1; i != VecElts; ++i)
3173 Mask.push_back(i);
3174 Item = DAG.getVectorShuffle(VecVT, dl, Item,
3175 DAG.getUNDEF(Item.getValueType()),
3176 &Mask[0]);
Chris Lattner2d91b962008-03-09 01:05:04 +00003177 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003178 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner2d91b962008-03-09 01:05:04 +00003179 }
3180 }
Scott Michel91099d62009-02-17 22:15:04 +00003181
Chris Lattnerac914892008-03-08 22:59:52 +00003182 // If we have a constant or non-constant insertion into the low element of
3183 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3184 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3185 // depending on what the source datatype is. Because we can only get here
3186 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3187 if (Idx == 0 &&
3188 // Don't do this for i64 values on x86-32.
3189 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003190 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003191 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003192 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3193 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003194 }
Evan Chengdea99362008-05-29 08:22:04 +00003195
3196 // Is it a vector logical left shift?
3197 if (NumElems == 2 && Idx == 1 &&
3198 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003199 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003200 return getVShift(true, VT,
Scott Michel91099d62009-02-17 22:15:04 +00003201 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesen24dd9a52009-02-07 00:55:49 +00003202 VT, Op.getOperand(1)),
Dale Johannesence0805b2009-02-03 19:33:06 +00003203 NumBits/2, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00003204 }
Scott Michel91099d62009-02-17 22:15:04 +00003205
Chris Lattner92bdcb52008-03-08 22:48:29 +00003206 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003207 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003208
Chris Lattnerac914892008-03-08 22:59:52 +00003209 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3210 // is a non-constant being inserted into an element other than the low one,
3211 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3212 // movd/movss) to move this into the low element, then shuffle it into
3213 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003214 if (EVTBits == 32) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003215 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michel91099d62009-02-17 22:15:04 +00003216
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003217 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003218 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3219 Subtarget->hasSSE2(), DAG);
Nate Begeman543d2142009-04-27 18:41:29 +00003220 SmallVector<int, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003221 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman543d2142009-04-27 18:41:29 +00003222 MaskVec.push_back(i == Idx ? 0 : 1);
3223 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003224 }
3225 }
3226
Chris Lattner66a4dda2008-03-09 05:42:06 +00003227 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3228 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003229 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00003230
Dan Gohman21463242007-07-24 22:55:08 +00003231 // A vector full of immediates; various special cases are already
3232 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003233 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003234 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003235
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003236 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003237 if (EVTBits == 64) {
3238 if (NumNonZero == 1) {
3239 // One half is zero or undef.
3240 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesence0805b2009-02-03 19:33:06 +00003241 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003242 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003243 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3244 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003245 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003246 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003247 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003248
3249 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3250 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003251 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003252 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003253 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003254 }
3255
3256 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003257 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003258 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003259 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003260 }
3261
3262 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003263 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003264 V.resize(NumElems);
3265 if (NumElems == 4 && NumZero > 0) {
3266 for (unsigned i = 0; i < 4; ++i) {
3267 bool isZero = !(NonZeros & (1 << i));
3268 if (isZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003269 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003270 else
Dale Johannesence0805b2009-02-03 19:33:06 +00003271 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003272 }
3273
3274 for (unsigned i = 0; i < 2; ++i) {
3275 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3276 default: break;
3277 case 0:
3278 V[i] = V[i*2]; // Must be a zero vector.
3279 break;
3280 case 1:
Nate Begeman543d2142009-04-27 18:41:29 +00003281 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003282 break;
3283 case 2:
Nate Begeman543d2142009-04-27 18:41:29 +00003284 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003285 break;
3286 case 3:
Nate Begeman543d2142009-04-27 18:41:29 +00003287 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003288 break;
3289 }
3290 }
3291
Nate Begeman543d2142009-04-27 18:41:29 +00003292 SmallVector<int, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003293 bool Reverse = (NonZeros & 0x3) == 2;
3294 for (unsigned i = 0; i < 2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003295 MaskVec.push_back(Reverse ? 1-i : i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3297 for (unsigned i = 0; i < 2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003298 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3299 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003300 }
3301
3302 if (Values.size() > 2) {
Nate Begeman543d2142009-04-27 18:41:29 +00003303 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3304 // values to be inserted is equal to the number of elements, in which case
3305 // use the unpack code below in the hopes of matching the consecutive elts
3306 // load merge pattern for shuffles.
3307 // FIXME: We could probably just check that here directly.
3308 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
3309 getSubtarget()->hasSSE41()) {
3310 V[0] = DAG.getUNDEF(VT);
3311 for (unsigned i = 0; i < NumElems; ++i)
3312 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3313 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3314 Op.getOperand(i), DAG.getIntPtrConstant(i));
3315 return V[0];
3316 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003317 // Expand into a number of unpckl*.
3318 // e.g. for v4f32
3319 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3320 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3321 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003322 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesence0805b2009-02-03 19:33:06 +00003323 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003324 NumElems >>= 1;
3325 while (NumElems != 0) {
3326 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003327 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003328 NumElems >>= 1;
3329 }
3330 return V[0];
3331 }
3332
Dan Gohman8181bd12008-07-27 21:46:04 +00003333 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003334}
3335
Nate Begeman2c87c422009-02-23 08:49:38 +00003336// v8i16 shuffles - Prefer shuffles in the following order:
3337// 1. [all] pshuflw, pshufhw, optional move
3338// 2. [ssse3] 1 x pshufb
3339// 3. [ssse3] 2 x pshufb + 1 x por
3340// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Chengfca29242007-12-07 08:07:39 +00003341static
Nate Begeman543d2142009-04-27 18:41:29 +00003342SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3343 SelectionDAG &DAG, X86TargetLowering &TLI) {
3344 SDValue V1 = SVOp->getOperand(0);
3345 SDValue V2 = SVOp->getOperand(1);
3346 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman2c87c422009-02-23 08:49:38 +00003347 SmallVector<int, 8> MaskVals;
Evan Cheng75184a92007-12-11 01:46:18 +00003348
Nate Begeman2c87c422009-02-23 08:49:38 +00003349 // Determine if more than 1 of the words in each of the low and high quadwords
3350 // of the result come from the same quadword of one of the two inputs. Undef
3351 // mask values count as coming from any quadword, for better codegen.
3352 SmallVector<unsigned, 4> LoQuad(4);
3353 SmallVector<unsigned, 4> HiQuad(4);
3354 BitVector InputQuads(4);
3355 for (unsigned i = 0; i < 8; ++i) {
3356 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman543d2142009-04-27 18:41:29 +00003357 int EltIdx = SVOp->getMaskElt(i);
Nate Begeman2c87c422009-02-23 08:49:38 +00003358 MaskVals.push_back(EltIdx);
3359 if (EltIdx < 0) {
3360 ++Quad[0];
3361 ++Quad[1];
3362 ++Quad[2];
3363 ++Quad[3];
Evan Cheng75184a92007-12-11 01:46:18 +00003364 continue;
Nate Begeman2c87c422009-02-23 08:49:38 +00003365 }
3366 ++Quad[EltIdx / 4];
3367 InputQuads.set(EltIdx / 4);
Evan Cheng75184a92007-12-11 01:46:18 +00003368 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003369
Nate Begeman2c87c422009-02-23 08:49:38 +00003370 int BestLoQuad = -1;
Evan Cheng75184a92007-12-11 01:46:18 +00003371 unsigned MaxQuad = 1;
3372 for (unsigned i = 0; i < 4; ++i) {
Nate Begeman2c87c422009-02-23 08:49:38 +00003373 if (LoQuad[i] > MaxQuad) {
3374 BestLoQuad = i;
3375 MaxQuad = LoQuad[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003376 }
Evan Chengfca29242007-12-07 08:07:39 +00003377 }
3378
Nate Begeman2c87c422009-02-23 08:49:38 +00003379 int BestHiQuad = -1;
Evan Cheng75184a92007-12-11 01:46:18 +00003380 MaxQuad = 1;
3381 for (unsigned i = 0; i < 4; ++i) {
Nate Begeman2c87c422009-02-23 08:49:38 +00003382 if (HiQuad[i] > MaxQuad) {
3383 BestHiQuad = i;
3384 MaxQuad = HiQuad[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003385 }
3386 }
3387
Nate Begeman2c87c422009-02-23 08:49:38 +00003388 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
3389 // of the two input vectors, shuffle them into one input vector so only a
3390 // single pshufb instruction is necessary. If There are more than 2 input
3391 // quads, disable the next transformation since it does not help SSSE3.
3392 bool V1Used = InputQuads[0] || InputQuads[1];
3393 bool V2Used = InputQuads[2] || InputQuads[3];
3394 if (TLI.getSubtarget()->hasSSSE3()) {
3395 if (InputQuads.count() == 2 && V1Used && V2Used) {
3396 BestLoQuad = InputQuads.find_first();
3397 BestHiQuad = InputQuads.find_next(BestLoQuad);
3398 }
3399 if (InputQuads.count() > 2) {
3400 BestLoQuad = -1;
3401 BestHiQuad = -1;
3402 }
3403 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003404
Nate Begeman2c87c422009-02-23 08:49:38 +00003405 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3406 // the shuffle mask. If a quad is scored as -1, that means that it contains
3407 // words from all 4 input quadwords.
3408 SDValue NewV;
3409 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00003410 SmallVector<int, 8> MaskV;
3411 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3412 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
3413 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
3414 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3415 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
Dale Johannesence0805b2009-02-03 19:33:06 +00003416 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng75184a92007-12-11 01:46:18 +00003417
Nate Begeman2c87c422009-02-23 08:49:38 +00003418 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3419 // source words for the shuffle, to aid later transformations.
3420 bool AllWordsInNewV = true;
Mon P Wangb1db1202009-03-11 06:35:11 +00003421 bool InOrder[2] = { true, true };
Evan Cheng75184a92007-12-11 01:46:18 +00003422 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman2c87c422009-02-23 08:49:38 +00003423 int idx = MaskVals[i];
Mon P Wangb1db1202009-03-11 06:35:11 +00003424 if (idx != (int)i)
3425 InOrder[i/4] = false;
Nate Begeman2c87c422009-02-23 08:49:38 +00003426 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng75184a92007-12-11 01:46:18 +00003427 continue;
Nate Begeman2c87c422009-02-23 08:49:38 +00003428 AllWordsInNewV = false;
3429 break;
Evan Cheng75184a92007-12-11 01:46:18 +00003430 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003431
Nate Begeman2c87c422009-02-23 08:49:38 +00003432 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3433 if (AllWordsInNewV) {
3434 for (int i = 0; i != 8; ++i) {
3435 int idx = MaskVals[i];
3436 if (idx < 0)
Evan Cheng75184a92007-12-11 01:46:18 +00003437 continue;
Nate Begeman2c87c422009-02-23 08:49:38 +00003438 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
3439 if ((idx != i) && idx < 4)
3440 pshufhw = false;
3441 if ((idx != i) && idx > 3)
3442 pshuflw = false;
Evan Cheng75184a92007-12-11 01:46:18 +00003443 }
Nate Begeman2c87c422009-02-23 08:49:38 +00003444 V1 = NewV;
3445 V2Used = false;
3446 BestLoQuad = 0;
3447 BestHiQuad = 1;
Evan Chengfca29242007-12-07 08:07:39 +00003448 }
Evan Cheng75184a92007-12-11 01:46:18 +00003449
Nate Begeman2c87c422009-02-23 08:49:38 +00003450 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3451 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wangb1db1202009-03-11 06:35:11 +00003452 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Nate Begeman543d2142009-04-27 18:41:29 +00003453 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
3454 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng75184a92007-12-11 01:46:18 +00003455 }
Evan Cheng75184a92007-12-11 01:46:18 +00003456 }
Nate Begeman2c87c422009-02-23 08:49:38 +00003457
3458 // If we have SSSE3, and all words of the result are from 1 input vector,
3459 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3460 // is present, fall back to case 4.
3461 if (TLI.getSubtarget()->hasSSSE3()) {
3462 SmallVector<SDValue,16> pshufbMask;
3463
3464 // If we have elements from both input vectors, set the high bit of the
3465 // shuffle mask element to zero out elements that come from V2 in the V1
3466 // mask, and elements that come from V1 in the V2 mask, so that the two
3467 // results can be OR'd together.
3468 bool TwoInputs = V1Used && V2Used;
3469 for (unsigned i = 0; i != 8; ++i) {
3470 int EltIdx = MaskVals[i] * 2;
3471 if (TwoInputs && (EltIdx >= 16)) {
3472 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3473 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3474 continue;
3475 }
3476 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3477 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
3478 }
3479 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
3480 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Cheng907a2d22009-02-25 22:49:59 +00003481 DAG.getNode(ISD::BUILD_VECTOR, dl,
3482 MVT::v16i8, &pshufbMask[0], 16));
Nate Begeman2c87c422009-02-23 08:49:38 +00003483 if (!TwoInputs)
3484 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3485
3486 // Calculate the shuffle mask for the second input, shuffle it, and
3487 // OR it with the first shuffled input.
3488 pshufbMask.clear();
3489 for (unsigned i = 0; i != 8; ++i) {
3490 int EltIdx = MaskVals[i] * 2;
3491 if (EltIdx < 16) {
3492 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3493 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3494 continue;
3495 }
3496 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3497 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
3498 }
3499 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
3500 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Cheng907a2d22009-02-25 22:49:59 +00003501 DAG.getNode(ISD::BUILD_VECTOR, dl,
3502 MVT::v16i8, &pshufbMask[0], 16));
Nate Begeman2c87c422009-02-23 08:49:38 +00003503 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3504 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3505 }
3506
3507 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3508 // and update MaskVals with new element order.
3509 BitVector InOrder(8);
3510 if (BestLoQuad >= 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00003511 SmallVector<int, 8> MaskV;
Nate Begeman2c87c422009-02-23 08:49:38 +00003512 for (int i = 0; i != 4; ++i) {
3513 int idx = MaskVals[i];
3514 if (idx < 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00003515 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00003516 InOrder.set(i);
3517 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman543d2142009-04-27 18:41:29 +00003518 MaskV.push_back(idx & 3);
Nate Begeman2c87c422009-02-23 08:49:38 +00003519 InOrder.set(i);
3520 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00003521 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00003522 }
3523 }
3524 for (unsigned i = 4; i != 8; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003525 MaskV.push_back(i);
3526 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3527 &MaskV[0]);
Nate Begeman2c87c422009-02-23 08:49:38 +00003528 }
3529
3530 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3531 // and update MaskVals with the new element order.
3532 if (BestHiQuad >= 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00003533 SmallVector<int, 8> MaskV;
Nate Begeman2c87c422009-02-23 08:49:38 +00003534 for (unsigned i = 0; i != 4; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003535 MaskV.push_back(i);
Nate Begeman2c87c422009-02-23 08:49:38 +00003536 for (unsigned i = 4; i != 8; ++i) {
3537 int idx = MaskVals[i];
3538 if (idx < 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00003539 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00003540 InOrder.set(i);
3541 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman543d2142009-04-27 18:41:29 +00003542 MaskV.push_back((idx & 3) + 4);
Nate Begeman2c87c422009-02-23 08:49:38 +00003543 InOrder.set(i);
3544 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00003545 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00003546 }
3547 }
Nate Begeman543d2142009-04-27 18:41:29 +00003548 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3549 &MaskV[0]);
Nate Begeman2c87c422009-02-23 08:49:38 +00003550 }
3551
3552 // In case BestHi & BestLo were both -1, which means each quadword has a word
3553 // from each of the four input quadwords, calculate the InOrder bitvector now
3554 // before falling through to the insert/extract cleanup.
3555 if (BestLoQuad == -1 && BestHiQuad == -1) {
3556 NewV = V1;
3557 for (int i = 0; i != 8; ++i)
3558 if (MaskVals[i] < 0 || MaskVals[i] == i)
3559 InOrder.set(i);
3560 }
3561
3562 // The other elements are put in the right place using pextrw and pinsrw.
3563 for (unsigned i = 0; i != 8; ++i) {
3564 if (InOrder[i])
3565 continue;
3566 int EltIdx = MaskVals[i];
3567 if (EltIdx < 0)
3568 continue;
3569 SDValue ExtOp = (EltIdx < 8)
3570 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
3571 DAG.getIntPtrConstant(EltIdx))
3572 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
3573 DAG.getIntPtrConstant(EltIdx - 8));
3574 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
3575 DAG.getIntPtrConstant(i));
3576 }
3577 return NewV;
3578}
3579
3580// v16i8 shuffles - Prefer shuffles in the following order:
3581// 1. [ssse3] 1 x pshufb
3582// 2. [ssse3] 2 x pshufb + 1 x por
3583// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3584static
Nate Begeman543d2142009-04-27 18:41:29 +00003585SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3586 SelectionDAG &DAG, X86TargetLowering &TLI) {
3587 SDValue V1 = SVOp->getOperand(0);
3588 SDValue V2 = SVOp->getOperand(1);
3589 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman2c87c422009-02-23 08:49:38 +00003590 SmallVector<int, 16> MaskVals;
Nate Begeman543d2142009-04-27 18:41:29 +00003591 SVOp->getMask(MaskVals);
Nate Begeman2c87c422009-02-23 08:49:38 +00003592
3593 // If we have SSSE3, case 1 is generated when all result bytes come from
3594 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
3595 // present, fall back to case 3.
3596 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3597 bool V1Only = true;
3598 bool V2Only = true;
3599 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003600 int EltIdx = MaskVals[i];
Nate Begeman2c87c422009-02-23 08:49:38 +00003601 if (EltIdx < 0)
3602 continue;
3603 if (EltIdx < 16)
3604 V2Only = false;
3605 else
3606 V1Only = false;
3607 }
3608
3609 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
3610 if (TLI.getSubtarget()->hasSSSE3()) {
3611 SmallVector<SDValue,16> pshufbMask;
3612
3613 // If all result elements are from one input vector, then only translate
3614 // undef mask values to 0x80 (zero out result) in the pshufb mask.
3615 //
3616 // Otherwise, we have elements from both input vectors, and must zero out
3617 // elements that come from V2 in the first mask, and V1 in the second mask
3618 // so that we can OR them together.
3619 bool TwoInputs = !(V1Only || V2Only);
3620 for (unsigned i = 0; i != 16; ++i) {
3621 int EltIdx = MaskVals[i];
3622 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
3623 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3624 continue;
3625 }
3626 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3627 }
3628 // If all the elements are from V2, assign it to V1 and return after
3629 // building the first pshufb.
3630 if (V2Only)
3631 V1 = V2;
3632 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Cheng907a2d22009-02-25 22:49:59 +00003633 DAG.getNode(ISD::BUILD_VECTOR, dl,
3634 MVT::v16i8, &pshufbMask[0], 16));
Nate Begeman2c87c422009-02-23 08:49:38 +00003635 if (!TwoInputs)
3636 return V1;
3637
3638 // Calculate the shuffle mask for the second input, shuffle it, and
3639 // OR it with the first shuffled input.
3640 pshufbMask.clear();
3641 for (unsigned i = 0; i != 16; ++i) {
3642 int EltIdx = MaskVals[i];
3643 if (EltIdx < 16) {
3644 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3645 continue;
3646 }
3647 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3648 }
3649 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Cheng907a2d22009-02-25 22:49:59 +00003650 DAG.getNode(ISD::BUILD_VECTOR, dl,
3651 MVT::v16i8, &pshufbMask[0], 16));
Nate Begeman2c87c422009-02-23 08:49:38 +00003652 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3653 }
3654
3655 // No SSSE3 - Calculate in place words and then fix all out of place words
3656 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
3657 // the 16 different words that comprise the two doublequadword input vectors.
3658 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3659 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
3660 SDValue NewV = V2Only ? V2 : V1;
3661 for (int i = 0; i != 8; ++i) {
3662 int Elt0 = MaskVals[i*2];
3663 int Elt1 = MaskVals[i*2+1];
3664
3665 // This word of the result is all undef, skip it.
3666 if (Elt0 < 0 && Elt1 < 0)
3667 continue;
3668
3669 // This word of the result is already in the correct place, skip it.
3670 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
3671 continue;
3672 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
3673 continue;
3674
3675 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
3676 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
3677 SDValue InsElt;
Mon P Wangd0cec7a2009-03-11 18:47:57 +00003678
3679 // If Elt0 and Elt1 are defined, are consecutive, and can be load
3680 // using a single extract together, load it and store it.
3681 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
3682 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3683 DAG.getIntPtrConstant(Elt1 / 2));
3684 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3685 DAG.getIntPtrConstant(i));
3686 continue;
3687 }
3688
Nate Begeman2c87c422009-02-23 08:49:38 +00003689 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wangd0cec7a2009-03-11 18:47:57 +00003690 // source byte is not also odd, shift the extracted word left 8 bits
3691 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begeman2c87c422009-02-23 08:49:38 +00003692 if (Elt1 >= 0) {
3693 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3694 DAG.getIntPtrConstant(Elt1 / 2));
3695 if ((Elt1 & 1) == 0)
3696 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
3697 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wangd0cec7a2009-03-11 18:47:57 +00003698 else if (Elt0 >= 0)
3699 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
3700 DAG.getConstant(0xFF00, MVT::i16));
Nate Begeman2c87c422009-02-23 08:49:38 +00003701 }
3702 // If Elt0 is defined, extract it from the appropriate source. If the
3703 // source byte is not also even, shift the extracted word right 8 bits. If
3704 // Elt1 was also defined, OR the extracted values together before
3705 // inserting them in the result.
3706 if (Elt0 >= 0) {
3707 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
3708 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
3709 if ((Elt0 & 1) != 0)
3710 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
3711 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wangd0cec7a2009-03-11 18:47:57 +00003712 else if (Elt1 >= 0)
3713 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
3714 DAG.getConstant(0x00FF, MVT::i16));
Nate Begeman2c87c422009-02-23 08:49:38 +00003715 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
3716 : InsElt0;
3717 }
3718 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3719 DAG.getIntPtrConstant(i));
3720 }
3721 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng75184a92007-12-11 01:46:18 +00003722}
3723
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003724/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3725/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3726/// done when every pair / quad of shuffle mask elements point to elements in
3727/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003728/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3729static
Nate Begeman543d2142009-04-27 18:41:29 +00003730SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
3731 SelectionDAG &DAG,
3732 TargetLowering &TLI, DebugLoc dl) {
3733 MVT VT = SVOp->getValueType(0);
3734 SDValue V1 = SVOp->getOperand(0);
3735 SDValue V2 = SVOp->getOperand(1);
3736 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003737 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003738 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003739 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003740 MVT NewVT = MaskVT;
3741 switch (VT.getSimpleVT()) {
3742 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003743 case MVT::v4f32: NewVT = MVT::v2f64; break;
3744 case MVT::v4i32: NewVT = MVT::v2i64; break;
3745 case MVT::v8i16: NewVT = MVT::v4i32; break;
3746 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003747 }
3748
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003749 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003750 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003751 NewVT = MVT::v2i64;
3752 else
3753 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003754 }
Nate Begeman543d2142009-04-27 18:41:29 +00003755 int Scale = NumElems / NewWidth;
3756 SmallVector<int, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003757 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman543d2142009-04-27 18:41:29 +00003758 int StartIdx = -1;
3759 for (int j = 0; j < Scale; ++j) {
3760 int EltIdx = SVOp->getMaskElt(i+j);
3761 if (EltIdx < 0)
Evan Cheng75184a92007-12-11 01:46:18 +00003762 continue;
Nate Begeman543d2142009-04-27 18:41:29 +00003763 if (StartIdx == -1)
Evan Cheng75184a92007-12-11 01:46:18 +00003764 StartIdx = EltIdx - (EltIdx % Scale);
3765 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003766 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003767 }
Nate Begeman543d2142009-04-27 18:41:29 +00003768 if (StartIdx == -1)
3769 MaskVec.push_back(-1);
Evan Cheng75184a92007-12-11 01:46:18 +00003770 else
Nate Begeman543d2142009-04-27 18:41:29 +00003771 MaskVec.push_back(StartIdx / Scale);
Evan Chengfca29242007-12-07 08:07:39 +00003772 }
3773
Dale Johannesence0805b2009-02-03 19:33:06 +00003774 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3775 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman543d2142009-04-27 18:41:29 +00003776 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Chengfca29242007-12-07 08:07:39 +00003777}
3778
Evan Chenge9b9c672008-05-09 21:53:03 +00003779/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003780///
Dan Gohman8181bd12008-07-27 21:46:04 +00003781static SDValue getVZextMovL(MVT VT, MVT OpVT,
Nate Begeman543d2142009-04-27 18:41:29 +00003782 SDValue SrcOp, SelectionDAG &DAG,
3783 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003784 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3785 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003786 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003787 LD = dyn_cast<LoadSDNode>(SrcOp);
3788 if (!LD) {
3789 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3790 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003791 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003792 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3793 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3794 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3795 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3796 // PR2108
3797 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesence0805b2009-02-03 19:33:06 +00003798 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3799 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3800 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3801 OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003802 SrcOp.getOperand(0)
3803 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003804 }
3805 }
3806 }
3807
Dale Johannesence0805b2009-02-03 19:33:06 +00003808 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3809 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michel91099d62009-02-17 22:15:04 +00003810 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesence0805b2009-02-03 19:33:06 +00003811 OpVT, SrcOp)));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003812}
3813
Evan Chengf50554e2008-07-22 21:13:36 +00003814/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3815/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003816static SDValue
Nate Begeman543d2142009-04-27 18:41:29 +00003817LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
3818 SDValue V1 = SVOp->getOperand(0);
3819 SDValue V2 = SVOp->getOperand(1);
3820 DebugLoc dl = SVOp->getDebugLoc();
3821 MVT VT = SVOp->getValueType(0);
3822
Evan Chengf50554e2008-07-22 21:13:36 +00003823 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003824 Locs.resize(4);
Nate Begeman543d2142009-04-27 18:41:29 +00003825 SmallVector<int, 8> Mask1(4U, -1);
3826 SmallVector<int, 8> PermMask;
3827 SVOp->getMask(PermMask);
3828
Evan Chengf50554e2008-07-22 21:13:36 +00003829 unsigned NumHi = 0;
3830 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003831 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003832 int Idx = PermMask[i];
3833 if (Idx < 0) {
Evan Chengf50554e2008-07-22 21:13:36 +00003834 Locs[i] = std::make_pair(-1, -1);
3835 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00003836 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
3837 if (Idx < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003838 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman543d2142009-04-27 18:41:29 +00003839 Mask1[NumLo] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00003840 NumLo++;
3841 } else {
3842 Locs[i] = std::make_pair(1, NumHi);
3843 if (2+NumHi < 4)
Nate Begeman543d2142009-04-27 18:41:29 +00003844 Mask1[2+NumHi] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00003845 NumHi++;
3846 }
3847 }
3848 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003849
Evan Chengf50554e2008-07-22 21:13:36 +00003850 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003851 // If no more than two elements come from either vector. This can be
3852 // implemented with two shuffles. First shuffle gather the elements.
3853 // The second shuffle, which takes the first shuffle as both of its
3854 // vector operands, put the elements into the right order.
Nate Begeman543d2142009-04-27 18:41:29 +00003855 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00003856
Nate Begeman543d2142009-04-27 18:41:29 +00003857 SmallVector<int, 8> Mask2(4U, -1);
3858
Evan Chengf50554e2008-07-22 21:13:36 +00003859 for (unsigned i = 0; i != 4; ++i) {
3860 if (Locs[i].first == -1)
3861 continue;
3862 else {
3863 unsigned Idx = (i < 2) ? 0 : 4;
3864 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman543d2142009-04-27 18:41:29 +00003865 Mask2[i] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00003866 }
3867 }
3868
Nate Begeman543d2142009-04-27 18:41:29 +00003869 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00003870 } else if (NumLo == 3 || NumHi == 3) {
3871 // Otherwise, we must have three elements from one vector, call it X, and
3872 // one element from the other, call it Y. First, use a shufps to build an
3873 // intermediate vector with the one element from Y and the element from X
3874 // that will be in the same half in the final destination (the indexes don't
3875 // matter). Then, use a shufps to build the final vector, taking the half
3876 // containing the element from Y from the intermediate, and the other half
3877 // from X.
3878 if (NumHi == 3) {
3879 // Normalize it so the 3 elements come from V1.
Nate Begeman543d2142009-04-27 18:41:29 +00003880 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003881 std::swap(V1, V2);
3882 }
3883
3884 // Find the element from V2.
3885 unsigned HiIndex;
3886 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman543d2142009-04-27 18:41:29 +00003887 int Val = PermMask[HiIndex];
3888 if (Val < 0)
Evan Cheng3cae0332008-07-23 00:22:17 +00003889 continue;
Evan Cheng3cae0332008-07-23 00:22:17 +00003890 if (Val >= 4)
3891 break;
3892 }
3893
Nate Begeman543d2142009-04-27 18:41:29 +00003894 Mask1[0] = PermMask[HiIndex];
3895 Mask1[1] = -1;
3896 Mask1[2] = PermMask[HiIndex^1];
3897 Mask1[3] = -1;
3898 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00003899
3900 if (HiIndex >= 2) {
Nate Begeman543d2142009-04-27 18:41:29 +00003901 Mask1[0] = PermMask[0];
3902 Mask1[1] = PermMask[1];
3903 Mask1[2] = HiIndex & 1 ? 6 : 4;
3904 Mask1[3] = HiIndex & 1 ? 4 : 6;
3905 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00003906 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00003907 Mask1[0] = HiIndex & 1 ? 2 : 0;
3908 Mask1[1] = HiIndex & 1 ? 0 : 2;
3909 Mask1[2] = PermMask[2];
3910 Mask1[3] = PermMask[3];
3911 if (Mask1[2] >= 0)
3912 Mask1[2] += 4;
3913 if (Mask1[3] >= 0)
3914 Mask1[3] += 4;
3915 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00003916 }
Evan Chengf50554e2008-07-22 21:13:36 +00003917 }
3918
3919 // Break it into (shuffle shuffle_hi, shuffle_lo).
3920 Locs.clear();
Nate Begeman543d2142009-04-27 18:41:29 +00003921 SmallVector<int,8> LoMask(4U, -1);
3922 SmallVector<int,8> HiMask(4U, -1);
3923
3924 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003925 unsigned MaskIdx = 0;
3926 unsigned LoIdx = 0;
3927 unsigned HiIdx = 2;
3928 for (unsigned i = 0; i != 4; ++i) {
3929 if (i == 2) {
3930 MaskPtr = &HiMask;
3931 MaskIdx = 1;
3932 LoIdx = 0;
3933 HiIdx = 2;
3934 }
Nate Begeman543d2142009-04-27 18:41:29 +00003935 int Idx = PermMask[i];
3936 if (Idx < 0) {
Evan Chengf50554e2008-07-22 21:13:36 +00003937 Locs[i] = std::make_pair(-1, -1);
Nate Begeman543d2142009-04-27 18:41:29 +00003938 } else if (Idx < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003939 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman543d2142009-04-27 18:41:29 +00003940 (*MaskPtr)[LoIdx] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00003941 LoIdx++;
3942 } else {
3943 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman543d2142009-04-27 18:41:29 +00003944 (*MaskPtr)[HiIdx] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00003945 HiIdx++;
3946 }
3947 }
3948
Nate Begeman543d2142009-04-27 18:41:29 +00003949 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
3950 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
3951 SmallVector<int, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003952 for (unsigned i = 0; i != 4; ++i) {
3953 if (Locs[i].first == -1) {
Nate Begeman543d2142009-04-27 18:41:29 +00003954 MaskOps.push_back(-1);
Evan Chengf50554e2008-07-22 21:13:36 +00003955 } else {
3956 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman543d2142009-04-27 18:41:29 +00003957 MaskOps.push_back(Idx);
Evan Chengf50554e2008-07-22 21:13:36 +00003958 }
3959 }
Nate Begeman543d2142009-04-27 18:41:29 +00003960 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengf50554e2008-07-22 21:13:36 +00003961}
3962
Dan Gohman8181bd12008-07-27 21:46:04 +00003963SDValue
3964X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman543d2142009-04-27 18:41:29 +00003965 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00003966 SDValue V1 = Op.getOperand(0);
3967 SDValue V2 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00003968 MVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00003969 DebugLoc dl = Op.getDebugLoc();
Nate Begeman543d2142009-04-27 18:41:29 +00003970 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands92c43912008-06-06 12:08:01 +00003971 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003972 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3973 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3974 bool V1IsSplat = false;
3975 bool V2IsSplat = false;
3976
Nate Begeman543d2142009-04-27 18:41:29 +00003977 if (isZeroShuffle(SVOp))
Dale Johannesence0805b2009-02-03 19:33:06 +00003978 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003979
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00003980 // Canonicalize movddup shuffles.
Nate Begeman543d2142009-04-27 18:41:29 +00003981 if (V2IsUndef && Subtarget->hasSSE2() && VT.getSizeInBits() == 128 &&
3982 X86::isMOVDDUPMask(SVOp))
3983 return CanonicalizeMovddup(SVOp, DAG, Subtarget->hasSSE3());
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00003984
Nate Begeman543d2142009-04-27 18:41:29 +00003985 // Promote splats to v4f32.
3986 if (SVOp->isSplat()) {
3987 if (isMMX || NumElems < 4)
3988 return Op;
3989 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003990 }
3991
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003992 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3993 // do it!
3994 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman543d2142009-04-27 18:41:29 +00003995 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00003996 if (NewOp.getNode())
Scott Michel91099d62009-02-17 22:15:04 +00003997 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesence0805b2009-02-03 19:33:06 +00003998 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003999 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4000 // FIXME: Figure out a cleaner way to do this.
4001 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004002 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman543d2142009-04-27 18:41:29 +00004003 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004004 if (NewOp.getNode()) {
Nate Begeman543d2142009-04-27 18:41:29 +00004005 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4006 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4007 DAG, Subtarget, dl);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004008 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004009 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman543d2142009-04-27 18:41:29 +00004010 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4011 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chenge9b9c672008-05-09 21:53:03 +00004012 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman543d2142009-04-27 18:41:29 +00004013 DAG, Subtarget, dl);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004014 }
4015 }
Nate Begeman543d2142009-04-27 18:41:29 +00004016
4017 if (X86::isPSHUFDMask(SVOp))
4018 return Op;
4019
Evan Chengdea99362008-05-29 08:22:04 +00004020 // Check if this can be converted into a logical shift.
4021 bool isLeft = false;
4022 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004023 SDValue ShVal;
Nate Begeman543d2142009-04-27 18:41:29 +00004024 bool isShift = getSubtarget()->hasSSE2() &&
4025 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengdea99362008-05-29 08:22:04 +00004026 if (isShift && ShVal.hasOneUse()) {
Scott Michel91099d62009-02-17 22:15:04 +00004027 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengdea99362008-05-29 08:22:04 +00004028 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004029 MVT EVT = VT.getVectorElementType();
4030 ShAmt *= EVT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004031 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00004032 }
Nate Begeman543d2142009-04-27 18:41:29 +00004033
4034 if (X86::isMOVLMask(SVOp)) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004035 if (V1IsUndef)
4036 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004037 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00004038 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004039 if (!isMMX)
4040 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004041 }
Nate Begeman543d2142009-04-27 18:41:29 +00004042
4043 // FIXME: fold these into legal mask.
4044 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4045 X86::isMOVSLDUPMask(SVOp) ||
4046 X86::isMOVHLPSMask(SVOp) ||
4047 X86::isMOVHPMask(SVOp) ||
4048 X86::isMOVLPMask(SVOp)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004049 return Op;
4050
Nate Begeman543d2142009-04-27 18:41:29 +00004051 if (ShouldXformToMOVHLPS(SVOp) ||
4052 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4053 return CommuteVectorShuffle(SVOp, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004054
Evan Chengdea99362008-05-29 08:22:04 +00004055 if (isShift) {
4056 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004057 MVT EVT = VT.getVectorElementType();
4058 ShAmt *= EVT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004059 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00004060 }
Nate Begeman543d2142009-04-27 18:41:29 +00004061
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004062 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004063 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4064 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004065 V1IsSplat = isSplatVector(V1.getNode());
4066 V2IsSplat = isSplatVector(V2.getNode());
Scott Michel91099d62009-02-17 22:15:04 +00004067
Chris Lattnere6aa3862007-11-25 00:24:49 +00004068 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004069 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman543d2142009-04-27 18:41:29 +00004070 Op = CommuteVectorShuffle(SVOp, DAG);
4071 SVOp = cast<ShuffleVectorSDNode>(Op);
4072 V1 = SVOp->getOperand(0);
4073 V2 = SVOp->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004074 std::swap(V1IsSplat, V2IsSplat);
4075 std::swap(V1IsUndef, V2IsUndef);
4076 Commuted = true;
4077 }
4078
Nate Begeman543d2142009-04-27 18:41:29 +00004079 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4080 // Shuffling low element of v1 into undef, just return v1.
4081 if (V2IsUndef)
4082 return V1;
4083 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4084 // the instruction selector will not match, so get a canonical MOVL with
4085 // swapped operands to undo the commute.
4086 return getMOVL(DAG, dl, VT, V2, V1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004087 }
4088
Nate Begeman543d2142009-04-27 18:41:29 +00004089 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4090 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4091 X86::isUNPCKLMask(SVOp) ||
4092 X86::isUNPCKHMask(SVOp))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004093 return Op;
4094
4095 if (V2IsSplat) {
4096 // Normalize mask so all entries that point to V2 points to its first
4097 // element then try to match unpck{h|l} again. If match, return a
4098 // new vector_shuffle with the corrected mask.
Nate Begeman543d2142009-04-27 18:41:29 +00004099 SDValue NewMask = NormalizeMask(SVOp, DAG);
4100 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4101 if (NSVOp != SVOp) {
4102 if (X86::isUNPCKLMask(NSVOp, true)) {
4103 return NewMask;
4104 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4105 return NewMask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004106 }
4107 }
4108 }
4109
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004110 if (Commuted) {
4111 // Commute is back and try unpck* again.
Nate Begeman543d2142009-04-27 18:41:29 +00004112 // FIXME: this seems wrong.
4113 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4114 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4115 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4116 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4117 X86::isUNPCKLMask(NewSVOp) ||
4118 X86::isUNPCKHMask(NewSVOp))
4119 return NewOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004120 }
4121
Nate Begeman2c87c422009-02-23 08:49:38 +00004122 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman543d2142009-04-27 18:41:29 +00004123
4124 // Normalize the node to match x86 shuffle ops if needed
4125 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4126 return CommuteVectorShuffle(SVOp, DAG);
4127
4128 // Check for legal shuffle and return?
4129 SmallVector<int, 16> PermMask;
4130 SVOp->getMask(PermMask);
4131 if (isShuffleMaskLegal(PermMask, VT))
Evan Chengbf8b2c52008-04-05 00:30:36 +00004132 return Op;
Nate Begeman543d2142009-04-27 18:41:29 +00004133
Evan Cheng75184a92007-12-11 01:46:18 +00004134 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4135 if (VT == MVT::v8i16) {
Nate Begeman543d2142009-04-27 18:41:29 +00004136 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004137 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004138 return NewOp;
4139 }
4140
Nate Begeman2c87c422009-02-23 08:49:38 +00004141 if (VT == MVT::v16i8) {
Nate Begeman543d2142009-04-27 18:41:29 +00004142 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begeman2c87c422009-02-23 08:49:38 +00004143 if (NewOp.getNode())
4144 return NewOp;
4145 }
4146
Evan Chengf50554e2008-07-22 21:13:36 +00004147 // Handle all 4 wide cases with a number of shuffles except for MMX.
4148 if (NumElems == 4 && !isMMX)
Nate Begeman543d2142009-04-27 18:41:29 +00004149 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004150
Dan Gohman8181bd12008-07-27 21:46:04 +00004151 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004152}
4153
Dan Gohman8181bd12008-07-27 21:46:04 +00004154SDValue
4155X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004156 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004157 MVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004158 DebugLoc dl = Op.getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00004159 if (VT.getSizeInBits() == 8) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004160 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004161 Op.getOperand(0), Op.getOperand(1));
Dale Johannesence0805b2009-02-03 19:33:06 +00004162 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004163 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004164 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004165 } else if (VT.getSizeInBits() == 16) {
Evan Chengf9393b32009-01-02 05:29:08 +00004166 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4167 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4168 if (Idx == 0)
Dale Johannesence0805b2009-02-03 19:33:06 +00004169 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4170 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4171 DAG.getNode(ISD::BIT_CONVERT, dl,
4172 MVT::v4i32,
Evan Chengf9393b32009-01-02 05:29:08 +00004173 Op.getOperand(0)),
4174 Op.getOperand(1)));
Dale Johannesence0805b2009-02-03 19:33:06 +00004175 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004176 Op.getOperand(0), Op.getOperand(1));
Dale Johannesence0805b2009-02-03 19:33:06 +00004177 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004178 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004179 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004180 } else if (VT == MVT::f32) {
4181 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4182 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004183 // result has a single use which is a store or a bitcast to i32. And in
4184 // the case of a store, it's not worth it if the index is a constant 0,
4185 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004186 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004187 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004188 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004189 if ((User->getOpcode() != ISD::STORE ||
4190 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4191 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004192 (User->getOpcode() != ISD::BIT_CONVERT ||
4193 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004194 return SDValue();
Dale Johannesence0805b2009-02-03 19:33:06 +00004195 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michel91099d62009-02-17 22:15:04 +00004196 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesence0805b2009-02-03 19:33:06 +00004197 Op.getOperand(0)),
4198 Op.getOperand(1));
4199 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004200 } else if (VT == MVT::i32) {
4201 // ExtractPS works with constant index.
4202 if (isa<ConstantSDNode>(Op.getOperand(1)))
4203 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004204 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004205 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004206}
4207
4208
Dan Gohman8181bd12008-07-27 21:46:04 +00004209SDValue
4210X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004211 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004212 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004213
Evan Cheng6c249332008-03-24 21:52:23 +00004214 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004215 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004216 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004217 return Res;
4218 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004219
Duncan Sands92c43912008-06-06 12:08:01 +00004220 MVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004221 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004222 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004223 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004224 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004225 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004226 if (Idx == 0)
Dale Johannesence0805b2009-02-03 19:33:06 +00004227 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4228 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michel91099d62009-02-17 22:15:04 +00004229 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesence0805b2009-02-03 19:33:06 +00004230 MVT::v4i32, Vec),
Evan Cheng75184a92007-12-11 01:46:18 +00004231 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004232 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004233 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesence0805b2009-02-03 19:33:06 +00004234 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004235 Op.getOperand(0), Op.getOperand(1));
Dale Johannesence0805b2009-02-03 19:33:06 +00004236 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004237 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004238 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004239 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004240 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004241 if (Idx == 0)
4242 return Op;
Nate Begeman543d2142009-04-27 18:41:29 +00004243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004244 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman543d2142009-04-27 18:41:29 +00004245 int Mask[4] = { Idx, -1, -1, -1 };
4246 MVT VVT = Op.getOperand(0).getValueType();
4247 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4248 DAG.getUNDEF(VVT), Mask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004249 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004250 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004251 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004252 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4253 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4254 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004255 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004256 if (Idx == 0)
4257 return Op;
4258
4259 // UNPCKHPD the element to the lowest double word, then movsd.
4260 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4261 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman543d2142009-04-27 18:41:29 +00004262 int Mask[2] = { 1, -1 };
4263 MVT VVT = Op.getOperand(0).getValueType();
4264 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4265 DAG.getUNDEF(VVT), Mask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004266 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004267 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004268 }
4269
Dan Gohman8181bd12008-07-27 21:46:04 +00004270 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004271}
4272
Dan Gohman8181bd12008-07-27 21:46:04 +00004273SDValue
4274X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004275 MVT VT = Op.getValueType();
4276 MVT EVT = VT.getVectorElementType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004277 DebugLoc dl = Op.getDebugLoc();
Nate Begemand77e59e2008-02-11 04:19:36 +00004278
Dan Gohman8181bd12008-07-27 21:46:04 +00004279 SDValue N0 = Op.getOperand(0);
4280 SDValue N1 = Op.getOperand(1);
4281 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004282
Dan Gohman5a7af042008-08-14 22:53:18 +00004283 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4284 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004285 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begeman2c87c422009-02-23 08:49:38 +00004286 : X86ISD::PINSRW;
Nate Begemand77e59e2008-02-11 04:19:36 +00004287 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4288 // argument.
4289 if (N1.getValueType() != MVT::i32)
Dale Johannesence0805b2009-02-03 19:33:06 +00004290 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begemand77e59e2008-02-11 04:19:36 +00004291 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004292 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesence0805b2009-02-03 19:33:06 +00004293 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004294 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004295 // Bits [7:6] of the constant are the source select. This will always be
4296 // zero here. The DAG Combiner may combine an extract_elt index into these
4297 // bits. For example (insert (extract, 3), 2) could be matched by putting
4298 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michel91099d62009-02-17 22:15:04 +00004299 // Bits [5:4] of the constant are the destination select. This is the
Nate Begemand77e59e2008-02-11 04:19:36 +00004300 // value of the incoming immediate.
Scott Michel91099d62009-02-17 22:15:04 +00004301 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begemand77e59e2008-02-11 04:19:36 +00004302 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004303 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00004304 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004305 } else if (EVT == MVT::i32) {
4306 // InsertPS works with constant index.
4307 if (isa<ConstantSDNode>(N2))
4308 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004309 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004310 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004311}
4312
Dan Gohman8181bd12008-07-27 21:46:04 +00004313SDValue
4314X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004315 MVT VT = Op.getValueType();
4316 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004317
4318 if (Subtarget->hasSSE41())
4319 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4320
Evan Chenge12a7eb2007-12-12 07:55:34 +00004321 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004322 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004323
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004324 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00004325 SDValue N0 = Op.getOperand(0);
4326 SDValue N1 = Op.getOperand(1);
4327 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004328
Duncan Sands92c43912008-06-06 12:08:01 +00004329 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004330 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4331 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004332 if (N1.getValueType() != MVT::i32)
Dale Johannesence0805b2009-02-03 19:33:06 +00004333 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004334 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004335 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesence0805b2009-02-03 19:33:06 +00004336 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004337 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004338 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004339}
4340
Dan Gohman8181bd12008-07-27 21:46:04 +00004341SDValue
4342X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004343 DebugLoc dl = Op.getDebugLoc();
Evan Cheng759fe022008-07-22 18:39:19 +00004344 if (Op.getValueType() == MVT::v2f32)
Dale Johannesence0805b2009-02-03 19:33:06 +00004345 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4346 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4347 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng759fe022008-07-22 18:39:19 +00004348 Op.getOperand(0))));
4349
Dale Johannesence0805b2009-02-03 19:33:06 +00004350 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004351 MVT VT = MVT::v2i32;
4352 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004353 default: break;
4354 case MVT::v16i8:
4355 case MVT::v8i16:
4356 VT = MVT::v4i32;
4357 break;
4358 }
Dale Johannesence0805b2009-02-03 19:33:06 +00004359 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4360 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004361}
4362
Bill Wendlingfef06052008-09-16 21:48:12 +00004363// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4364// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4365// one of the above mentioned nodes. It has to be wrapped because otherwise
4366// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4367// be used to form addressing mode. These wrapped nodes will be selected
4368// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004369SDValue
4370X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004371 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dale Johannesen175fdef2009-02-06 21:50:26 +00004372 // FIXME there isn't really any debug info here, should come from the parent
4373 DebugLoc dl = CP->getDebugLoc();
Evan Cheng68c18682009-03-13 07:51:59 +00004374 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
4375 CP->getAlignment());
Dale Johannesen24dd9a52009-02-07 00:55:49 +00004376 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004377 // With PIC, the address is actually $g + Offset.
4378 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4379 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesen175fdef2009-02-06 21:50:26 +00004380 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen24dd9a52009-02-07 00:55:49 +00004381 DAG.getNode(X86ISD::GlobalBaseReg,
4382 DebugLoc::getUnknownLoc(),
4383 getPointerTy()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004384 Result);
4385 }
4386
4387 return Result;
4388}
4389
Dan Gohman8181bd12008-07-27 21:46:04 +00004390SDValue
Dale Johannesenea996922009-02-04 20:06:27 +00004391X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman36322c72008-10-18 02:06:02 +00004392 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00004393 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00004394 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4395 bool ExtraLoadRequired =
4396 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4397
4398 // Create the TargetGlobalAddress node, folding in the constant
4399 // offset if it is legal.
4400 SDValue Result;
Dan Gohman3d5257c2008-10-21 03:38:42 +00004401 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +00004402 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4403 Offset = 0;
4404 } else
4405 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dale Johannesen24dd9a52009-02-07 00:55:49 +00004406 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00004407
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408 // With PIC, the address is actually $g + Offset.
Dan Gohman36322c72008-10-18 02:06:02 +00004409 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dale Johannesenea996922009-02-04 20:06:27 +00004410 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4411 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004412 Result);
4413 }
Scott Michel91099d62009-02-17 22:15:04 +00004414
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004415 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4416 // load the value at address GV, not the value of GV itself. This means that
4417 // the GlobalAddress must be in the base or index register of the address, not
4418 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4419 // The same applies for external symbols during PIC codegen
Dan Gohman36322c72008-10-18 02:06:02 +00004420 if (ExtraLoadRequired)
Dale Johannesenea996922009-02-04 20:06:27 +00004421 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004422 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423
Dan Gohman36322c72008-10-18 02:06:02 +00004424 // If there was a non-zero offset that we didn't fold, create an explicit
4425 // addition for it.
4426 if (Offset != 0)
Dale Johannesenea996922009-02-04 20:06:27 +00004427 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman36322c72008-10-18 02:06:02 +00004428 DAG.getConstant(Offset, getPointerTy()));
4429
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004430 return Result;
4431}
4432
Evan Cheng7f250d62008-09-24 00:05:32 +00004433SDValue
4434X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4435 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00004436 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004437 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00004438}
4439
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00004440static SDValue
4441GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Rafael Espindola7fc4b8d2009-04-24 12:59:40 +00004442 SDValue *InFlag, const MVT PtrVT, unsigned ReturnReg) {
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00004443 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4444 DebugLoc dl = GA->getDebugLoc();
4445 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4446 GA->getValueType(0),
4447 GA->getOffset());
4448 if (InFlag) {
4449 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola7fc4b8d2009-04-24 12:59:40 +00004450 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00004451 } else {
4452 SDValue Ops[] = { Chain, TGA };
Rafael Espindola7fc4b8d2009-04-24 12:59:40 +00004453 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00004454 }
Rafael Espindola7fc4b8d2009-04-24 12:59:40 +00004455 SDValue Flag = Chain.getValue(1);
4456 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00004457}
4458
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004459// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004460static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004461LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004462 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004463 SDValue InFlag;
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00004464 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4465 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004466 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesen24dd9a52009-02-07 00:55:49 +00004467 DebugLoc::getUnknownLoc(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004468 PtrVT), InFlag);
4469 InFlag = Chain.getValue(1);
4470
Rafael Espindola7fc4b8d2009-04-24 12:59:40 +00004471 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004472}
4473
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004474// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004475static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004476LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004477 const MVT PtrVT) {
Rafael Espindola7fc4b8d2009-04-24 12:59:40 +00004478 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004479}
4480
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004481// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4482// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004483static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Rafael Espindolab93a5122009-04-13 13:02:49 +00004484 const MVT PtrVT, TLSModel::Model model,
4485 bool is64Bit) {
Dale Johannesenea996922009-02-04 20:06:27 +00004486 DebugLoc dl = GA->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004487 // Get the Thread Pointer
Rafael Espindolabca99f72009-04-08 21:14:34 +00004488 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
4489 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindolab93a5122009-04-13 13:02:49 +00004490 DAG.getRegister(is64Bit? X86::FS : X86::GS,
4491 MVT::i32));
Rafael Espindolabca99f72009-04-08 21:14:34 +00004492
4493 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
4494 NULL, 0);
4495
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004496 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4497 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004498 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004499 GA->getValueType(0),
4500 GA->getOffset());
Dale Johannesen24dd9a52009-02-07 00:55:49 +00004501 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004502
Rafael Espindola7b620af2009-02-27 13:37:18 +00004503 if (model == TLSModel::InitialExec)
Dale Johannesenea996922009-02-04 20:06:27 +00004504 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004505 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004506
4507 // The address of the thread local variable is the add of the thread
4508 // pointer with the offset of the variable.
Dale Johannesenea996922009-02-04 20:06:27 +00004509 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004510}
4511
Dan Gohman8181bd12008-07-27 21:46:04 +00004512SDValue
4513X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004514 // TODO: implement the "local dynamic" model
4515 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004516 assert(Subtarget->isTargetELF() &&
4517 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004518 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Rafael Espindola7b620af2009-02-27 13:37:18 +00004519 GlobalValue *GV = GA->getGlobal();
4520 TLSModel::Model model =
4521 getTLSModel (GV, getTargetMachine().getRelocationModel());
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004522 if (Subtarget->is64Bit()) {
Rafael Espindola7b620af2009-02-27 13:37:18 +00004523 switch (model) {
4524 case TLSModel::GeneralDynamic:
4525 case TLSModel::LocalDynamic: // not implemented
Rafael Espindola7b620af2009-02-27 13:37:18 +00004526 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Rafael Espindolab93a5122009-04-13 13:02:49 +00004527
4528 case TLSModel::InitialExec:
4529 case TLSModel::LocalExec:
4530 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, true);
Rafael Espindola7b620af2009-02-27 13:37:18 +00004531 }
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004532 } else {
Rafael Espindola7b620af2009-02-27 13:37:18 +00004533 switch (model) {
4534 case TLSModel::GeneralDynamic:
4535 case TLSModel::LocalDynamic: // not implemented
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004536 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Rafael Espindola7b620af2009-02-27 13:37:18 +00004537
4538 case TLSModel::InitialExec:
4539 case TLSModel::LocalExec:
Rafael Espindolab93a5122009-04-13 13:02:49 +00004540 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, false);
Rafael Espindola7b620af2009-02-27 13:37:18 +00004541 }
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004542 }
Chris Lattnerda028df2009-04-01 22:14:45 +00004543 assert(0 && "Unreachable");
4544 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004545}
4546
Dan Gohman8181bd12008-07-27 21:46:04 +00004547SDValue
4548X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen175fdef2009-02-06 21:50:26 +00004549 // FIXME there isn't really any debug info here
4550 DebugLoc dl = Op.getDebugLoc();
Bill Wendlingfef06052008-09-16 21:48:12 +00004551 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4552 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dale Johannesen24dd9a52009-02-07 00:55:49 +00004553 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004554 // With PIC, the address is actually $g + Offset.
4555 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4556 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesen175fdef2009-02-06 21:50:26 +00004557 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Scott Michel91099d62009-02-17 22:15:04 +00004558 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesen24dd9a52009-02-07 00:55:49 +00004559 DebugLoc::getUnknownLoc(),
4560 getPointerTy()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004561 Result);
4562 }
4563
4564 return Result;
4565}
4566
Dan Gohman8181bd12008-07-27 21:46:04 +00004567SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004568 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dale Johannesen175fdef2009-02-06 21:50:26 +00004569 // FIXME there isn't really any debug into here
4570 DebugLoc dl = JT->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00004571 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dale Johannesen24dd9a52009-02-07 00:55:49 +00004572 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004573 // With PIC, the address is actually $g + Offset.
4574 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4575 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesen175fdef2009-02-06 21:50:26 +00004576 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen24dd9a52009-02-07 00:55:49 +00004577 DAG.getNode(X86ISD::GlobalBaseReg,
4578 DebugLoc::getUnknownLoc(),
4579 getPointerTy()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004580 Result);
4581 }
4582
4583 return Result;
4584}
4585
Chris Lattner62814a32007-10-17 06:02:13 +00004586/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michel91099d62009-02-17 22:15:04 +00004587/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004588SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004589 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004590 MVT VT = Op.getValueType();
4591 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004592 DebugLoc dl = Op.getDebugLoc();
Chris Lattner62814a32007-10-17 06:02:13 +00004593 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004594 SDValue ShOpLo = Op.getOperand(0);
4595 SDValue ShOpHi = Op.getOperand(1);
4596 SDValue ShAmt = Op.getOperand(2);
4597 SDValue Tmp1 = isSRA ?
Scott Michel91099d62009-02-17 22:15:04 +00004598 DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Dale Johannesence0805b2009-02-03 19:33:06 +00004599 DAG.getConstant(VTBits - 1, MVT::i8)) :
Dan Gohman092014e2008-03-03 22:22:09 +00004600 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004601
Dan Gohman8181bd12008-07-27 21:46:04 +00004602 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004603 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004604 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4605 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004606 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00004607 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4608 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004609 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004610
Dale Johannesence0805b2009-02-03 19:33:06 +00004611 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004612 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesence0805b2009-02-03 19:33:06 +00004613 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004614 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004615
Dan Gohman8181bd12008-07-27 21:46:04 +00004616 SDValue Hi, Lo;
4617 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4618 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4619 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004620
Chris Lattner62814a32007-10-17 06:02:13 +00004621 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004622 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4623 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004624 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00004625 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4626 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004627 }
4628
Dan Gohman8181bd12008-07-27 21:46:04 +00004629 SDValue Ops[2] = { Lo, Hi };
Dale Johannesence0805b2009-02-03 19:33:06 +00004630 return DAG.getMergeValues(Ops, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004631}
4632
Dan Gohman8181bd12008-07-27 21:46:04 +00004633SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004634 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004635 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004636 "Unknown SINT_TO_FP to lower!");
Scott Michel91099d62009-02-17 22:15:04 +00004637
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004638 // These are really Legal; caller falls through into that case.
4639 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004640 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00004641 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004642 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004643 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00004644
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004645 DebugLoc dl = Op.getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00004646 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004647 MachineFunction &MF = DAG.getMachineFunction();
4648 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004649 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00004650 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling6b42d012009-03-13 08:41:47 +00004651 StackSlot,
4652 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004653
4654 // Build the FILD
4655 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004656 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004657 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004658 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4659 else
4660 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004661 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004662 Ops.push_back(Chain);
4663 Ops.push_back(StackSlot);
4664 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004665 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004666 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004667
Dale Johannesen2fc20782007-09-14 22:26:36 +00004668 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004669 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004670 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004671
4672 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4673 // shouldn't be necessary except that RFP cannot be live across
4674 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4675 MachineFunction &MF = DAG.getMachineFunction();
4676 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004677 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004678 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004679 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004680 Ops.push_back(Chain);
4681 Ops.push_back(Result);
4682 Ops.push_back(StackSlot);
4683 Ops.push_back(DAG.getValueType(Op.getValueType()));
4684 Ops.push_back(InFlag);
Dale Johannesence0805b2009-02-03 19:33:06 +00004685 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4686 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004687 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004688 }
4689
4690 return Result;
4691}
4692
Bill Wendling14a30ef2009-01-17 03:56:04 +00004693// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4694SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4695 // This algorithm is not obvious. Here it is in C code, more or less:
4696 /*
4697 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4698 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4699 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesenfb019af2008-10-21 23:07:49 +00004700
Bill Wendling14a30ef2009-01-17 03:56:04 +00004701 // Copy ints to xmm registers.
4702 __m128i xh = _mm_cvtsi32_si128( hi );
4703 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004704
Bill Wendling14a30ef2009-01-17 03:56:04 +00004705 // Combine into low half of a single xmm register.
4706 __m128i x = _mm_unpacklo_epi32( xh, xl );
4707 __m128d d;
4708 double sd;
Dale Johannesenfb019af2008-10-21 23:07:49 +00004709
Bill Wendling14a30ef2009-01-17 03:56:04 +00004710 // Merge in appropriate exponents to give the integer bits the right
4711 // magnitude.
4712 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004713
Bill Wendling14a30ef2009-01-17 03:56:04 +00004714 // Subtract away the biases to deal with the IEEE-754 double precision
4715 // implicit 1.
4716 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004717
Bill Wendling14a30ef2009-01-17 03:56:04 +00004718 // All conversions up to here are exact. The correctly rounded result is
4719 // calculated using the current rounding mode using the following
4720 // horizontal add.
4721 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4722 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4723 // store doesn't really need to be here (except
4724 // maybe to zero the other double)
4725 return sd;
4726 }
4727 */
Dale Johannesenfb019af2008-10-21 23:07:49 +00004728
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004729 DebugLoc dl = Op.getDebugLoc();
Dale Johannesence0805b2009-02-03 19:33:06 +00004730
Dale Johannesena359b8b2008-10-21 20:50:01 +00004731 // Build some magic constants.
Bill Wendling14a30ef2009-01-17 03:56:04 +00004732 std::vector<Constant*> CV0;
Dale Johannesena359b8b2008-10-21 20:50:01 +00004733 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4734 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4735 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4736 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4737 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng68c18682009-03-13 07:51:59 +00004738 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesena359b8b2008-10-21 20:50:01 +00004739
Bill Wendling14a30ef2009-01-17 03:56:04 +00004740 std::vector<Constant*> CV1;
Dale Johannesena359b8b2008-10-21 20:50:01 +00004741 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4742 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4743 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng68c18682009-03-13 07:51:59 +00004744 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesena359b8b2008-10-21 20:50:01 +00004745
Dale Johannesence0805b2009-02-03 19:33:06 +00004746 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4747 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004748 Op.getOperand(0),
4749 DAG.getIntPtrConstant(1)));
Dale Johannesence0805b2009-02-03 19:33:06 +00004750 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4751 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004752 Op.getOperand(0),
4753 DAG.getIntPtrConstant(0)));
Nate Begeman543d2142009-04-27 18:41:29 +00004754 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
Dale Johannesence0805b2009-02-03 19:33:06 +00004755 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004756 PseudoSourceValue::getConstantPool(), 0,
4757 false, 16);
Nate Begeman543d2142009-04-27 18:41:29 +00004758 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Dale Johannesence0805b2009-02-03 19:33:06 +00004759 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4760 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004761 PseudoSourceValue::getConstantPool(), 0,
4762 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00004763 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling14a30ef2009-01-17 03:56:04 +00004764
Dale Johannesena359b8b2008-10-21 20:50:01 +00004765 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman543d2142009-04-27 18:41:29 +00004766 int ShufMask[2] = { 1, -1 };
4767 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
4768 DAG.getUNDEF(MVT::v2f64), ShufMask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004769 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4770 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesena359b8b2008-10-21 20:50:01 +00004771 DAG.getIntPtrConstant(0));
4772}
4773
Bill Wendling14a30ef2009-01-17 03:56:04 +00004774// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4775SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004776 DebugLoc dl = Op.getDebugLoc();
Bill Wendling14a30ef2009-01-17 03:56:04 +00004777 // FP constant to bias correct the final result.
4778 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4779 MVT::f64);
4780
4781 // Load the 32-bit value into an XMM register.
Dale Johannesence0805b2009-02-03 19:33:06 +00004782 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4783 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004784 Op.getOperand(0),
4785 DAG.getIntPtrConstant(0)));
4786
Dale Johannesence0805b2009-02-03 19:33:06 +00004787 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4788 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling14a30ef2009-01-17 03:56:04 +00004789 DAG.getIntPtrConstant(0));
4790
4791 // Or the load with the bias.
Dale Johannesence0805b2009-02-03 19:33:06 +00004792 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4793 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4794 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Chengc0ab5e52009-01-19 08:19:57 +00004795 MVT::v2f64, Load)),
Dale Johannesence0805b2009-02-03 19:33:06 +00004796 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4797 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Chengc0ab5e52009-01-19 08:19:57 +00004798 MVT::v2f64, Bias)));
Dale Johannesence0805b2009-02-03 19:33:06 +00004799 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4800 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling14a30ef2009-01-17 03:56:04 +00004801 DAG.getIntPtrConstant(0));
4802
4803 // Subtract the bias.
Dale Johannesence0805b2009-02-03 19:33:06 +00004804 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling14a30ef2009-01-17 03:56:04 +00004805
4806 // Handle final rounding.
Bill Wendlingdb547de2009-01-17 07:40:19 +00004807 MVT DestVT = Op.getValueType();
4808
4809 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004810 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendlingdb547de2009-01-17 07:40:19 +00004811 DAG.getIntPtrConstant(0));
4812 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004813 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendlingdb547de2009-01-17 07:40:19 +00004814 }
4815
4816 // Handle final rounding.
4817 return Sub;
Bill Wendling14a30ef2009-01-17 03:56:04 +00004818}
4819
4820SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Cheng44fd2392009-01-19 08:08:22 +00004821 SDValue N0 = Op.getOperand(0);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004822 DebugLoc dl = Op.getDebugLoc();
Bill Wendling14a30ef2009-01-17 03:56:04 +00004823
Evan Cheng44fd2392009-01-19 08:08:22 +00004824 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4825 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4826 // the optimization here.
4827 if (DAG.SignBitIsZero(N0))
Dale Johannesence0805b2009-02-03 19:33:06 +00004828 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Cheng44fd2392009-01-19 08:08:22 +00004829
4830 MVT SrcVT = N0.getValueType();
Bill Wendling14a30ef2009-01-17 03:56:04 +00004831 if (SrcVT == MVT::i64) {
4832 // We only handle SSE2 f64 target here; caller can handle the rest.
4833 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4834 return SDValue();
Bill Wendlingdb547de2009-01-17 07:40:19 +00004835
Bill Wendling14a30ef2009-01-17 03:56:04 +00004836 return LowerUINT_TO_FP_i64(Op, DAG);
4837 } else if (SrcVT == MVT::i32) {
Bill Wendling14a30ef2009-01-17 03:56:04 +00004838 return LowerUINT_TO_FP_i32(Op, DAG);
4839 }
4840
4841 assert(0 && "Unknown UINT_TO_FP to lower!");
4842 return SDValue();
4843}
4844
Dan Gohman8181bd12008-07-27 21:46:04 +00004845std::pair<SDValue,SDValue> X86TargetLowering::
4846FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004847 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004848 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4849 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004850 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004851
Dale Johannesen2fc20782007-09-14 22:26:36 +00004852 // These are really Legal.
Scott Michel91099d62009-02-17 22:15:04 +00004853 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004854 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004855 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004856 if (Subtarget->is64Bit() &&
4857 Op.getValueType() == MVT::i64 &&
4858 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004859 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004860
Evan Cheng05441e62007-10-15 20:11:21 +00004861 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4862 // stack slot.
4863 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004864 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004865 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004866 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004867 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004868 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004869 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4870 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4871 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4872 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004873 }
4874
Dan Gohman8181bd12008-07-27 21:46:04 +00004875 SDValue Chain = DAG.getEntryNode();
4876 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004877 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004878 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesence0805b2009-02-03 19:33:06 +00004879 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004880 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004881 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004882 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004883 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4884 };
Dale Johannesence0805b2009-02-03 19:33:06 +00004885 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004886 Chain = Value.getValue(1);
4887 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4888 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4889 }
4890
4891 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004892 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesence0805b2009-02-03 19:33:06 +00004893 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004894
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004895 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004896}
4897
Dan Gohman8181bd12008-07-27 21:46:04 +00004898SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4899 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4900 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004901 if (FIST.getNode() == 0) return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00004902
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004903 // Load the result.
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004904 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesence0805b2009-02-03 19:33:06 +00004905 FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004906}
4907
Dan Gohman8181bd12008-07-27 21:46:04 +00004908SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004909 DebugLoc dl = Op.getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00004910 MVT VT = Op.getValueType();
4911 MVT EltVT = VT;
4912 if (VT.isVector())
4913 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004914 std::vector<Constant*> CV;
4915 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004916 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004917 CV.push_back(C);
4918 CV.push_back(C);
4919 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004920 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004921 CV.push_back(C);
4922 CV.push_back(C);
4923 CV.push_back(C);
4924 CV.push_back(C);
4925 }
Dan Gohman11821702007-07-27 17:16:43 +00004926 Constant *C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00004927 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00004928 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004929 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004930 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00004931 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004932}
4933
Dan Gohman8181bd12008-07-27 21:46:04 +00004934SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004935 DebugLoc dl = Op.getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00004936 MVT VT = Op.getValueType();
4937 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004938 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004939 if (VT.isVector()) {
4940 EltVT = VT.getVectorElementType();
4941 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004942 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004943 std::vector<Constant*> CV;
4944 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004945 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004946 CV.push_back(C);
4947 CV.push_back(C);
4948 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004949 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004950 CV.push_back(C);
4951 CV.push_back(C);
4952 CV.push_back(C);
4953 CV.push_back(C);
4954 }
Dan Gohman11821702007-07-27 17:16:43 +00004955 Constant *C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00004956 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00004957 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004958 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004959 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004960 if (VT.isVector()) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004961 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4962 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Scott Michel91099d62009-02-17 22:15:04 +00004963 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesence0805b2009-02-03 19:33:06 +00004964 Op.getOperand(0)),
4965 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Cheng92b8f782007-07-19 23:36:01 +00004966 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00004967 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng92b8f782007-07-19 23:36:01 +00004968 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004969}
4970
Dan Gohman8181bd12008-07-27 21:46:04 +00004971SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4972 SDValue Op0 = Op.getOperand(0);
4973 SDValue Op1 = Op.getOperand(1);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004974 DebugLoc dl = Op.getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00004975 MVT VT = Op.getValueType();
4976 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004977
4978 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004979 if (SrcVT.bitsLT(VT)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004980 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004981 SrcVT = VT;
4982 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004983 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004984 if (SrcVT.bitsGT(VT)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004985 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004986 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004987 }
4988
4989 // At this point the operands and the result should have the same
4990 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004991
4992 // First get the sign bit of second operand.
4993 std::vector<Constant*> CV;
4994 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004995 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4996 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004997 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004998 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4999 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5000 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5001 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002 }
Dan Gohman11821702007-07-27 17:16:43 +00005003 Constant *C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00005004 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005005 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005006 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005007 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005008 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005009
5010 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005011 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005012 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesence0805b2009-02-03 19:33:06 +00005013 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5014 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005015 DAG.getConstant(32, MVT::i32));
Dale Johannesence0805b2009-02-03 19:33:06 +00005016 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5017 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00005018 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005019 }
5020
5021 // Clear first operand sign bit.
5022 CV.clear();
5023 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005024 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
5025 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005026 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005027 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
5028 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5029 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5030 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005031 }
Dan Gohman11821702007-07-27 17:16:43 +00005032 C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00005033 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005034 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005035 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005036 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005037 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005038
5039 // Or the value with the sign bit.
Dale Johannesence0805b2009-02-03 19:33:06 +00005040 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005041}
5042
Dan Gohman99a12192009-03-04 19:44:21 +00005043/// Emit nodes that will be selected as "test Op0,Op0", or something
5044/// equivalent.
Dan Gohmanc8b47852009-03-07 01:58:32 +00005045SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5046 SelectionDAG &DAG) {
Dan Gohman99a12192009-03-04 19:44:21 +00005047 DebugLoc dl = Op.getDebugLoc();
5048
Dan Gohmanc8b47852009-03-07 01:58:32 +00005049 // CF and OF aren't always set the way we want. Determine which
5050 // of these we need.
5051 bool NeedCF = false;
5052 bool NeedOF = false;
5053 switch (X86CC) {
5054 case X86::COND_A: case X86::COND_AE:
5055 case X86::COND_B: case X86::COND_BE:
5056 NeedCF = true;
5057 break;
5058 case X86::COND_G: case X86::COND_GE:
5059 case X86::COND_L: case X86::COND_LE:
5060 case X86::COND_O: case X86::COND_NO:
5061 NeedOF = true;
5062 break;
5063 default: break;
5064 }
5065
Dan Gohman99a12192009-03-04 19:44:21 +00005066 // See if we can use the EFLAGS value from the operand instead of
Dan Gohmanc8b47852009-03-07 01:58:32 +00005067 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5068 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5069 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman99a12192009-03-04 19:44:21 +00005070 unsigned Opcode = 0;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005071 unsigned NumOperands = 0;
Dan Gohman99a12192009-03-04 19:44:21 +00005072 switch (Op.getNode()->getOpcode()) {
5073 case ISD::ADD:
5074 // Due to an isel shortcoming, be conservative if this add is likely to
5075 // be selected as part of a load-modify-store instruction. When the root
5076 // node in a match is a store, isel doesn't know how to remap non-chain
5077 // non-flag uses of other nodes in the match, such as the ADD in this
5078 // case. This leads to the ADD being left around and reselected, with
5079 // the result being two adds in the output.
5080 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5081 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5082 if (UI->getOpcode() == ISD::STORE)
5083 goto default_case;
Dan Gohman99a12192009-03-04 19:44:21 +00005084 if (ConstantSDNode *C =
Dan Gohmand90a8fd2009-03-05 19:32:48 +00005085 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5086 // An add of one will be selected as an INC.
Dan Gohman99a12192009-03-04 19:44:21 +00005087 if (C->getAPIntValue() == 1) {
5088 Opcode = X86ISD::INC;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005089 NumOperands = 1;
Dan Gohman99a12192009-03-04 19:44:21 +00005090 break;
5091 }
Dan Gohmand90a8fd2009-03-05 19:32:48 +00005092 // An add of negative one (subtract of one) will be selected as a DEC.
5093 if (C->getAPIntValue().isAllOnesValue()) {
5094 Opcode = X86ISD::DEC;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005095 NumOperands = 1;
Dan Gohmand90a8fd2009-03-05 19:32:48 +00005096 break;
5097 }
5098 }
Dan Gohman99a12192009-03-04 19:44:21 +00005099 // Otherwise use a regular EFLAGS-setting add.
5100 Opcode = X86ISD::ADD;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005101 NumOperands = 2;
Dan Gohman99a12192009-03-04 19:44:21 +00005102 break;
5103 case ISD::SUB:
5104 // Due to the ISEL shortcoming noted above, be conservative if this sub is
5105 // likely to be selected as part of a load-modify-store instruction.
5106 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5107 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5108 if (UI->getOpcode() == ISD::STORE)
5109 goto default_case;
Dan Gohman99a12192009-03-04 19:44:21 +00005110 // Otherwise use a regular EFLAGS-setting sub.
5111 Opcode = X86ISD::SUB;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005112 NumOperands = 2;
Dan Gohman99a12192009-03-04 19:44:21 +00005113 break;
5114 case X86ISD::ADD:
5115 case X86ISD::SUB:
5116 case X86ISD::INC:
5117 case X86ISD::DEC:
5118 return SDValue(Op.getNode(), 1);
5119 default:
5120 default_case:
5121 break;
5122 }
5123 if (Opcode != 0) {
Dan Gohmanee036282009-04-09 23:54:40 +00005124 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman99a12192009-03-04 19:44:21 +00005125 SmallVector<SDValue, 4> Ops;
Dan Gohmanc8b47852009-03-07 01:58:32 +00005126 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman99a12192009-03-04 19:44:21 +00005127 Ops.push_back(Op.getOperand(i));
Dan Gohmanee036282009-04-09 23:54:40 +00005128 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman99a12192009-03-04 19:44:21 +00005129 DAG.ReplaceAllUsesWith(Op, New);
5130 return SDValue(New.getNode(), 1);
5131 }
5132 }
5133
5134 // Otherwise just emit a CMP with 0, which is the TEST pattern.
5135 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
5136 DAG.getConstant(0, Op.getValueType()));
5137}
5138
5139/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5140/// equivalent.
Dan Gohmanc8b47852009-03-07 01:58:32 +00005141SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5142 SelectionDAG &DAG) {
Dan Gohman99a12192009-03-04 19:44:21 +00005143 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5144 if (C->getAPIntValue() == 0)
Dan Gohmanc8b47852009-03-07 01:58:32 +00005145 return EmitTest(Op0, X86CC, DAG);
Dan Gohman99a12192009-03-04 19:44:21 +00005146
5147 DebugLoc dl = Op0.getDebugLoc();
5148 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5149}
5150
Dan Gohman8181bd12008-07-27 21:46:04 +00005151SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00005152 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00005153 SDValue Op0 = Op.getOperand(0);
5154 SDValue Op1 = Op.getOperand(1);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005155 DebugLoc dl = Op.getDebugLoc();
Chris Lattner77a62312008-12-25 05:34:37 +00005156 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Scott Michel91099d62009-02-17 22:15:04 +00005157
Dan Gohman22cefb02009-01-29 01:59:02 +00005158 // Lower (X & (1 << N)) == 0 to BT(X, N).
5159 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5160 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman13dd9522009-01-13 23:25:30 +00005161 if (Op0.getOpcode() == ISD::AND &&
5162 Op0.hasOneUse() &&
5163 Op1.getOpcode() == ISD::Constant &&
Dan Gohman22cefb02009-01-29 01:59:02 +00005164 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattner77a62312008-12-25 05:34:37 +00005165 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohman22cefb02009-01-29 01:59:02 +00005166 SDValue LHS, RHS;
5167 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5168 if (ConstantSDNode *Op010C =
5169 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5170 if (Op010C->getZExtValue() == 1) {
5171 LHS = Op0.getOperand(0);
5172 RHS = Op0.getOperand(1).getOperand(1);
5173 }
5174 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5175 if (ConstantSDNode *Op000C =
5176 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5177 if (Op000C->getZExtValue() == 1) {
5178 LHS = Op0.getOperand(1);
5179 RHS = Op0.getOperand(0).getOperand(1);
5180 }
5181 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5182 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5183 SDValue AndLHS = Op0.getOperand(0);
5184 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5185 LHS = AndLHS.getOperand(0);
5186 RHS = AndLHS.getOperand(1);
5187 }
5188 }
Evan Cheng950aac02007-09-25 01:57:46 +00005189
Dan Gohman22cefb02009-01-29 01:59:02 +00005190 if (LHS.getNode()) {
Chris Lattner77a62312008-12-25 05:34:37 +00005191 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5192 // instruction. Since the shift amount is in-range-or-undefined, we know
5193 // that doing a bittest on the i16 value is ok. We extend to i32 because
5194 // the encoding for the i16 version is larger than the i32 version.
5195 if (LHS.getValueType() == MVT::i8)
Dale Johannesence0805b2009-02-03 19:33:06 +00005196 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattner77a62312008-12-25 05:34:37 +00005197
5198 // If the operand types disagree, extend the shift amount to match. Since
5199 // BT ignores high bits (like shifts) we can use anyextend.
5200 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesence0805b2009-02-03 19:33:06 +00005201 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohman22cefb02009-01-29 01:59:02 +00005202
Dale Johannesence0805b2009-02-03 19:33:06 +00005203 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005204 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesence0805b2009-02-03 19:33:06 +00005205 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner77a62312008-12-25 05:34:37 +00005206 DAG.getConstant(Cond, MVT::i8), BT);
5207 }
5208 }
5209
5210 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5211 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Scott Michel91099d62009-02-17 22:15:04 +00005212
Dan Gohmanc8b47852009-03-07 01:58:32 +00005213 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Dale Johannesence0805b2009-02-03 19:33:06 +00005214 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner60435922008-12-24 00:11:37 +00005215 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005216}
5217
Dan Gohman8181bd12008-07-27 21:46:04 +00005218SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5219 SDValue Cond;
5220 SDValue Op0 = Op.getOperand(0);
5221 SDValue Op1 = Op.getOperand(1);
5222 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00005223 MVT VT = Op.getValueType();
5224 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5225 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005226 DebugLoc dl = Op.getDebugLoc();
Nate Begeman03605a02008-07-17 16:51:19 +00005227
5228 if (isFP) {
5229 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00005230 MVT VT0 = Op0.getValueType();
5231 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5232 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00005233 bool Swap = false;
5234
5235 switch (SetCCOpcode) {
5236 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005237 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00005238 case ISD::SETEQ: SSECC = 0; break;
Scott Michel91099d62009-02-17 22:15:04 +00005239 case ISD::SETOGT:
Nate Begeman03605a02008-07-17 16:51:19 +00005240 case ISD::SETGT: Swap = true; // Fallthrough
5241 case ISD::SETLT:
5242 case ISD::SETOLT: SSECC = 1; break;
5243 case ISD::SETOGE:
5244 case ISD::SETGE: Swap = true; // Fallthrough
5245 case ISD::SETLE:
5246 case ISD::SETOLE: SSECC = 2; break;
5247 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005248 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00005249 case ISD::SETNE: SSECC = 4; break;
5250 case ISD::SETULE: Swap = true;
5251 case ISD::SETUGE: SSECC = 5; break;
5252 case ISD::SETULT: Swap = true;
5253 case ISD::SETUGT: SSECC = 6; break;
5254 case ISD::SETO: SSECC = 7; break;
5255 }
5256 if (Swap)
5257 std::swap(Op0, Op1);
5258
Nate Begeman6357f9d2008-07-25 19:05:58 +00005259 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00005260 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00005261 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005262 SDValue UNORD, EQ;
Dale Johannesence0805b2009-02-03 19:33:06 +00005263 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5264 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5265 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begeman6357f9d2008-07-25 19:05:58 +00005266 }
5267 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005268 SDValue ORD, NEQ;
Dale Johannesence0805b2009-02-03 19:33:06 +00005269 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5270 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5271 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begeman6357f9d2008-07-25 19:05:58 +00005272 }
5273 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00005274 }
5275 // Handle all other FP comparisons here.
Dale Johannesence0805b2009-02-03 19:33:06 +00005276 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman03605a02008-07-17 16:51:19 +00005277 }
Scott Michel91099d62009-02-17 22:15:04 +00005278
Nate Begeman03605a02008-07-17 16:51:19 +00005279 // We are handling one of the integer comparisons here. Since SSE only has
5280 // GT and EQ comparisons for integer, swapping operands and multiple
5281 // operations may be required for some comparisons.
5282 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5283 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michel91099d62009-02-17 22:15:04 +00005284
Nate Begeman03605a02008-07-17 16:51:19 +00005285 switch (VT.getSimpleVT()) {
5286 default: break;
5287 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5288 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5289 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5290 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5291 }
Scott Michel91099d62009-02-17 22:15:04 +00005292
Nate Begeman03605a02008-07-17 16:51:19 +00005293 switch (SetCCOpcode) {
5294 default: break;
5295 case ISD::SETNE: Invert = true;
5296 case ISD::SETEQ: Opc = EQOpc; break;
5297 case ISD::SETLT: Swap = true;
5298 case ISD::SETGT: Opc = GTOpc; break;
5299 case ISD::SETGE: Swap = true;
5300 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5301 case ISD::SETULT: Swap = true;
5302 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5303 case ISD::SETUGE: Swap = true;
5304 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5305 }
5306 if (Swap)
5307 std::swap(Op0, Op1);
Scott Michel91099d62009-02-17 22:15:04 +00005308
Nate Begeman03605a02008-07-17 16:51:19 +00005309 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5310 // bits of the inputs before performing those operations.
5311 if (FlipSigns) {
5312 MVT EltVT = VT.getVectorElementType();
Duncan Sands505ba942009-02-01 18:06:53 +00005313 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5314 EltVT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005315 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Cheng907a2d22009-02-25 22:49:59 +00005316 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5317 SignBits.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00005318 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5319 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman03605a02008-07-17 16:51:19 +00005320 }
Scott Michel91099d62009-02-17 22:15:04 +00005321
Dale Johannesence0805b2009-02-03 19:33:06 +00005322 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005323
5324 // If the logical-not of the result is required, perform that now.
Bob Wilson81a42cf2009-01-22 17:39:32 +00005325 if (Invert)
Dale Johannesence0805b2009-02-03 19:33:06 +00005326 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson81a42cf2009-01-22 17:39:32 +00005327
Nate Begeman03605a02008-07-17 16:51:19 +00005328 return Result;
5329}
Evan Cheng950aac02007-09-25 01:57:46 +00005330
Evan Chengd580f022008-12-03 08:38:43 +00005331// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman99a12192009-03-04 19:44:21 +00005332static bool isX86LogicalCmp(SDValue Op) {
5333 unsigned Opc = Op.getNode()->getOpcode();
5334 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5335 return true;
5336 if (Op.getResNo() == 1 &&
5337 (Opc == X86ISD::ADD ||
5338 Opc == X86ISD::SUB ||
5339 Opc == X86ISD::SMUL ||
5340 Opc == X86ISD::UMUL ||
5341 Opc == X86ISD::INC ||
5342 Opc == X86ISD::DEC))
5343 return true;
5344
5345 return false;
Evan Chengd580f022008-12-03 08:38:43 +00005346}
5347
Dan Gohman8181bd12008-07-27 21:46:04 +00005348SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005349 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005350 SDValue Cond = Op.getOperand(0);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005351 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00005352 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005353
5354 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005355 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005356
Evan Cheng50d37ab2007-10-08 22:16:29 +00005357 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5358 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005359 if (Cond.getOpcode() == X86ISD::SETCC) {
5360 CC = Cond.getOperand(0);
5361
Dan Gohman8181bd12008-07-27 21:46:04 +00005362 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005363 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005364 MVT VT = Op.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00005365
Evan Cheng50d37ab2007-10-08 22:16:29 +00005366 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005367 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005368 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005369 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michel91099d62009-02-17 22:15:04 +00005370
Chris Lattnere4577dc2009-03-12 06:52:53 +00005371 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5372 Opc == X86ISD::BT) { // FIXME
Evan Cheng50d37ab2007-10-08 22:16:29 +00005373 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005374 addTest = false;
5375 }
5376 }
5377
5378 if (addTest) {
5379 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohmanc8b47852009-03-07 01:58:32 +00005380 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng950aac02007-09-25 01:57:46 +00005381 }
5382
Dan Gohmanee036282009-04-09 23:54:40 +00005383 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005384 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005385 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5386 // condition is true.
5387 Ops.push_back(Op.getOperand(2));
5388 Ops.push_back(Op.getOperand(1));
5389 Ops.push_back(CC);
5390 Ops.push_back(Cond);
Dan Gohmanee036282009-04-09 23:54:40 +00005391 return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005392}
5393
Evan Chengd580f022008-12-03 08:38:43 +00005394// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5395// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5396// from the AND / OR.
5397static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5398 Opc = Op.getOpcode();
5399 if (Opc != ISD::OR && Opc != ISD::AND)
5400 return false;
5401 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5402 Op.getOperand(0).hasOneUse() &&
5403 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5404 Op.getOperand(1).hasOneUse());
5405}
5406
Evan Cheng67f98b12009-02-02 08:19:07 +00005407// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5408// 1 and that the SETCC node has a single use.
Evan Cheng8c3af2c2009-02-02 08:07:36 +00005409static bool isXor1OfSetCC(SDValue Op) {
5410 if (Op.getOpcode() != ISD::XOR)
5411 return false;
5412 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5413 if (N1C && N1C->getAPIntValue() == 1) {
5414 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5415 Op.getOperand(0).hasOneUse();
5416 }
5417 return false;
5418}
5419
Dan Gohman8181bd12008-07-27 21:46:04 +00005420SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005421 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005422 SDValue Chain = Op.getOperand(0);
5423 SDValue Cond = Op.getOperand(1);
5424 SDValue Dest = Op.getOperand(2);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005425 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00005426 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005427
5428 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005429 Cond = LowerSETCC(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005430#if 0
5431 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingf5399032008-12-12 21:15:41 +00005432 else if (Cond.getOpcode() == X86ISD::ADD ||
5433 Cond.getOpcode() == X86ISD::SUB ||
5434 Cond.getOpcode() == X86ISD::SMUL ||
5435 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling7e04be62008-12-09 22:08:41 +00005436 Cond = LowerXALUO(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005437#endif
Scott Michel91099d62009-02-17 22:15:04 +00005438
Evan Cheng50d37ab2007-10-08 22:16:29 +00005439 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5440 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005441 if (Cond.getOpcode() == X86ISD::SETCC) {
5442 CC = Cond.getOperand(0);
5443
Dan Gohman8181bd12008-07-27 21:46:04 +00005444 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005445 unsigned Opc = Cmp.getOpcode();
Chris Lattner77a62312008-12-25 05:34:37 +00005446 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman99a12192009-03-04 19:44:21 +00005447 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005448 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005449 addTest = false;
Bill Wendlingd3511522008-12-02 01:06:39 +00005450 } else {
Evan Chengd580f022008-12-03 08:38:43 +00005451 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling809e7bd2008-12-03 08:32:02 +00005452 default: break;
5453 case X86::COND_O:
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005454 case X86::COND_B:
Chris Lattner77a62312008-12-25 05:34:37 +00005455 // These can only come from an arithmetic instruction with overflow,
5456 // e.g. SADDO, UADDO.
Bill Wendling809e7bd2008-12-03 08:32:02 +00005457 Cond = Cond.getNode()->getOperand(1);
5458 addTest = false;
5459 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00005460 }
Evan Cheng950aac02007-09-25 01:57:46 +00005461 }
Evan Chengd580f022008-12-03 08:38:43 +00005462 } else {
5463 unsigned CondOpc;
5464 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5465 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Chengd580f022008-12-03 08:38:43 +00005466 if (CondOpc == ISD::OR) {
5467 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5468 // two branches instead of an explicit OR instruction with a
5469 // separate test.
5470 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman99a12192009-03-04 19:44:21 +00005471 isX86LogicalCmp(Cmp)) {
Evan Chengd580f022008-12-03 08:38:43 +00005472 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005473 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Chengd580f022008-12-03 08:38:43 +00005474 Chain, Dest, CC, Cmp);
5475 CC = Cond.getOperand(1).getOperand(0);
5476 Cond = Cmp;
5477 addTest = false;
5478 }
5479 } else { // ISD::AND
5480 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5481 // two branches instead of an explicit AND instruction with a
5482 // separate test. However, we only do this if this block doesn't
5483 // have a fall-through edge, because this requires an explicit
5484 // jmp when the condition is false.
5485 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman99a12192009-03-04 19:44:21 +00005486 isX86LogicalCmp(Cmp) &&
Evan Chengd580f022008-12-03 08:38:43 +00005487 Op.getNode()->hasOneUse()) {
5488 X86::CondCode CCode =
5489 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5490 CCode = X86::GetOppositeBranchCondition(CCode);
5491 CC = DAG.getConstant(CCode, MVT::i8);
5492 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5493 // Look for an unconditional branch following this conditional branch.
5494 // We need this because we need to reverse the successors in order
5495 // to implement FCMP_OEQ.
5496 if (User.getOpcode() == ISD::BR) {
5497 SDValue FalseBB = User.getOperand(1);
5498 SDValue NewBR =
5499 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5500 assert(NewBR == User);
5501 Dest = FalseBB;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005502
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005503 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Chengd580f022008-12-03 08:38:43 +00005504 Chain, Dest, CC, Cmp);
5505 X86::CondCode CCode =
5506 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5507 CCode = X86::GetOppositeBranchCondition(CCode);
5508 CC = DAG.getConstant(CCode, MVT::i8);
5509 Cond = Cmp;
5510 addTest = false;
5511 }
5512 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005513 }
Evan Cheng8c3af2c2009-02-02 08:07:36 +00005514 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5515 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5516 // It should be transformed during dag combiner except when the condition
5517 // is set by a arithmetics with overflow node.
5518 X86::CondCode CCode =
5519 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5520 CCode = X86::GetOppositeBranchCondition(CCode);
5521 CC = DAG.getConstant(CCode, MVT::i8);
5522 Cond = Cond.getOperand(0).getOperand(1);
5523 addTest = false;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005524 }
Evan Cheng950aac02007-09-25 01:57:46 +00005525 }
5526
5527 if (addTest) {
5528 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohmanc8b47852009-03-07 01:58:32 +00005529 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng950aac02007-09-25 01:57:46 +00005530 }
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005531 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005532 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005533}
5534
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005535
5536// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5537// Calls to _alloca is needed to probe the stack when allocating more than 4k
5538// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5539// that the guard pages used by the OS virtual memory manager are allocated in
5540// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005541SDValue
5542X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005543 SelectionDAG &DAG) {
5544 assert(Subtarget->isTargetCygMing() &&
5545 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005546 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005547
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005548 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005549 SDValue Chain = Op.getOperand(0);
5550 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005551 // FIXME: Ensure alignment here
5552
Dan Gohman8181bd12008-07-27 21:46:04 +00005553 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005554
Duncan Sands92c43912008-06-06 12:08:01 +00005555 MVT IntPtr = getPointerTy();
5556 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005557
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005558 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005559
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00005560 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005561 Flag = Chain.getValue(1);
5562
5563 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005564 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005565 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005566 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005567 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005568 Flag };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005569 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005570 Flag = Chain.getValue(1);
5571
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005572 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005573 DAG.getIntPtrConstant(0, true),
5574 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005575 Flag);
5576
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00005577 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005578
Dan Gohman8181bd12008-07-27 21:46:04 +00005579 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005580 return DAG.getMergeValues(Ops1, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005581}
5582
Dan Gohman8181bd12008-07-27 21:46:04 +00005583SDValue
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005584X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005585 SDValue Chain,
5586 SDValue Dst, SDValue Src,
5587 SDValue Size, unsigned Align,
5588 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005589 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005590 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005591
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005592 // If not DWORD aligned or size is more than the threshold, call the library.
5593 // The libc version is likely to be faster for these cases. It can use the
5594 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005595 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005596 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005597 ConstantSize->getZExtValue() >
5598 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005599 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005600
5601 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005602 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005603
Bill Wendling4b2e3782008-10-01 00:59:58 +00005604 if (const char *bzeroEntry = V &&
5605 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5606 MVT IntPtr = getPointerTy();
5607 const Type *IntPtrTy = TD->getIntPtrType();
Scott Michel91099d62009-02-17 22:15:04 +00005608 TargetLowering::ArgListTy Args;
Bill Wendling4b2e3782008-10-01 00:59:58 +00005609 TargetLowering::ArgListEntry Entry;
5610 Entry.Node = Dst;
5611 Entry.Ty = IntPtrTy;
5612 Args.push_back(Entry);
5613 Entry.Node = Size;
5614 Args.push_back(Entry);
5615 std::pair<SDValue,SDValue> CallResult =
Scott Michel91099d62009-02-17 22:15:04 +00005616 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5617 CallingConv::C, false,
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005618 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling4b2e3782008-10-01 00:59:58 +00005619 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005620 }
5621
Dan Gohmane8b391e2008-04-12 04:36:06 +00005622 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005623 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005624 }
5625
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005626 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005627 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005628 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005629 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005630 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005631 unsigned BytesLeft = 0;
5632 bool TwoRepStos = false;
5633 if (ValC) {
5634 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005635 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005636
5637 // If the value is a constant, then we can potentially use larger sets.
5638 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005639 case 2: // WORD aligned
5640 AVT = MVT::i16;
5641 ValReg = X86::AX;
5642 Val = (Val << 8) | Val;
5643 break;
5644 case 0: // DWORD aligned
5645 AVT = MVT::i32;
5646 ValReg = X86::EAX;
5647 Val = (Val << 8) | Val;
5648 Val = (Val << 16) | Val;
5649 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5650 AVT = MVT::i64;
5651 ValReg = X86::RAX;
5652 Val = (Val << 32) | Val;
5653 }
5654 break;
5655 default: // Byte aligned
5656 AVT = MVT::i8;
5657 ValReg = X86::AL;
5658 Count = DAG.getIntPtrConstant(SizeVal);
5659 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005660 }
5661
Duncan Sandsec142ee2008-06-08 20:54:56 +00005662 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005663 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005664 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5665 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005666 }
5667
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005668 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005669 InFlag);
5670 InFlag = Chain.getValue(1);
5671 } else {
5672 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005673 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005674 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005675 InFlag = Chain.getValue(1);
5676 }
5677
Scott Michel91099d62009-02-17 22:15:04 +00005678 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005679 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005680 Count, InFlag);
5681 InFlag = Chain.getValue(1);
Scott Michel91099d62009-02-17 22:15:04 +00005682 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005683 X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005684 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005685 InFlag = Chain.getValue(1);
5686
5687 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005688 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005689 Ops.push_back(Chain);
5690 Ops.push_back(DAG.getValueType(AVT));
5691 Ops.push_back(InFlag);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005692 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005693
5694 if (TwoRepStos) {
5695 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005696 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005697 MVT CVT = Count.getValueType();
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005698 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005699 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Scott Michel91099d62009-02-17 22:15:04 +00005700 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005701 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005702 Left, InFlag);
5703 InFlag = Chain.getValue(1);
5704 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5705 Ops.clear();
5706 Ops.push_back(Chain);
5707 Ops.push_back(DAG.getValueType(MVT::i8));
5708 Ops.push_back(InFlag);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005709 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005710 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005711 // Handle the last 1 - 7 bytes.
5712 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005713 MVT AddrVT = Dst.getValueType();
5714 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005715
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005716 Chain = DAG.getMemset(Chain, dl,
5717 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005718 DAG.getConstant(Offset, AddrVT)),
5719 Src,
5720 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005721 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005722 }
5723
Dan Gohmane8b391e2008-04-12 04:36:06 +00005724 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005725 return Chain;
5726}
5727
Dan Gohman8181bd12008-07-27 21:46:04 +00005728SDValue
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005729X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005730 SDValue Chain, SDValue Dst, SDValue Src,
5731 SDValue Size, unsigned Align,
5732 bool AlwaysInline,
5733 const Value *DstSV, uint64_t DstSVOff,
Scott Michel91099d62009-02-17 22:15:04 +00005734 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005735 // This requires the copy size to be a constant, preferrably
5736 // within a subtarget-specific limit.
5737 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5738 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005739 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005740 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005741 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005742 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005743
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005744 /// If not DWORD aligned, call the library.
5745 if ((Align & 3) != 0)
5746 return SDValue();
5747
5748 // DWORD aligned
5749 MVT AVT = MVT::i32;
5750 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005751 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005752
Duncan Sands92c43912008-06-06 12:08:01 +00005753 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005754 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005755 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005756 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005757
Dan Gohman8181bd12008-07-27 21:46:04 +00005758 SDValue InFlag(0, 0);
Scott Michel91099d62009-02-17 22:15:04 +00005759 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005760 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005761 Count, InFlag);
5762 InFlag = Chain.getValue(1);
Scott Michel91099d62009-02-17 22:15:04 +00005763 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005764 X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005765 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005766 InFlag = Chain.getValue(1);
Scott Michel91099d62009-02-17 22:15:04 +00005767 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005768 X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005769 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005770 InFlag = Chain.getValue(1);
5771
5772 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005773 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005774 Ops.push_back(Chain);
5775 Ops.push_back(DAG.getValueType(AVT));
5776 Ops.push_back(InFlag);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005777 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005778
Dan Gohman8181bd12008-07-27 21:46:04 +00005779 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005780 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005781 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005782 // Handle the last 1 - 7 bytes.
5783 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005784 MVT DstVT = Dst.getValueType();
5785 MVT SrcVT = Src.getValueType();
5786 MVT SizeVT = Size.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00005787 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005788 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005789 DAG.getConstant(Offset, DstVT)),
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005790 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005791 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005792 DAG.getConstant(BytesLeft, SizeVT),
5793 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005794 DstSV, DstSVOff + Offset,
5795 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005796 }
5797
Scott Michel91099d62009-02-17 22:15:04 +00005798 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005799 &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005800}
5801
Dan Gohman8181bd12008-07-27 21:46:04 +00005802SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005803 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005804 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005805
5806 if (!Subtarget->is64Bit()) {
5807 // vastart just stores the address of the VarArgsFrameIndex slot into the
5808 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005809 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005810 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005811 }
5812
5813 // __va_list_tag:
5814 // gp_offset (0 - 6 * 8)
5815 // fp_offset (48 - 48 + 8 * 16)
5816 // overflow_arg_area (point to parameters coming in memory).
5817 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005818 SmallVector<SDValue, 8> MemOps;
5819 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005820 // Store gp_offset
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005821 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005822 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005823 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005824 MemOps.push_back(Store);
5825
5826 // Store fp_offset
Scott Michel91099d62009-02-17 22:15:04 +00005827 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005828 FIN, DAG.getIntPtrConstant(4));
5829 Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005830 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005831 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005832 MemOps.push_back(Store);
5833
5834 // Store ptr to overflow_arg_area
Scott Michel91099d62009-02-17 22:15:04 +00005835 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005836 FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005837 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005838 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005839 MemOps.push_back(Store);
5840
5841 // Store ptr to reg_save_area.
Scott Michel91099d62009-02-17 22:15:04 +00005842 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005843 FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005844 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005845 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005846 MemOps.push_back(Store);
Scott Michel91099d62009-02-17 22:15:04 +00005847 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005848 &MemOps[0], MemOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005849}
5850
Dan Gohman8181bd12008-07-27 21:46:04 +00005851SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005852 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5853 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005854 SDValue Chain = Op.getOperand(0);
5855 SDValue SrcPtr = Op.getOperand(1);
5856 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005857
5858 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5859 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005860 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005861}
5862
Dan Gohman8181bd12008-07-27 21:46:04 +00005863SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005864 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005865 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005866 SDValue Chain = Op.getOperand(0);
5867 SDValue DstPtr = Op.getOperand(1);
5868 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005869 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5870 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005871 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005872
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00005873 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman840ff5c2008-04-18 20:55:41 +00005874 DAG.getIntPtrConstant(24), 8, false,
5875 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005876}
5877
Dan Gohman8181bd12008-07-27 21:46:04 +00005878SDValue
5879X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005880 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005881 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005882 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005883 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005884 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005885 case Intrinsic::x86_sse_comieq_ss:
5886 case Intrinsic::x86_sse_comilt_ss:
5887 case Intrinsic::x86_sse_comile_ss:
5888 case Intrinsic::x86_sse_comigt_ss:
5889 case Intrinsic::x86_sse_comige_ss:
5890 case Intrinsic::x86_sse_comineq_ss:
5891 case Intrinsic::x86_sse_ucomieq_ss:
5892 case Intrinsic::x86_sse_ucomilt_ss:
5893 case Intrinsic::x86_sse_ucomile_ss:
5894 case Intrinsic::x86_sse_ucomigt_ss:
5895 case Intrinsic::x86_sse_ucomige_ss:
5896 case Intrinsic::x86_sse_ucomineq_ss:
5897 case Intrinsic::x86_sse2_comieq_sd:
5898 case Intrinsic::x86_sse2_comilt_sd:
5899 case Intrinsic::x86_sse2_comile_sd:
5900 case Intrinsic::x86_sse2_comigt_sd:
5901 case Intrinsic::x86_sse2_comige_sd:
5902 case Intrinsic::x86_sse2_comineq_sd:
5903 case Intrinsic::x86_sse2_ucomieq_sd:
5904 case Intrinsic::x86_sse2_ucomilt_sd:
5905 case Intrinsic::x86_sse2_ucomile_sd:
5906 case Intrinsic::x86_sse2_ucomigt_sd:
5907 case Intrinsic::x86_sse2_ucomige_sd:
5908 case Intrinsic::x86_sse2_ucomineq_sd: {
5909 unsigned Opc = 0;
5910 ISD::CondCode CC = ISD::SETCC_INVALID;
5911 switch (IntNo) {
5912 default: break;
5913 case Intrinsic::x86_sse_comieq_ss:
5914 case Intrinsic::x86_sse2_comieq_sd:
5915 Opc = X86ISD::COMI;
5916 CC = ISD::SETEQ;
5917 break;
5918 case Intrinsic::x86_sse_comilt_ss:
5919 case Intrinsic::x86_sse2_comilt_sd:
5920 Opc = X86ISD::COMI;
5921 CC = ISD::SETLT;
5922 break;
5923 case Intrinsic::x86_sse_comile_ss:
5924 case Intrinsic::x86_sse2_comile_sd:
5925 Opc = X86ISD::COMI;
5926 CC = ISD::SETLE;
5927 break;
5928 case Intrinsic::x86_sse_comigt_ss:
5929 case Intrinsic::x86_sse2_comigt_sd:
5930 Opc = X86ISD::COMI;
5931 CC = ISD::SETGT;
5932 break;
5933 case Intrinsic::x86_sse_comige_ss:
5934 case Intrinsic::x86_sse2_comige_sd:
5935 Opc = X86ISD::COMI;
5936 CC = ISD::SETGE;
5937 break;
5938 case Intrinsic::x86_sse_comineq_ss:
5939 case Intrinsic::x86_sse2_comineq_sd:
5940 Opc = X86ISD::COMI;
5941 CC = ISD::SETNE;
5942 break;
5943 case Intrinsic::x86_sse_ucomieq_ss:
5944 case Intrinsic::x86_sse2_ucomieq_sd:
5945 Opc = X86ISD::UCOMI;
5946 CC = ISD::SETEQ;
5947 break;
5948 case Intrinsic::x86_sse_ucomilt_ss:
5949 case Intrinsic::x86_sse2_ucomilt_sd:
5950 Opc = X86ISD::UCOMI;
5951 CC = ISD::SETLT;
5952 break;
5953 case Intrinsic::x86_sse_ucomile_ss:
5954 case Intrinsic::x86_sse2_ucomile_sd:
5955 Opc = X86ISD::UCOMI;
5956 CC = ISD::SETLE;
5957 break;
5958 case Intrinsic::x86_sse_ucomigt_ss:
5959 case Intrinsic::x86_sse2_ucomigt_sd:
5960 Opc = X86ISD::UCOMI;
5961 CC = ISD::SETGT;
5962 break;
5963 case Intrinsic::x86_sse_ucomige_ss:
5964 case Intrinsic::x86_sse2_ucomige_sd:
5965 Opc = X86ISD::UCOMI;
5966 CC = ISD::SETGE;
5967 break;
5968 case Intrinsic::x86_sse_ucomineq_ss:
5969 case Intrinsic::x86_sse2_ucomineq_sd:
5970 Opc = X86ISD::UCOMI;
5971 CC = ISD::SETNE;
5972 break;
5973 }
5974
Dan Gohman8181bd12008-07-27 21:46:04 +00005975 SDValue LHS = Op.getOperand(1);
5976 SDValue RHS = Op.getOperand(2);
Chris Lattnerebb91142008-12-24 23:53:05 +00005977 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005978 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
5979 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005980 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005981 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005982 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005983
5984 // Fix vector shift instructions where the last operand is a non-immediate
5985 // i32 value.
5986 case Intrinsic::x86_sse2_pslli_w:
5987 case Intrinsic::x86_sse2_pslli_d:
5988 case Intrinsic::x86_sse2_pslli_q:
5989 case Intrinsic::x86_sse2_psrli_w:
5990 case Intrinsic::x86_sse2_psrli_d:
5991 case Intrinsic::x86_sse2_psrli_q:
5992 case Intrinsic::x86_sse2_psrai_w:
5993 case Intrinsic::x86_sse2_psrai_d:
5994 case Intrinsic::x86_mmx_pslli_w:
5995 case Intrinsic::x86_mmx_pslli_d:
5996 case Intrinsic::x86_mmx_pslli_q:
5997 case Intrinsic::x86_mmx_psrli_w:
5998 case Intrinsic::x86_mmx_psrli_d:
5999 case Intrinsic::x86_mmx_psrli_q:
6000 case Intrinsic::x86_mmx_psrai_w:
6001 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006002 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006003 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006004 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006005
6006 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006007 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006008 switch (IntNo) {
6009 case Intrinsic::x86_sse2_pslli_w:
6010 NewIntNo = Intrinsic::x86_sse2_psll_w;
6011 break;
6012 case Intrinsic::x86_sse2_pslli_d:
6013 NewIntNo = Intrinsic::x86_sse2_psll_d;
6014 break;
6015 case Intrinsic::x86_sse2_pslli_q:
6016 NewIntNo = Intrinsic::x86_sse2_psll_q;
6017 break;
6018 case Intrinsic::x86_sse2_psrli_w:
6019 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6020 break;
6021 case Intrinsic::x86_sse2_psrli_d:
6022 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6023 break;
6024 case Intrinsic::x86_sse2_psrli_q:
6025 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6026 break;
6027 case Intrinsic::x86_sse2_psrai_w:
6028 NewIntNo = Intrinsic::x86_sse2_psra_w;
6029 break;
6030 case Intrinsic::x86_sse2_psrai_d:
6031 NewIntNo = Intrinsic::x86_sse2_psra_d;
6032 break;
6033 default: {
6034 ShAmtVT = MVT::v2i32;
6035 switch (IntNo) {
6036 case Intrinsic::x86_mmx_pslli_w:
6037 NewIntNo = Intrinsic::x86_mmx_psll_w;
6038 break;
6039 case Intrinsic::x86_mmx_pslli_d:
6040 NewIntNo = Intrinsic::x86_mmx_psll_d;
6041 break;
6042 case Intrinsic::x86_mmx_pslli_q:
6043 NewIntNo = Intrinsic::x86_mmx_psll_q;
6044 break;
6045 case Intrinsic::x86_mmx_psrli_w:
6046 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6047 break;
6048 case Intrinsic::x86_mmx_psrli_d:
6049 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6050 break;
6051 case Intrinsic::x86_mmx_psrli_q:
6052 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6053 break;
6054 case Intrinsic::x86_mmx_psrai_w:
6055 NewIntNo = Intrinsic::x86_mmx_psra_w;
6056 break;
6057 case Intrinsic::x86_mmx_psrai_d:
6058 NewIntNo = Intrinsic::x86_mmx_psra_d;
6059 break;
6060 default: abort(); // Can't reach here.
6061 }
6062 break;
6063 }
6064 }
Duncan Sands92c43912008-06-06 12:08:01 +00006065 MVT VT = Op.getValueType();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006066 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6067 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6068 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006069 DAG.getConstant(NewIntNo, MVT::i32),
6070 Op.getOperand(1), ShAmt);
6071 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006072 }
6073}
6074
Dan Gohman8181bd12008-07-27 21:46:04 +00006075SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006076 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006077 DebugLoc dl = Op.getDebugLoc();
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006078
6079 if (Depth > 0) {
6080 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6081 SDValue Offset =
6082 DAG.getConstant(TD->getPointerSize(),
6083 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006084 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michel91099d62009-02-17 22:15:04 +00006085 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006086 FrameAddr, Offset),
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006087 NULL, 0);
6088 }
6089
6090 // Just load the return address.
Dan Gohman8181bd12008-07-27 21:46:04 +00006091 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michel91099d62009-02-17 22:15:04 +00006092 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006093 RetAddrFI, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006094}
6095
Dan Gohman8181bd12008-07-27 21:46:04 +00006096SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00006097 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6098 MFI->setFrameAddressIsTaken(true);
6099 MVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006100 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng33633672008-09-27 01:56:22 +00006101 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6102 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006103 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng33633672008-09-27 01:56:22 +00006104 while (Depth--)
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006105 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng33633672008-09-27 01:56:22 +00006106 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006107}
6108
Dan Gohman8181bd12008-07-27 21:46:04 +00006109SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00006110 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006111 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006112}
6113
Dan Gohman8181bd12008-07-27 21:46:04 +00006114SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006115{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006116 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00006117 SDValue Chain = Op.getOperand(0);
6118 SDValue Offset = Op.getOperand(1);
6119 SDValue Handler = Op.getOperand(2);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006120 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006121
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006122 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6123 getPointerTy());
6124 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006125
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006126 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006127 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006128 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6129 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006130 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006131 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006132
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006133 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006134 MVT::Other,
6135 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006136}
6137
Dan Gohman8181bd12008-07-27 21:46:04 +00006138SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006139 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006140 SDValue Root = Op.getOperand(0);
6141 SDValue Trmp = Op.getOperand(1); // trampoline
6142 SDValue FPtr = Op.getOperand(2); // nested function
6143 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006144 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006145
Dan Gohman12a9c082008-02-06 22:27:42 +00006146 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006147
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006148 const X86InstrInfo *TII =
6149 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6150
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006151 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006152 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006153
6154 // Large code-model.
6155
6156 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6157 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6158
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006159 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6160 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006161
6162 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6163
6164 // Load the pointer to the nested function into R11.
6165 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00006166 SDValue Addr = Trmp;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006167 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6168 Addr, TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006169
Scott Michel91099d62009-02-17 22:15:04 +00006170 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006171 DAG.getConstant(2, MVT::i64));
6172 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006173
6174 // Load the 'nest' parameter value into R10.
6175 // R10 is specified in X86CallingConv.td
6176 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Scott Michel91099d62009-02-17 22:15:04 +00006177 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006178 DAG.getConstant(10, MVT::i64));
6179 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6180 Addr, TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006181
Scott Michel91099d62009-02-17 22:15:04 +00006182 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006183 DAG.getConstant(12, MVT::i64));
6184 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006185
6186 // Jump to the nested function.
6187 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Scott Michel91099d62009-02-17 22:15:04 +00006188 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006189 DAG.getConstant(20, MVT::i64));
6190 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6191 Addr, TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006192
6193 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Scott Michel91099d62009-02-17 22:15:04 +00006194 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006195 DAG.getConstant(22, MVT::i64));
6196 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006197 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006198
Dan Gohman8181bd12008-07-27 21:46:04 +00006199 SDValue Ops[] =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006200 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6201 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006202 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00006203 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006204 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6205 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00006206 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006207
6208 switch (CC) {
6209 default:
6210 assert(0 && "Unsupported calling convention");
6211 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006212 case CallingConv::X86_StdCall: {
6213 // Pass 'nest' parameter in ECX.
6214 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006215 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006216
6217 // Check that ECX wasn't needed by an 'inreg' parameter.
6218 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00006219 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006220
Chris Lattner1c8733e2008-03-12 17:45:29 +00006221 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006222 unsigned InRegCount = 0;
6223 unsigned Idx = 1;
6224
6225 for (FunctionType::param_iterator I = FTy->param_begin(),
6226 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00006227 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006228 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006229 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006230
6231 if (InRegCount > 2) {
6232 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6233 abort();
6234 }
6235 }
6236 break;
6237 }
6238 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00006239 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006240 // Pass 'nest' parameter in EAX.
6241 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006242 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006243 break;
6244 }
6245
Dan Gohman8181bd12008-07-27 21:46:04 +00006246 SDValue OutChains[4];
6247 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006248
Scott Michel91099d62009-02-17 22:15:04 +00006249 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006250 DAG.getConstant(10, MVT::i32));
6251 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006252
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006253 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006254 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michel91099d62009-02-17 22:15:04 +00006255 OutChains[0] = DAG.getStore(Root, dl,
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006256 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00006257 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006258
Scott Michel91099d62009-02-17 22:15:04 +00006259 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006260 DAG.getConstant(1, MVT::i32));
6261 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006262
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006263 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Scott Michel91099d62009-02-17 22:15:04 +00006264 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006265 DAG.getConstant(5, MVT::i32));
6266 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006267 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006268
Scott Michel91099d62009-02-17 22:15:04 +00006269 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006270 DAG.getConstant(6, MVT::i32));
6271 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006272
Dan Gohman8181bd12008-07-27 21:46:04 +00006273 SDValue Ops[] =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006274 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6275 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006276 }
6277}
6278
Dan Gohman8181bd12008-07-27 21:46:04 +00006279SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006280 /*
6281 The rounding mode is in bits 11:10 of FPSR, and has the following
6282 settings:
6283 00 Round to nearest
6284 01 Round to -inf
6285 10 Round to +inf
6286 11 Round to 0
6287
6288 FLT_ROUNDS, on the other hand, expects the following:
6289 -1 Undefined
6290 0 Round to 0
6291 1 Round to nearest
6292 2 Round to +inf
6293 3 Round to -inf
6294
6295 To perform the conversion, we do:
6296 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6297 */
6298
6299 MachineFunction &MF = DAG.getMachineFunction();
6300 const TargetMachine &TM = MF.getTarget();
6301 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6302 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00006303 MVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006304 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006305
6306 // Save FP Control Word to stack slot
6307 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00006308 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006309
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006310 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00006311 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006312
6313 // Load FP Control Word from stack slot
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006314 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006315
6316 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00006317 SDValue CWD1 =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006318 DAG.getNode(ISD::SRL, dl, MVT::i16,
6319 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006320 CWD, DAG.getConstant(0x800, MVT::i16)),
6321 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006322 SDValue CWD2 =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006323 DAG.getNode(ISD::SRL, dl, MVT::i16,
6324 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006325 CWD, DAG.getConstant(0x400, MVT::i16)),
6326 DAG.getConstant(9, MVT::i8));
6327
Dan Gohman8181bd12008-07-27 21:46:04 +00006328 SDValue RetVal =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006329 DAG.getNode(ISD::AND, dl, MVT::i16,
6330 DAG.getNode(ISD::ADD, dl, MVT::i16,
6331 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006332 DAG.getConstant(1, MVT::i16)),
6333 DAG.getConstant(3, MVT::i16));
6334
6335
Duncan Sands92c43912008-06-06 12:08:01 +00006336 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen24dd9a52009-02-07 00:55:49 +00006337 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006338}
6339
Dan Gohman8181bd12008-07-27 21:46:04 +00006340SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006341 MVT VT = Op.getValueType();
6342 MVT OpVT = VT;
6343 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006344 DebugLoc dl = Op.getDebugLoc();
Evan Cheng48679f42007-12-14 02:13:44 +00006345
6346 Op = Op.getOperand(0);
6347 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006348 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00006349 OpVT = MVT::i32;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006350 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006351 }
Evan Cheng48679f42007-12-14 02:13:44 +00006352
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006353 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6354 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006355 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006356
6357 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006358 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006359 Ops.push_back(Op);
6360 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6361 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6362 Ops.push_back(Op.getValue(1));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006363 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006364
6365 // Finally xor with NumBits-1.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006366 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006367
Evan Cheng48679f42007-12-14 02:13:44 +00006368 if (VT == MVT::i8)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006369 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006370 return Op;
6371}
6372
Dan Gohman8181bd12008-07-27 21:46:04 +00006373SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006374 MVT VT = Op.getValueType();
6375 MVT OpVT = VT;
6376 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006377 DebugLoc dl = Op.getDebugLoc();
Evan Cheng48679f42007-12-14 02:13:44 +00006378
6379 Op = Op.getOperand(0);
6380 if (VT == MVT::i8) {
6381 OpVT = MVT::i32;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006382 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006383 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006384
6385 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6386 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006387 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006388
6389 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006390 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006391 Ops.push_back(Op);
6392 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6393 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6394 Ops.push_back(Op.getValue(1));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006395 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006396
Evan Cheng48679f42007-12-14 02:13:44 +00006397 if (VT == MVT::i8)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006398 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006399 return Op;
6400}
6401
Mon P Wang14edb092008-12-18 21:42:19 +00006402SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6403 MVT VT = Op.getValueType();
6404 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006405 DebugLoc dl = Op.getDebugLoc();
Scott Michel91099d62009-02-17 22:15:04 +00006406
Mon P Wang14edb092008-12-18 21:42:19 +00006407 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6408 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6409 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6410 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6411 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6412 //
6413 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6414 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6415 // return AloBlo + AloBhi + AhiBlo;
6416
6417 SDValue A = Op.getOperand(0);
6418 SDValue B = Op.getOperand(1);
Scott Michel91099d62009-02-17 22:15:04 +00006419
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006420 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006421 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6422 A, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006423 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006424 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6425 B, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006426 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006427 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6428 A, B);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006429 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006430 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6431 A, Bhi);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006432 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006433 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6434 Ahi, B);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006435 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006436 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6437 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006438 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006439 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6440 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006441 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6442 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wang14edb092008-12-18 21:42:19 +00006443 return Res;
6444}
6445
6446
Bill Wendling7e04be62008-12-09 22:08:41 +00006447SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6448 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6449 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendlingd3511522008-12-02 01:06:39 +00006450 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6451 // has only one use.
Bill Wendlingd06b4202008-11-26 22:37:40 +00006452 SDNode *N = Op.getNode();
Bill Wendlingd3511522008-12-02 01:06:39 +00006453 SDValue LHS = N->getOperand(0);
6454 SDValue RHS = N->getOperand(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00006455 unsigned BaseOp = 0;
6456 unsigned Cond = 0;
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006457 DebugLoc dl = Op.getDebugLoc();
Bill Wendling7e04be62008-12-09 22:08:41 +00006458
6459 switch (Op.getOpcode()) {
6460 default: assert(0 && "Unknown ovf instruction!");
6461 case ISD::SADDO:
Dan Gohman99a12192009-03-04 19:44:21 +00006462 // A subtract of one will be selected as a INC. Note that INC doesn't
6463 // set CF, so we can't do this for UADDO.
6464 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6465 if (C->getAPIntValue() == 1) {
6466 BaseOp = X86ISD::INC;
6467 Cond = X86::COND_O;
6468 break;
6469 }
Bill Wendlingae034ed2008-12-12 00:56:36 +00006470 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00006471 Cond = X86::COND_O;
6472 break;
6473 case ISD::UADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006474 BaseOp = X86ISD::ADD;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006475 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006476 break;
6477 case ISD::SSUBO:
Dan Gohman99a12192009-03-04 19:44:21 +00006478 // A subtract of one will be selected as a DEC. Note that DEC doesn't
6479 // set CF, so we can't do this for USUBO.
6480 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6481 if (C->getAPIntValue() == 1) {
6482 BaseOp = X86ISD::DEC;
6483 Cond = X86::COND_O;
6484 break;
6485 }
Bill Wendlingae034ed2008-12-12 00:56:36 +00006486 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00006487 Cond = X86::COND_O;
6488 break;
6489 case ISD::USUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006490 BaseOp = X86ISD::SUB;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006491 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006492 break;
6493 case ISD::SMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006494 BaseOp = X86ISD::SMUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00006495 Cond = X86::COND_O;
6496 break;
6497 case ISD::UMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006498 BaseOp = X86ISD::UMUL;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006499 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006500 break;
6501 }
Bill Wendlingd06b4202008-11-26 22:37:40 +00006502
Bill Wendlingd3511522008-12-02 01:06:39 +00006503 // Also sets EFLAGS.
6504 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006505 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendlingd06b4202008-11-26 22:37:40 +00006506
Bill Wendlingd3511522008-12-02 01:06:39 +00006507 SDValue SetCC =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006508 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendling35f1a9d2008-12-10 02:01:32 +00006509 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendlingd06b4202008-11-26 22:37:40 +00006510
Bill Wendlingd3511522008-12-02 01:06:39 +00006511 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6512 return Sum;
Bill Wendling4c134df2008-11-24 19:21:46 +00006513}
6514
Dan Gohman8181bd12008-07-27 21:46:04 +00006515SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006516 MVT T = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006517 DebugLoc dl = Op.getDebugLoc();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00006518 unsigned Reg = 0;
6519 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006520 switch(T.getSimpleVT()) {
6521 default:
6522 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006523 case MVT::i8: Reg = X86::AL; size = 1; break;
6524 case MVT::i16: Reg = X86::AX; size = 2; break;
6525 case MVT::i32: Reg = X86::EAX; size = 4; break;
Scott Michel91099d62009-02-17 22:15:04 +00006526 case MVT::i64:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006527 assert(Subtarget->is64Bit() && "Node not type legal!");
6528 Reg = X86::RAX; size = 8;
Andrew Lenharth81580822008-03-05 01:15:49 +00006529 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00006530 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006531 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00006532 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00006533 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006534 Op.getOperand(1),
6535 Op.getOperand(3),
6536 DAG.getTargetConstant(size, MVT::i8),
6537 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006538 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006539 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michel91099d62009-02-17 22:15:04 +00006540 SDValue cpOut =
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006541 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006542 return cpOut;
6543}
6544
Duncan Sands7d9834b2008-12-01 11:39:25 +00006545SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif825aa892008-08-28 23:19:51 +00006546 SelectionDAG &DAG) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006547 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharth81580822008-03-05 01:15:49 +00006548 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006549 SDValue TheChain = Op.getOperand(0);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006550 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006551 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006552 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6553 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006554 rax.getValue(2));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006555 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006556 DAG.getConstant(32, MVT::i8));
6557 SDValue Ops[] = {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006558 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006559 rdx.getValue(1)
6560 };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006561 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesenf160d802008-10-02 18:53:47 +00006562}
6563
Dale Johannesen9011d872008-09-29 22:25:26 +00006564SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6565 SDNode *Node = Op.getNode();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006566 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen9011d872008-09-29 22:25:26 +00006567 MVT T = Node->getValueType(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006568 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Chengef356282009-02-23 09:03:22 +00006569 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006570 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006571 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen9011d872008-09-29 22:25:26 +00006572 Node->getOperand(0),
6573 Node->getOperand(1), negOp,
6574 cast<AtomicSDNode>(Node)->getSrcValue(),
6575 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006576}
6577
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006578/// LowerOperation - Provide custom lowering hooks for some operations.
6579///
Dan Gohman8181bd12008-07-27 21:46:04 +00006580SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006581 switch (Op.getOpcode()) {
6582 default: assert(0 && "Should not custom lower this!");
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006583 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6584 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006585 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6586 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6587 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6588 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6589 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6590 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6591 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6592 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006593 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006594 case ISD::SHL_PARTS:
6595 case ISD::SRA_PARTS:
6596 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6597 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00006598 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006599 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6600 case ISD::FABS: return LowerFABS(Op, DAG);
6601 case ISD::FNEG: return LowerFNEG(Op, DAG);
6602 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006603 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006604 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006605 case ISD::SELECT: return LowerSELECT(Op, DAG);
6606 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006607 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6608 case ISD::CALL: return LowerCALL(Op, DAG);
6609 case ISD::RET: return LowerRET(Op, DAG);
6610 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006611 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006612 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006613 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6614 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6615 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6616 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6617 case ISD::FRAME_TO_ARGS_OFFSET:
6618 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6619 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6620 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006621 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006622 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006623 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6624 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wang14edb092008-12-18 21:42:19 +00006625 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling7e04be62008-12-09 22:08:41 +00006626 case ISD::SADDO:
6627 case ISD::UADDO:
6628 case ISD::SSUBO:
6629 case ISD::USUBO:
6630 case ISD::SMULO:
6631 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006632 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006633 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006634}
6635
Duncan Sands7d9834b2008-12-01 11:39:25 +00006636void X86TargetLowering::
6637ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6638 SelectionDAG &DAG, unsigned NewOp) {
6639 MVT T = Node->getValueType(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006640 DebugLoc dl = Node->getDebugLoc();
Duncan Sands7d9834b2008-12-01 11:39:25 +00006641 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6642
6643 SDValue Chain = Node->getOperand(0);
6644 SDValue In1 = Node->getOperand(1);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006645 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006646 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006647 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006648 Node->getOperand(2), DAG.getIntPtrConstant(1));
6649 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6650 // have a MemOperand. Pass the info through as a normal operand.
6651 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6652 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6653 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006654 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006655 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006656 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006657 Results.push_back(Result.getValue(2));
6658}
6659
Duncan Sandsac496a12008-07-04 11:47:58 +00006660/// ReplaceNodeResults - Replace a node with an illegal result type
6661/// with a new node built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00006662void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6663 SmallVectorImpl<SDValue>&Results,
6664 SelectionDAG &DAG) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006665 DebugLoc dl = N->getDebugLoc();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006666 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006667 default:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006668 assert(false && "Do not know how to custom type legalize this operation!");
6669 return;
6670 case ISD::FP_TO_SINT: {
6671 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
6672 SDValue FIST = Vals.first, StackSlot = Vals.second;
6673 if (FIST.getNode() != 0) {
6674 MVT VT = N->getValueType(0);
6675 // Return a load from the stack slot.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006676 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006677 }
6678 return;
6679 }
6680 case ISD::READCYCLECOUNTER: {
6681 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6682 SDValue TheChain = N->getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006683 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Scott Michel91099d62009-02-17 22:15:04 +00006684 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006685 rd.getValue(1));
6686 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006687 eax.getValue(2));
6688 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6689 SDValue Ops[] = { eax, edx };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006690 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006691 Results.push_back(edx.getValue(1));
6692 return;
6693 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006694 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006695 MVT T = N->getValueType(0);
6696 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6697 SDValue cpInL, cpInH;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006698 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006699 DAG.getConstant(0, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006700 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006701 DAG.getConstant(1, MVT::i32));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006702 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6703 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006704 cpInL.getValue(1));
6705 SDValue swapInL, swapInH;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006706 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006707 DAG.getConstant(0, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006708 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006709 DAG.getConstant(1, MVT::i32));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006710 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006711 cpInH.getValue(1));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006712 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006713 swapInL.getValue(1));
6714 SDValue Ops[] = { swapInH.getValue(0),
6715 N->getOperand(1),
6716 swapInH.getValue(1) };
6717 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006718 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006719 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6720 MVT::i32, Result.getValue(1));
6721 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6722 MVT::i32, cpOutL.getValue(2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006723 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006724 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006725 Results.push_back(cpOutH.getValue(1));
6726 return;
6727 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006728 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006729 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6730 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006731 case ISD::ATOMIC_LOAD_AND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006732 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6733 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006734 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006735 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6736 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006737 case ISD::ATOMIC_LOAD_OR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006738 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6739 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006740 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006741 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6742 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006743 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006744 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6745 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006746 case ISD::ATOMIC_SWAP:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006747 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6748 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006749 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006750}
6751
6752const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6753 switch (Opcode) {
6754 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006755 case X86ISD::BSF: return "X86ISD::BSF";
6756 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006757 case X86ISD::SHLD: return "X86ISD::SHLD";
6758 case X86ISD::SHRD: return "X86ISD::SHRD";
6759 case X86ISD::FAND: return "X86ISD::FAND";
6760 case X86ISD::FOR: return "X86ISD::FOR";
6761 case X86ISD::FXOR: return "X86ISD::FXOR";
6762 case X86ISD::FSRL: return "X86ISD::FSRL";
6763 case X86ISD::FILD: return "X86ISD::FILD";
6764 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6765 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6766 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6767 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6768 case X86ISD::FLD: return "X86ISD::FLD";
6769 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006770 case X86ISD::CALL: return "X86ISD::CALL";
6771 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6772 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohman7fe9b7f2008-12-23 22:45:23 +00006773 case X86ISD::BT: return "X86ISD::BT";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006774 case X86ISD::CMP: return "X86ISD::CMP";
6775 case X86ISD::COMI: return "X86ISD::COMI";
6776 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6777 case X86ISD::SETCC: return "X86ISD::SETCC";
6778 case X86ISD::CMOV: return "X86ISD::CMOV";
6779 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6780 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6781 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6782 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006783 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6784 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006785 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006786 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006787 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6788 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006789 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begeman2c87c422009-02-23 08:49:38 +00006790 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006791 case X86ISD::FMAX: return "X86ISD::FMAX";
6792 case X86ISD::FMIN: return "X86ISD::FMIN";
6793 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6794 case X86ISD::FRCP: return "X86ISD::FRCP";
6795 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindolabca99f72009-04-08 21:14:34 +00006796 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006797 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006798 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006799 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006800 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6801 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006802 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6803 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6804 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6805 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6806 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6807 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006808 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6809 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006810 case X86ISD::VSHL: return "X86ISD::VSHL";
6811 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006812 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6813 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6814 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6815 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6816 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6817 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6818 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6819 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6820 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6821 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingae034ed2008-12-12 00:56:36 +00006822 case X86ISD::ADD: return "X86ISD::ADD";
6823 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingf5399032008-12-12 21:15:41 +00006824 case X86ISD::SMUL: return "X86ISD::SMUL";
6825 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman99a12192009-03-04 19:44:21 +00006826 case X86ISD::INC: return "X86ISD::INC";
6827 case X86ISD::DEC: return "X86ISD::DEC";
Evan Chengc3495762009-03-30 21:36:47 +00006828 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006829 }
6830}
6831
6832// isLegalAddressingMode - Return true if the addressing mode represented
6833// by AM is legal for this target, for a load/store of the specified type.
Scott Michel91099d62009-02-17 22:15:04 +00006834bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006835 const Type *Ty) const {
6836 // X86 supports extremely general addressing modes.
Scott Michel91099d62009-02-17 22:15:04 +00006837
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006838 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6839 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6840 return false;
Scott Michel91099d62009-02-17 22:15:04 +00006841
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006842 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006843 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006844 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6845 return false;
Dale Johannesen64660e92008-12-05 21:47:27 +00006846 // If BaseGV requires a register, we cannot also have a BaseReg.
6847 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6848 AM.HasBaseReg)
6849 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006850
6851 // X86-64 only supports addr of globals in small code model.
6852 if (Subtarget->is64Bit()) {
6853 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6854 return false;
6855 // If lower 4G is not available, then we must use rip-relative addressing.
6856 if (AM.BaseOffs || AM.Scale > 1)
6857 return false;
6858 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006859 }
Scott Michel91099d62009-02-17 22:15:04 +00006860
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006861 switch (AM.Scale) {
6862 case 0:
6863 case 1:
6864 case 2:
6865 case 4:
6866 case 8:
6867 // These scales always work.
6868 break;
6869 case 3:
6870 case 5:
6871 case 9:
6872 // These scales are formed with basereg+scalereg. Only accept if there is
6873 // no basereg yet.
6874 if (AM.HasBaseReg)
6875 return false;
6876 break;
6877 default: // Other stuff never works.
6878 return false;
6879 }
Scott Michel91099d62009-02-17 22:15:04 +00006880
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006881 return true;
6882}
6883
6884
Evan Cheng27a820a2007-10-26 01:56:11 +00006885bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6886 if (!Ty1->isInteger() || !Ty2->isInteger())
6887 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006888 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6889 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006890 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006891 return false;
6892 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006893}
6894
Duncan Sands92c43912008-06-06 12:08:01 +00006895bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6896 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006897 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006898 unsigned NumBits1 = VT1.getSizeInBits();
6899 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006900 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006901 return false;
6902 return Subtarget->is64Bit() || NumBits1 < 64;
6903}
Evan Cheng27a820a2007-10-26 01:56:11 +00006904
Dan Gohman4cedb1c2009-04-08 00:15:30 +00006905bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohmanb044da32009-04-09 02:06:09 +00006906 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman4cedb1c2009-04-08 00:15:30 +00006907 return Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit();
6908}
6909
6910bool X86TargetLowering::isZExtFree(MVT VT1, MVT VT2) const {
Dan Gohmanb044da32009-04-09 02:06:09 +00006911 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman4cedb1c2009-04-08 00:15:30 +00006912 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
6913}
6914
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006915/// isShuffleMaskLegal - Targets can use this to indicate that they only
6916/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6917/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6918/// are assumed to be legal.
6919bool
Nate Begemane8f61cb2009-04-29 05:20:52 +00006920X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
6921 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006922 // Only do shuffles on 128-bit vector types for now.
Nate Begeman543d2142009-04-27 18:41:29 +00006923 if (VT.getSizeInBits() == 64)
6924 return false;
6925
6926 // FIXME: pshufb, blends, palignr, shifts.
6927 return (VT.getVectorNumElements() == 2 ||
6928 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
6929 isMOVLMask(M, VT) ||
6930 isSHUFPMask(M, VT) ||
6931 isPSHUFDMask(M, VT) ||
6932 isPSHUFHWMask(M, VT) ||
6933 isPSHUFLWMask(M, VT) ||
6934 isUNPCKLMask(M, VT) ||
6935 isUNPCKHMask(M, VT) ||
6936 isUNPCKL_v_undef_Mask(M, VT) ||
6937 isUNPCKH_v_undef_Mask(M, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006938}
6939
Dan Gohman48d5f062008-04-09 20:09:42 +00006940bool
Nate Begemane8f61cb2009-04-29 05:20:52 +00006941X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Nate Begeman543d2142009-04-27 18:41:29 +00006942 MVT VT) const {
6943 unsigned NumElts = VT.getVectorNumElements();
6944 // FIXME: This collection of masks seems suspect.
6945 if (NumElts == 2)
6946 return true;
6947 if (NumElts == 4 && VT.getSizeInBits() == 128) {
6948 return (isMOVLMask(Mask, VT) ||
6949 isCommutedMOVLMask(Mask, VT, true) ||
6950 isSHUFPMask(Mask, VT) ||
6951 isCommutedSHUFPMask(Mask, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006952 }
6953 return false;
6954}
6955
6956//===----------------------------------------------------------------------===//
6957// X86 Scheduler Hooks
6958//===----------------------------------------------------------------------===//
6959
Mon P Wang078a62d2008-05-05 19:05:59 +00006960// private utility function
6961MachineBasicBlock *
6962X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6963 MachineBasicBlock *MBB,
6964 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006965 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006966 unsigned LoadOpc,
6967 unsigned CXchgOpc,
6968 unsigned copyOpc,
6969 unsigned notOpc,
6970 unsigned EAXreg,
6971 TargetRegisterClass *RC,
Dan Gohman96d60922009-02-07 16:15:20 +00006972 bool invSrc) const {
Mon P Wang078a62d2008-05-05 19:05:59 +00006973 // For the atomic bitwise operator, we generate
6974 // thisMBB:
6975 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006976 // ld t1 = [bitinstr.addr]
6977 // op t2 = t1, [bitinstr.val]
6978 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006979 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6980 // bz newMBB
6981 // fallthrough -->nextMBB
6982 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6983 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006984 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006985 ++MBBIter;
Scott Michel91099d62009-02-17 22:15:04 +00006986
Mon P Wang078a62d2008-05-05 19:05:59 +00006987 /// First build the CFG
6988 MachineFunction *F = MBB->getParent();
6989 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006990 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6991 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6992 F->insert(MBBIter, newMBB);
6993 F->insert(MBBIter, nextMBB);
Scott Michel91099d62009-02-17 22:15:04 +00006994
Mon P Wang078a62d2008-05-05 19:05:59 +00006995 // Move all successors to thisMBB to nextMBB
6996 nextMBB->transferSuccessors(thisMBB);
Scott Michel91099d62009-02-17 22:15:04 +00006997
Mon P Wang078a62d2008-05-05 19:05:59 +00006998 // Update thisMBB to fall through to newMBB
6999 thisMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007000
Mon P Wang078a62d2008-05-05 19:05:59 +00007001 // newMBB jumps to itself and fall through to nextMBB
7002 newMBB->addSuccessor(nextMBB);
7003 newMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007004
Mon P Wang078a62d2008-05-05 19:05:59 +00007005 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007006 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
7007 "unexpected number of operands");
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007008 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang078a62d2008-05-05 19:05:59 +00007009 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007010 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang078a62d2008-05-05 19:05:59 +00007011 int numArgs = bInstr->getNumOperands() - 1;
7012 for (int i=0; i < numArgs; ++i)
7013 argOpers[i] = &bInstr->getOperand(i+1);
7014
7015 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007016 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7017 int valArgIndx = lastAddrIndx + 1;
Scott Michel91099d62009-02-17 22:15:04 +00007018
Dale Johannesend20e4452008-08-19 18:47:28 +00007019 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007020 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00007021 for (int i=0; i <= lastAddrIndx; ++i)
7022 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007023
Dale Johannesend20e4452008-08-19 18:47:28 +00007024 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007025 if (invSrc) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007026 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007027 }
Scott Michel91099d62009-02-17 22:15:04 +00007028 else
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007029 tt = t1;
7030
Dale Johannesend20e4452008-08-19 18:47:28 +00007031 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007032 assert((argOpers[valArgIndx]->isReg() ||
7033 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00007034 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007035 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007036 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00007037 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007038 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007039 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00007040 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007041
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007042 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00007043 MIB.addReg(t1);
Scott Michel91099d62009-02-17 22:15:04 +00007044
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007045 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00007046 for (int i=0; i <= lastAddrIndx; ++i)
7047 (*MIB).addOperand(*argOpers[i]);
7048 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00007049 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7050 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7051
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007052 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesend20e4452008-08-19 18:47:28 +00007053 MIB.addReg(EAXreg);
Scott Michel91099d62009-02-17 22:15:04 +00007054
Mon P Wang078a62d2008-05-05 19:05:59 +00007055 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007056 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007057
Dan Gohman221a4372008-07-07 23:14:23 +00007058 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00007059 return nextMBB;
7060}
7061
Dale Johannesen44eb5372008-10-03 19:41:08 +00007062// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00007063MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00007064X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7065 MachineBasicBlock *MBB,
7066 unsigned regOpcL,
7067 unsigned regOpcH,
7068 unsigned immOpcL,
7069 unsigned immOpcH,
Dan Gohman96d60922009-02-07 16:15:20 +00007070 bool invSrc) const {
Dale Johannesenf160d802008-10-02 18:53:47 +00007071 // For the atomic bitwise operator, we generate
7072 // thisMBB (instructions are in pairs, except cmpxchg8b)
7073 // ld t1,t2 = [bitinstr.addr]
7074 // newMBB:
7075 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7076 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007077 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00007078 // mov ECX, EBX <- t5, t6
7079 // mov EAX, EDX <- t1, t2
7080 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7081 // mov t3, t4 <- EAX, EDX
7082 // bz newMBB
7083 // result in out1, out2
7084 // fallthrough -->nextMBB
7085
7086 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7087 const unsigned LoadOpc = X86::MOV32rm;
7088 const unsigned copyOpc = X86::MOV32rr;
7089 const unsigned NotOpc = X86::NOT32r;
7090 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7091 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7092 MachineFunction::iterator MBBIter = MBB;
7093 ++MBBIter;
Scott Michel91099d62009-02-17 22:15:04 +00007094
Dale Johannesenf160d802008-10-02 18:53:47 +00007095 /// First build the CFG
7096 MachineFunction *F = MBB->getParent();
7097 MachineBasicBlock *thisMBB = MBB;
7098 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7099 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7100 F->insert(MBBIter, newMBB);
7101 F->insert(MBBIter, nextMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007102
Dale Johannesenf160d802008-10-02 18:53:47 +00007103 // Move all successors to thisMBB to nextMBB
7104 nextMBB->transferSuccessors(thisMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007105
Dale Johannesenf160d802008-10-02 18:53:47 +00007106 // Update thisMBB to fall through to newMBB
7107 thisMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007108
Dale Johannesenf160d802008-10-02 18:53:47 +00007109 // newMBB jumps to itself and fall through to nextMBB
7110 newMBB->addSuccessor(nextMBB);
7111 newMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007112
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007113 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesenf160d802008-10-02 18:53:47 +00007114 // Insert instructions into newMBB based on incoming instruction
7115 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007116 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
7117 "unexpected number of operands");
Dale Johannesenf160d802008-10-02 18:53:47 +00007118 MachineOperand& dest1Oper = bInstr->getOperand(0);
7119 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007120 MachineOperand* argOpers[2 + X86AddrNumOperands];
7121 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesenf160d802008-10-02 18:53:47 +00007122 argOpers[i] = &bInstr->getOperand(i+2);
7123
7124 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007125 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michel91099d62009-02-17 22:15:04 +00007126
Dale Johannesenf160d802008-10-02 18:53:47 +00007127 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007128 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesenf160d802008-10-02 18:53:47 +00007129 for (int i=0; i <= lastAddrIndx; ++i)
7130 (*MIB).addOperand(*argOpers[i]);
7131 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007132 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007133 // add 4 to displacement.
Rafael Espindolabca99f72009-04-08 21:14:34 +00007134 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesenf160d802008-10-02 18:53:47 +00007135 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007136 MachineOperand newOp3 = *(argOpers[3]);
7137 if (newOp3.isImm())
7138 newOp3.setImm(newOp3.getImm()+4);
7139 else
7140 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00007141 (*MIB).addOperand(newOp3);
Rafael Espindolabca99f72009-04-08 21:14:34 +00007142 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesenf160d802008-10-02 18:53:47 +00007143
7144 // t3/4 are defined later, at the bottom of the loop
7145 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7146 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007147 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007148 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007149 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007150 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7151
7152 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7153 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
Scott Michel91099d62009-02-17 22:15:04 +00007154 if (invSrc) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007155 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7156 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesenf160d802008-10-02 18:53:47 +00007157 } else {
7158 tt1 = t1;
7159 tt2 = t2;
7160 }
7161
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007162 int valArgIndx = lastAddrIndx + 1;
7163 assert((argOpers[valArgIndx]->isReg() ||
7164 argOpers[valArgIndx]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00007165 "invalid operand");
7166 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7167 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007168 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007169 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesenf160d802008-10-02 18:53:47 +00007170 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007171 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007172 if (regOpcL != X86::MOV32rr)
7173 MIB.addReg(tt1);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007174 (*MIB).addOperand(*argOpers[valArgIndx]);
7175 assert(argOpers[valArgIndx + 1]->isReg() ==
7176 argOpers[valArgIndx]->isReg());
7177 assert(argOpers[valArgIndx + 1]->isImm() ==
7178 argOpers[valArgIndx]->isImm());
7179 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007180 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesenf160d802008-10-02 18:53:47 +00007181 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007182 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007183 if (regOpcH != X86::MOV32rr)
7184 MIB.addReg(tt2);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007185 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesenf160d802008-10-02 18:53:47 +00007186
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007187 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007188 MIB.addReg(t1);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007189 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007190 MIB.addReg(t2);
7191
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007192 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007193 MIB.addReg(t5);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007194 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007195 MIB.addReg(t6);
Scott Michel91099d62009-02-17 22:15:04 +00007196
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007197 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesenf160d802008-10-02 18:53:47 +00007198 for (int i=0; i <= lastAddrIndx; ++i)
7199 (*MIB).addOperand(*argOpers[i]);
7200
7201 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7202 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7203
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007204 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesenf160d802008-10-02 18:53:47 +00007205 MIB.addReg(X86::EAX);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007206 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesenf160d802008-10-02 18:53:47 +00007207 MIB.addReg(X86::EDX);
Scott Michel91099d62009-02-17 22:15:04 +00007208
Dale Johannesenf160d802008-10-02 18:53:47 +00007209 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007210 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesenf160d802008-10-02 18:53:47 +00007211
7212 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7213 return nextMBB;
7214}
7215
7216// private utility function
7217MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00007218X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7219 MachineBasicBlock *MBB,
Dan Gohman96d60922009-02-07 16:15:20 +00007220 unsigned cmovOpc) const {
Mon P Wang078a62d2008-05-05 19:05:59 +00007221 // For the atomic min/max operator, we generate
7222 // thisMBB:
7223 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00007224 // ld t1 = [min/max.addr]
Scott Michel91099d62009-02-17 22:15:04 +00007225 // mov t2 = [min/max.val]
Mon P Wang078a62d2008-05-05 19:05:59 +00007226 // cmp t1, t2
7227 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00007228 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00007229 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7230 // bz newMBB
7231 // fallthrough -->nextMBB
7232 //
7233 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7234 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007235 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00007236 ++MBBIter;
Scott Michel91099d62009-02-17 22:15:04 +00007237
Mon P Wang078a62d2008-05-05 19:05:59 +00007238 /// First build the CFG
7239 MachineFunction *F = MBB->getParent();
7240 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00007241 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7242 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7243 F->insert(MBBIter, newMBB);
7244 F->insert(MBBIter, nextMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007245
Mon P Wang078a62d2008-05-05 19:05:59 +00007246 // Move all successors to thisMBB to nextMBB
7247 nextMBB->transferSuccessors(thisMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007248
Mon P Wang078a62d2008-05-05 19:05:59 +00007249 // Update thisMBB to fall through to newMBB
7250 thisMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007251
Mon P Wang078a62d2008-05-05 19:05:59 +00007252 // newMBB jumps to newMBB and fall through to nextMBB
7253 newMBB->addSuccessor(nextMBB);
7254 newMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007255
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007256 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang078a62d2008-05-05 19:05:59 +00007257 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007258 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
7259 "unexpected number of operands");
Mon P Wang078a62d2008-05-05 19:05:59 +00007260 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007261 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang078a62d2008-05-05 19:05:59 +00007262 int numArgs = mInstr->getNumOperands() - 1;
7263 for (int i=0; i < numArgs; ++i)
7264 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michel91099d62009-02-17 22:15:04 +00007265
Mon P Wang078a62d2008-05-05 19:05:59 +00007266 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007267 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7268 int valArgIndx = lastAddrIndx + 1;
Scott Michel91099d62009-02-17 22:15:04 +00007269
Mon P Wang318b0372008-05-05 22:56:23 +00007270 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007271 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00007272 for (int i=0; i <= lastAddrIndx; ++i)
7273 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00007274
Mon P Wang078a62d2008-05-05 19:05:59 +00007275 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007276 assert((argOpers[valArgIndx]->isReg() ||
7277 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00007278 "invalid operand");
Scott Michel91099d62009-02-17 22:15:04 +00007279
7280 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007281 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007282 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michel91099d62009-02-17 22:15:04 +00007283 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007284 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00007285 (*MIB).addOperand(*argOpers[valArgIndx]);
7286
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007287 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wang318b0372008-05-05 22:56:23 +00007288 MIB.addReg(t1);
7289
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007290 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang078a62d2008-05-05 19:05:59 +00007291 MIB.addReg(t1);
7292 MIB.addReg(t2);
7293
7294 // Generate movc
7295 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007296 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang078a62d2008-05-05 19:05:59 +00007297 MIB.addReg(t2);
7298 MIB.addReg(t1);
7299
7300 // Cmp and exchange if none has modified the memory location
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007301 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang078a62d2008-05-05 19:05:59 +00007302 for (int i=0; i <= lastAddrIndx; ++i)
7303 (*MIB).addOperand(*argOpers[i]);
7304 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00007305 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7306 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Scott Michel91099d62009-02-17 22:15:04 +00007307
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007308 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang078a62d2008-05-05 19:05:59 +00007309 MIB.addReg(X86::EAX);
Scott Michel91099d62009-02-17 22:15:04 +00007310
Mon P Wang078a62d2008-05-05 19:05:59 +00007311 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007312 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007313
Dan Gohman221a4372008-07-07 23:14:23 +00007314 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00007315 return nextMBB;
7316}
7317
7318
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007319MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00007320X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman96d60922009-02-07 16:15:20 +00007321 MachineBasicBlock *BB) const {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007322 DebugLoc dl = MI->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007323 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7324 switch (MI->getOpcode()) {
7325 default: assert(false && "Unexpected instr type to insert");
Mon P Wang83edba52008-12-12 01:25:51 +00007326 case X86::CMOV_V1I64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007327 case X86::CMOV_FR32:
7328 case X86::CMOV_FR64:
7329 case X86::CMOV_V4F32:
7330 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00007331 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007332 // To "insert" a SELECT_CC instruction, we actually have to insert the
7333 // diamond control-flow pattern. The incoming instruction knows the
7334 // destination vreg to set, the condition code register to branch on, the
7335 // true/false values to select between, and a branch opcode to use.
7336 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007337 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007338 ++It;
7339
7340 // thisMBB:
7341 // ...
7342 // TrueVal = ...
7343 // cmpTY ccX, r1, r2
7344 // bCC copy1MBB
7345 // fallthrough --> copy0MBB
7346 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00007347 MachineFunction *F = BB->getParent();
7348 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7349 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007350 unsigned Opc =
7351 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007352 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00007353 F->insert(It, copy0MBB);
7354 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007355 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007356 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00007357 sinkMBB->transferSuccessors(BB);
7358
7359 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007360 BB->addSuccessor(copy0MBB);
7361 BB->addSuccessor(sinkMBB);
7362
7363 // copy0MBB:
7364 // %FalseValue = ...
7365 // # fallthrough to sinkMBB
7366 BB = copy0MBB;
7367
7368 // Update machine-CFG edges
7369 BB->addSuccessor(sinkMBB);
7370
7371 // sinkMBB:
7372 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7373 // ...
7374 BB = sinkMBB;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007375 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007376 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7377 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7378
Dan Gohman221a4372008-07-07 23:14:23 +00007379 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007380 return BB;
7381 }
7382
7383 case X86::FP32_TO_INT16_IN_MEM:
7384 case X86::FP32_TO_INT32_IN_MEM:
7385 case X86::FP32_TO_INT64_IN_MEM:
7386 case X86::FP64_TO_INT16_IN_MEM:
7387 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007388 case X86::FP64_TO_INT64_IN_MEM:
7389 case X86::FP80_TO_INT16_IN_MEM:
7390 case X86::FP80_TO_INT32_IN_MEM:
7391 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007392 // Change the floating point control register to use "round towards zero"
7393 // mode when truncating to an integer value.
7394 MachineFunction *F = BB->getParent();
7395 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007396 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007397
7398 // Load the old value of the high byte of the control word...
7399 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00007400 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Scott Michel91099d62009-02-17 22:15:04 +00007401 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007402 CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007403
7404 // Set the high part to be round to zero...
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007405 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007406 .addImm(0xC7F);
7407
7408 // Reload the modified control word now...
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007409 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007410
7411 // Restore the memory image of control word to original value
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007412 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007413 .addReg(OldCW);
7414
7415 // Get the X86 opcode to use.
7416 unsigned Opc;
7417 switch (MI->getOpcode()) {
7418 default: assert(0 && "illegal opcode!");
7419 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7420 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7421 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7422 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7423 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7424 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007425 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7426 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7427 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007428 }
7429
7430 X86AddressMode AM;
7431 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007432 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007433 AM.BaseType = X86AddressMode::RegBase;
7434 AM.Base.Reg = Op.getReg();
7435 } else {
7436 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00007437 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007438 }
7439 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007440 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007441 AM.Scale = Op.getImm();
7442 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007443 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007444 AM.IndexReg = Op.getImm();
7445 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007446 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007447 AM.GV = Op.getGlobal();
7448 } else {
7449 AM.Disp = Op.getImm();
7450 }
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007451 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Rafael Espindolafee9c0f2009-04-08 08:09:33 +00007452 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007453
7454 // Reload the original control word now.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007455 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007456
Dan Gohman221a4372008-07-07 23:14:23 +00007457 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007458 return BB;
7459 }
Mon P Wang078a62d2008-05-05 19:05:59 +00007460 case X86::ATOMAND32:
7461 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michel91099d62009-02-17 22:15:04 +00007462 X86::AND32ri, X86::MOV32rm,
Dale Johannesend20e4452008-08-19 18:47:28 +00007463 X86::LCMPXCHG32, X86::MOV32rr,
7464 X86::NOT32r, X86::EAX,
7465 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007466 case X86::ATOMOR32:
Scott Michel91099d62009-02-17 22:15:04 +00007467 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
7468 X86::OR32ri, X86::MOV32rm,
Dale Johannesend20e4452008-08-19 18:47:28 +00007469 X86::LCMPXCHG32, X86::MOV32rr,
7470 X86::NOT32r, X86::EAX,
7471 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007472 case X86::ATOMXOR32:
7473 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michel91099d62009-02-17 22:15:04 +00007474 X86::XOR32ri, X86::MOV32rm,
Dale Johannesend20e4452008-08-19 18:47:28 +00007475 X86::LCMPXCHG32, X86::MOV32rr,
7476 X86::NOT32r, X86::EAX,
7477 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007478 case X86::ATOMNAND32:
7479 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007480 X86::AND32ri, X86::MOV32rm,
7481 X86::LCMPXCHG32, X86::MOV32rr,
7482 X86::NOT32r, X86::EAX,
7483 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00007484 case X86::ATOMMIN32:
7485 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7486 case X86::ATOMMAX32:
7487 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7488 case X86::ATOMUMIN32:
7489 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7490 case X86::ATOMUMAX32:
7491 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00007492
7493 case X86::ATOMAND16:
7494 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7495 X86::AND16ri, X86::MOV16rm,
7496 X86::LCMPXCHG16, X86::MOV16rr,
7497 X86::NOT16r, X86::AX,
7498 X86::GR16RegisterClass);
7499 case X86::ATOMOR16:
Scott Michel91099d62009-02-17 22:15:04 +00007500 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007501 X86::OR16ri, X86::MOV16rm,
7502 X86::LCMPXCHG16, X86::MOV16rr,
7503 X86::NOT16r, X86::AX,
7504 X86::GR16RegisterClass);
7505 case X86::ATOMXOR16:
7506 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7507 X86::XOR16ri, X86::MOV16rm,
7508 X86::LCMPXCHG16, X86::MOV16rr,
7509 X86::NOT16r, X86::AX,
7510 X86::GR16RegisterClass);
7511 case X86::ATOMNAND16:
7512 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7513 X86::AND16ri, X86::MOV16rm,
7514 X86::LCMPXCHG16, X86::MOV16rr,
7515 X86::NOT16r, X86::AX,
7516 X86::GR16RegisterClass, true);
7517 case X86::ATOMMIN16:
7518 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7519 case X86::ATOMMAX16:
7520 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7521 case X86::ATOMUMIN16:
7522 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7523 case X86::ATOMUMAX16:
7524 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7525
7526 case X86::ATOMAND8:
7527 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7528 X86::AND8ri, X86::MOV8rm,
7529 X86::LCMPXCHG8, X86::MOV8rr,
7530 X86::NOT8r, X86::AL,
7531 X86::GR8RegisterClass);
7532 case X86::ATOMOR8:
Scott Michel91099d62009-02-17 22:15:04 +00007533 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007534 X86::OR8ri, X86::MOV8rm,
7535 X86::LCMPXCHG8, X86::MOV8rr,
7536 X86::NOT8r, X86::AL,
7537 X86::GR8RegisterClass);
7538 case X86::ATOMXOR8:
7539 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7540 X86::XOR8ri, X86::MOV8rm,
7541 X86::LCMPXCHG8, X86::MOV8rr,
7542 X86::NOT8r, X86::AL,
7543 X86::GR8RegisterClass);
7544 case X86::ATOMNAND8:
7545 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7546 X86::AND8ri, X86::MOV8rm,
7547 X86::LCMPXCHG8, X86::MOV8rr,
7548 X86::NOT8r, X86::AL,
7549 X86::GR8RegisterClass, true);
7550 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00007551 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007552 case X86::ATOMAND64:
7553 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michel91099d62009-02-17 22:15:04 +00007554 X86::AND64ri32, X86::MOV64rm,
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007555 X86::LCMPXCHG64, X86::MOV64rr,
7556 X86::NOT64r, X86::RAX,
7557 X86::GR64RegisterClass);
7558 case X86::ATOMOR64:
Scott Michel91099d62009-02-17 22:15:04 +00007559 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7560 X86::OR64ri32, X86::MOV64rm,
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007561 X86::LCMPXCHG64, X86::MOV64rr,
7562 X86::NOT64r, X86::RAX,
7563 X86::GR64RegisterClass);
7564 case X86::ATOMXOR64:
7565 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michel91099d62009-02-17 22:15:04 +00007566 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007567 X86::LCMPXCHG64, X86::MOV64rr,
7568 X86::NOT64r, X86::RAX,
7569 X86::GR64RegisterClass);
7570 case X86::ATOMNAND64:
7571 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7572 X86::AND64ri32, X86::MOV64rm,
7573 X86::LCMPXCHG64, X86::MOV64rr,
7574 X86::NOT64r, X86::RAX,
7575 X86::GR64RegisterClass, true);
7576 case X86::ATOMMIN64:
7577 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7578 case X86::ATOMMAX64:
7579 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7580 case X86::ATOMUMIN64:
7581 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7582 case X86::ATOMUMAX64:
7583 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00007584
7585 // This group does 64-bit operations on a 32-bit host.
7586 case X86::ATOMAND6432:
Scott Michel91099d62009-02-17 22:15:04 +00007587 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00007588 X86::AND32rr, X86::AND32rr,
7589 X86::AND32ri, X86::AND32ri,
7590 false);
7591 case X86::ATOMOR6432:
Scott Michel91099d62009-02-17 22:15:04 +00007592 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00007593 X86::OR32rr, X86::OR32rr,
7594 X86::OR32ri, X86::OR32ri,
7595 false);
7596 case X86::ATOMXOR6432:
Scott Michel91099d62009-02-17 22:15:04 +00007597 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00007598 X86::XOR32rr, X86::XOR32rr,
7599 X86::XOR32ri, X86::XOR32ri,
7600 false);
7601 case X86::ATOMNAND6432:
Scott Michel91099d62009-02-17 22:15:04 +00007602 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00007603 X86::AND32rr, X86::AND32rr,
7604 X86::AND32ri, X86::AND32ri,
7605 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00007606 case X86::ATOMADD6432:
Scott Michel91099d62009-02-17 22:15:04 +00007607 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00007608 X86::ADD32rr, X86::ADC32rr,
7609 X86::ADD32ri, X86::ADC32ri,
7610 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00007611 case X86::ATOMSUB6432:
Scott Michel91099d62009-02-17 22:15:04 +00007612 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00007613 X86::SUB32rr, X86::SBB32rr,
7614 X86::SUB32ri, X86::SBB32ri,
7615 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007616 case X86::ATOMSWAP6432:
Scott Michel91099d62009-02-17 22:15:04 +00007617 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007618 X86::MOV32rr, X86::MOV32rr,
7619 X86::MOV32ri, X86::MOV32ri,
7620 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007621 }
7622}
7623
7624//===----------------------------------------------------------------------===//
7625// X86 Optimization Hooks
7626//===----------------------------------------------------------------------===//
7627
Dan Gohman8181bd12008-07-27 21:46:04 +00007628void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00007629 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00007630 APInt &KnownZero,
7631 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007632 const SelectionDAG &DAG,
7633 unsigned Depth) const {
7634 unsigned Opc = Op.getOpcode();
7635 assert((Opc >= ISD::BUILTIN_OP_END ||
7636 Opc == ISD::INTRINSIC_WO_CHAIN ||
7637 Opc == ISD::INTRINSIC_W_CHAIN ||
7638 Opc == ISD::INTRINSIC_VOID) &&
7639 "Should use MaskedValueIsZero if you don't know whether Op"
7640 " is a target node!");
7641
Dan Gohman1d79e432008-02-13 23:07:24 +00007642 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007643 switch (Opc) {
7644 default: break;
Evan Cheng8e9b21c2009-02-02 09:15:04 +00007645 case X86ISD::ADD:
7646 case X86ISD::SUB:
7647 case X86ISD::SMUL:
7648 case X86ISD::UMUL:
Dan Gohman99a12192009-03-04 19:44:21 +00007649 case X86ISD::INC:
7650 case X86ISD::DEC:
Evan Cheng8e9b21c2009-02-02 09:15:04 +00007651 // These nodes' second result is a boolean.
7652 if (Op.getResNo() == 0)
7653 break;
7654 // Fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007655 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007656 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7657 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007658 break;
7659 }
7660}
7661
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007662/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007663/// node is a GlobalAddress + offset.
7664bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7665 GlobalValue* &GA, int64_t &Offset) const{
7666 if (N->getOpcode() == X86ISD::Wrapper) {
7667 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007668 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00007669 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007670 return true;
7671 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007672 }
Evan Chengef7be082008-05-12 19:56:52 +00007673 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007674}
7675
Evan Chengef7be082008-05-12 19:56:52 +00007676static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7677 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007678 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007679 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007680 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007681 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007682 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007683 return false;
7684}
7685
Nate Begeman543d2142009-04-27 18:41:29 +00007686static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
7687 MVT EVT, SDNode *&Base,
Evan Chengef7be082008-05-12 19:56:52 +00007688 SelectionDAG &DAG, MachineFrameInfo *MFI,
7689 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007690 Base = NULL;
7691 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00007692 if (N->getMaskElt(i) < 0) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007693 if (!Base)
7694 return false;
7695 continue;
7696 }
7697
Dan Gohman8181bd12008-07-27 21:46:04 +00007698 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007699 if (!Elt.getNode() ||
7700 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007701 return false;
7702 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007703 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007704 if (Base->getOpcode() == ISD::UNDEF)
7705 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007706 continue;
7707 }
7708 if (Elt.getOpcode() == ISD::UNDEF)
7709 continue;
7710
Gabor Greif1c80d112008-08-28 21:40:38 +00007711 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007712 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007713 return false;
7714 }
7715 return true;
7716}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007717
7718/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7719/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7720/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang6e30ad02009-04-03 02:43:30 +00007721/// order. In the case of v2i64, it will see if it can rewrite the
7722/// shuffle to be an appropriate build vector so it can take advantage of
7723// performBuildVectorCombine.
Dan Gohman8181bd12008-07-27 21:46:04 +00007724static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman543d2142009-04-27 18:41:29 +00007725 const TargetLowering &TLI) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007726 DebugLoc dl = N->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00007727 MVT VT = N->getValueType(0);
7728 MVT EVT = VT.getVectorElementType();
Nate Begeman543d2142009-04-27 18:41:29 +00007729 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7730 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang6e30ad02009-04-03 02:43:30 +00007731
7732 // For x86-32 machines, if we see an insert and then a shuffle in a v2i64
7733 // where the upper half is 0, it is advantageous to rewrite it as a build
7734 // vector of (0, val) so it can use movq.
7735 if (VT == MVT::v2i64) {
7736 SDValue In[2];
7737 In[0] = N->getOperand(0);
7738 In[1] = N->getOperand(1);
Nate Begeman543d2142009-04-27 18:41:29 +00007739 int Idx0 = SVN->getMaskElt(0);
7740 int Idx1 = SVN->getMaskElt(1);
7741 // FIXME: can we take advantage of undef index?
7742 if (Idx0 >= 0 && Idx1 >= 0 &&
Mon P Wang6e30ad02009-04-03 02:43:30 +00007743 In[Idx0/2].getOpcode() == ISD::INSERT_VECTOR_ELT &&
7744 In[Idx1/2].getOpcode() == ISD::BUILD_VECTOR) {
7745 ConstantSDNode* InsertVecIdx =
7746 dyn_cast<ConstantSDNode>(In[Idx0/2].getOperand(2));
7747 if (InsertVecIdx &&
Nate Begeman543d2142009-04-27 18:41:29 +00007748 InsertVecIdx->getZExtValue() == (unsigned)(Idx0 % 2) &&
Mon P Wang6e30ad02009-04-03 02:43:30 +00007749 isZeroNode(In[Idx1/2].getOperand(Idx1 % 2))) {
7750 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
7751 In[Idx0/2].getOperand(1),
7752 In[Idx1/2].getOperand(Idx1 % 2));
7753 }
7754 }
7755 }
7756
7757 // Try to combine a vector_shuffle into a 128-bit load.
7758 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007759 SDNode *Base = NULL;
Nate Begeman543d2142009-04-27 18:41:29 +00007760 if (!EltsFromConsecutiveLoads(SVN, NumElems, EVT, Base, DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007761 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007762
Dan Gohman11821702007-07-27 17:16:43 +00007763 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007764 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007765 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michel91099d62009-02-17 22:15:04 +00007766 LD->getSrcValue(), LD->getSrcValueOffset(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007767 LD->isVolatile());
7768 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7769 LD->getSrcValue(), LD->getSrcValueOffset(),
7770 LD->isVolatile(), LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007771}
7772
Evan Chengb6290462008-05-12 23:04:07 +00007773/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007774static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohman22cefb02009-01-29 01:59:02 +00007775 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng6617eed2008-09-24 23:26:36 +00007776 const X86Subtarget *Subtarget,
7777 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007778 unsigned NumOps = N->getNumOperands();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007779 DebugLoc dl = N->getDebugLoc();
Evan Chengdea99362008-05-29 08:22:04 +00007780
Evan Chenge9b9c672008-05-09 21:53:03 +00007781 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007782 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007783 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007784
Duncan Sands92c43912008-06-06 12:08:01 +00007785 MVT VT = N->getValueType(0);
7786 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007787 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7788 // We are looking for load i64 and zero extend. We want to transform
7789 // it before legalizer has a chance to expand it. Also look for i64
7790 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007791 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007792 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007793 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007794 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007795 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007796
7797 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007798 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007799 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007800 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007801 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007802 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007803 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007804 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007805 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007806
7807 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007808 LoadSDNode *LD = cast<LoadSDNode>(Base);
Scott Michel91099d62009-02-17 22:15:04 +00007809
Nate Begeman211c4742008-05-28 00:24:25 +00007810 // Load must not be an extload.
7811 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007812 return SDValue();
Mon P Wang28b36412009-01-30 07:07:40 +00007813
7814 // Load type should legal type so we don't have to legalize it.
7815 if (!TLI.isTypeLegal(VT))
7816 return SDValue();
7817
Evan Cheng6617eed2008-09-24 23:26:36 +00007818 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7819 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007820 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Dan Gohman22cefb02009-01-29 01:59:02 +00007821 TargetLowering::TargetLoweringOpt TLO(DAG);
7822 TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1));
7823 DCI.CommitTargetLoweringOpt(TLO);
Evan Cheng6617eed2008-09-24 23:26:36 +00007824 return ResNode;
Scott Michel91099d62009-02-17 22:15:04 +00007825}
Evan Chenge9b9c672008-05-09 21:53:03 +00007826
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007827/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007828static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner472f1d52009-03-11 05:48:52 +00007829 const X86Subtarget *Subtarget) {
7830 DebugLoc DL = N->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00007831 SDValue Cond = N->getOperand(0);
Chris Lattner472f1d52009-03-11 05:48:52 +00007832 // Get the LHS/RHS of the select.
7833 SDValue LHS = N->getOperand(1);
7834 SDValue RHS = N->getOperand(2);
7835
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007836 // If we have SSE[12] support, try to form min/max nodes.
7837 if (Subtarget->hasSSE2() &&
Chris Lattner472f1d52009-03-11 05:48:52 +00007838 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
7839 Cond.getOpcode() == ISD::SETCC) {
7840 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007841
Chris Lattner472f1d52009-03-11 05:48:52 +00007842 unsigned Opcode = 0;
7843 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7844 switch (CC) {
7845 default: break;
7846 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7847 case ISD::SETULE:
7848 case ISD::SETLE:
7849 if (!UnsafeFPMath) break;
7850 // FALL THROUGH.
7851 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7852 case ISD::SETLT:
7853 Opcode = X86ISD::FMIN;
7854 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007855
Chris Lattner472f1d52009-03-11 05:48:52 +00007856 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7857 case ISD::SETUGT:
7858 case ISD::SETGT:
7859 if (!UnsafeFPMath) break;
7860 // FALL THROUGH.
7861 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7862 case ISD::SETGE:
7863 Opcode = X86ISD::FMAX;
7864 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007865 }
Chris Lattner472f1d52009-03-11 05:48:52 +00007866 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7867 switch (CC) {
7868 default: break;
7869 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7870 case ISD::SETUGT:
7871 case ISD::SETGT:
7872 if (!UnsafeFPMath) break;
7873 // FALL THROUGH.
7874 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7875 case ISD::SETGE:
7876 Opcode = X86ISD::FMIN;
7877 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007878
Chris Lattner472f1d52009-03-11 05:48:52 +00007879 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7880 case ISD::SETULE:
7881 case ISD::SETLE:
7882 if (!UnsafeFPMath) break;
7883 // FALL THROUGH.
7884 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7885 case ISD::SETLT:
7886 Opcode = X86ISD::FMAX;
7887 break;
7888 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007889 }
7890
Chris Lattner472f1d52009-03-11 05:48:52 +00007891 if (Opcode)
7892 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007893 }
Chris Lattner472f1d52009-03-11 05:48:52 +00007894
Chris Lattnere4577dc2009-03-12 06:52:53 +00007895 // If this is a select between two integer constants, try to do some
7896 // optimizations.
Chris Lattnera054e842009-03-13 05:53:31 +00007897 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
7898 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnere4577dc2009-03-12 06:52:53 +00007899 // Don't do this for crazy integer types.
7900 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
7901 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnera054e842009-03-13 05:53:31 +00007902 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnere4577dc2009-03-12 06:52:53 +00007903 bool NeedsCondInvert = false;
7904
Chris Lattnera054e842009-03-13 05:53:31 +00007905 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnere4577dc2009-03-12 06:52:53 +00007906 // Efficiently invertible.
7907 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
7908 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
7909 isa<ConstantSDNode>(Cond.getOperand(1))))) {
7910 NeedsCondInvert = true;
Chris Lattnera054e842009-03-13 05:53:31 +00007911 std::swap(TrueC, FalseC);
Chris Lattnere4577dc2009-03-12 06:52:53 +00007912 }
7913
7914 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnera054e842009-03-13 05:53:31 +00007915 if (FalseC->getAPIntValue() == 0 &&
7916 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnere4577dc2009-03-12 06:52:53 +00007917 if (NeedsCondInvert) // Invert the condition if needed.
7918 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7919 DAG.getConstant(1, Cond.getValueType()));
7920
7921 // Zero extend the condition if needed.
7922 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
7923
Chris Lattnera054e842009-03-13 05:53:31 +00007924 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnere4577dc2009-03-12 06:52:53 +00007925 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
7926 DAG.getConstant(ShAmt, MVT::i8));
7927 }
Chris Lattner938d6652009-03-13 05:22:11 +00007928
7929 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnera054e842009-03-13 05:53:31 +00007930 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner938d6652009-03-13 05:22:11 +00007931 if (NeedsCondInvert) // Invert the condition if needed.
7932 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7933 DAG.getConstant(1, Cond.getValueType()));
7934
7935 // Zero extend the condition if needed.
Chris Lattnera054e842009-03-13 05:53:31 +00007936 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
7937 FalseC->getValueType(0), Cond);
Chris Lattner938d6652009-03-13 05:22:11 +00007938 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnera054e842009-03-13 05:53:31 +00007939 SDValue(FalseC, 0));
Chris Lattner938d6652009-03-13 05:22:11 +00007940 }
Chris Lattnera054e842009-03-13 05:53:31 +00007941
7942 // Optimize cases that will turn into an LEA instruction. This requires
7943 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
7944 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
7945 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
7946 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
7947
7948 bool isFastMultiplier = false;
7949 if (Diff < 10) {
7950 switch ((unsigned char)Diff) {
7951 default: break;
7952 case 1: // result = add base, cond
7953 case 2: // result = lea base( , cond*2)
7954 case 3: // result = lea base(cond, cond*2)
7955 case 4: // result = lea base( , cond*4)
7956 case 5: // result = lea base(cond, cond*4)
7957 case 8: // result = lea base( , cond*8)
7958 case 9: // result = lea base(cond, cond*8)
7959 isFastMultiplier = true;
7960 break;
7961 }
7962 }
7963
7964 if (isFastMultiplier) {
7965 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
7966 if (NeedsCondInvert) // Invert the condition if needed.
7967 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7968 DAG.getConstant(1, Cond.getValueType()));
7969
7970 // Zero extend the condition if needed.
7971 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
7972 Cond);
7973 // Scale the condition by the difference.
7974 if (Diff != 1)
7975 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
7976 DAG.getConstant(Diff, Cond.getValueType()));
7977
7978 // Add the base if non-zero.
7979 if (FalseC->getAPIntValue() != 0)
7980 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
7981 SDValue(FalseC, 0));
7982 return Cond;
7983 }
7984 }
Chris Lattnere4577dc2009-03-12 06:52:53 +00007985 }
7986 }
7987
Dan Gohman8181bd12008-07-27 21:46:04 +00007988 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007989}
7990
Chris Lattnere4577dc2009-03-12 06:52:53 +00007991/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
7992static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
7993 TargetLowering::DAGCombinerInfo &DCI) {
7994 DebugLoc DL = N->getDebugLoc();
7995
7996 // If the flag operand isn't dead, don't touch this CMOV.
7997 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
7998 return SDValue();
7999
8000 // If this is a select between two integer constants, try to do some
8001 // optimizations. Note that the operands are ordered the opposite of SELECT
8002 // operands.
8003 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8004 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8005 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8006 // larger than FalseC (the false value).
8007 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
8008
8009 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8010 CC = X86::GetOppositeBranchCondition(CC);
8011 std::swap(TrueC, FalseC);
8012 }
8013
8014 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnera054e842009-03-13 05:53:31 +00008015 // This is efficient for any integer data type (including i8/i16) and
8016 // shift amount.
Chris Lattnere4577dc2009-03-12 06:52:53 +00008017 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8018 SDValue Cond = N->getOperand(3);
8019 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8020 DAG.getConstant(CC, MVT::i8), Cond);
8021
8022 // Zero extend the condition if needed.
8023 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
8024
8025 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8026 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
8027 DAG.getConstant(ShAmt, MVT::i8));
8028 if (N->getNumValues() == 2) // Dead flag value?
8029 return DCI.CombineTo(N, Cond, SDValue());
8030 return Cond;
8031 }
Chris Lattnera054e842009-03-13 05:53:31 +00008032
8033 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8034 // for any integer data type, including i8/i16.
Chris Lattner938d6652009-03-13 05:22:11 +00008035 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8036 SDValue Cond = N->getOperand(3);
8037 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8038 DAG.getConstant(CC, MVT::i8), Cond);
8039
8040 // Zero extend the condition if needed.
Chris Lattnera054e842009-03-13 05:53:31 +00008041 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8042 FalseC->getValueType(0), Cond);
Chris Lattner938d6652009-03-13 05:22:11 +00008043 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8044 SDValue(FalseC, 0));
Chris Lattnera054e842009-03-13 05:53:31 +00008045
Chris Lattner938d6652009-03-13 05:22:11 +00008046 if (N->getNumValues() == 2) // Dead flag value?
8047 return DCI.CombineTo(N, Cond, SDValue());
8048 return Cond;
8049 }
Chris Lattnera054e842009-03-13 05:53:31 +00008050
8051 // Optimize cases that will turn into an LEA instruction. This requires
8052 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
8053 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
8054 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
8055 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
8056
8057 bool isFastMultiplier = false;
8058 if (Diff < 10) {
8059 switch ((unsigned char)Diff) {
8060 default: break;
8061 case 1: // result = add base, cond
8062 case 2: // result = lea base( , cond*2)
8063 case 3: // result = lea base(cond, cond*2)
8064 case 4: // result = lea base( , cond*4)
8065 case 5: // result = lea base(cond, cond*4)
8066 case 8: // result = lea base( , cond*8)
8067 case 9: // result = lea base(cond, cond*8)
8068 isFastMultiplier = true;
8069 break;
8070 }
8071 }
8072
8073 if (isFastMultiplier) {
8074 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8075 SDValue Cond = N->getOperand(3);
8076 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8077 DAG.getConstant(CC, MVT::i8), Cond);
8078 // Zero extend the condition if needed.
8079 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8080 Cond);
8081 // Scale the condition by the difference.
8082 if (Diff != 1)
8083 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8084 DAG.getConstant(Diff, Cond.getValueType()));
8085
8086 // Add the base if non-zero.
8087 if (FalseC->getAPIntValue() != 0)
8088 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8089 SDValue(FalseC, 0));
8090 if (N->getNumValues() == 2) // Dead flag value?
8091 return DCI.CombineTo(N, Cond, SDValue());
8092 return Cond;
8093 }
8094 }
Chris Lattnere4577dc2009-03-12 06:52:53 +00008095 }
8096 }
8097 return SDValue();
8098}
8099
8100
Evan Cheng04ecee12009-03-28 05:57:29 +00008101/// PerformMulCombine - Optimize a single multiply with constant into two
8102/// in order to implement it with two cheaper instructions, e.g.
8103/// LEA + SHL, LEA + LEA.
8104static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8105 TargetLowering::DAGCombinerInfo &DCI) {
8106 if (DAG.getMachineFunction().
8107 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
8108 return SDValue();
8109
8110 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8111 return SDValue();
8112
8113 MVT VT = N->getValueType(0);
8114 if (VT != MVT::i64)
8115 return SDValue();
8116
8117 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8118 if (!C)
8119 return SDValue();
8120 uint64_t MulAmt = C->getZExtValue();
8121 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
8122 return SDValue();
8123
8124 uint64_t MulAmt1 = 0;
8125 uint64_t MulAmt2 = 0;
8126 if ((MulAmt % 9) == 0) {
8127 MulAmt1 = 9;
8128 MulAmt2 = MulAmt / 9;
8129 } else if ((MulAmt % 5) == 0) {
8130 MulAmt1 = 5;
8131 MulAmt2 = MulAmt / 5;
8132 } else if ((MulAmt % 3) == 0) {
8133 MulAmt1 = 3;
8134 MulAmt2 = MulAmt / 3;
8135 }
8136 if (MulAmt2 &&
8137 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
8138 DebugLoc DL = N->getDebugLoc();
8139
8140 if (isPowerOf2_64(MulAmt2) &&
8141 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
8142 // If second multiplifer is pow2, issue it first. We want the multiply by
8143 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
8144 // is an add.
8145 std::swap(MulAmt1, MulAmt2);
8146
8147 SDValue NewMul;
8148 if (isPowerOf2_64(MulAmt1))
8149 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
8150 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
8151 else
Evan Chengc3495762009-03-30 21:36:47 +00008152 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng04ecee12009-03-28 05:57:29 +00008153 DAG.getConstant(MulAmt1, VT));
8154
8155 if (isPowerOf2_64(MulAmt2))
8156 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
8157 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
8158 else
Evan Chengc3495762009-03-30 21:36:47 +00008159 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng04ecee12009-03-28 05:57:29 +00008160 DAG.getConstant(MulAmt2, VT));
8161
8162 // Do not add new nodes to DAG combiner worklist.
8163 DCI.CombineTo(N, NewMul, false);
8164 }
8165 return SDValue();
8166}
8167
8168
sampo025b75c2009-01-26 00:52:55 +00008169/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
8170/// when possible.
8171static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
8172 const X86Subtarget *Subtarget) {
8173 // On X86 with SSE2 support, we can transform this to a vector shift if
8174 // all elements are shifted by the same amount. We can't do this in legalize
8175 // because the a constant vector is typically transformed to a constant pool
8176 // so we have no knowledge of the shift amount.
sampo087d53c2009-01-26 03:15:31 +00008177 if (!Subtarget->hasSSE2())
8178 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00008179
sampo025b75c2009-01-26 00:52:55 +00008180 MVT VT = N->getValueType(0);
sampo087d53c2009-01-26 03:15:31 +00008181 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
8182 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00008183
Mon P Wanga91e9642009-01-28 08:12:05 +00008184 SDValue ShAmtOp = N->getOperand(1);
8185 MVT EltVT = VT.getVectorElementType();
Chris Lattner472f1d52009-03-11 05:48:52 +00008186 DebugLoc DL = N->getDebugLoc();
Mon P Wanga91e9642009-01-28 08:12:05 +00008187 SDValue BaseShAmt;
8188 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
8189 unsigned NumElts = VT.getVectorNumElements();
8190 unsigned i = 0;
8191 for (; i != NumElts; ++i) {
8192 SDValue Arg = ShAmtOp.getOperand(i);
8193 if (Arg.getOpcode() == ISD::UNDEF) continue;
8194 BaseShAmt = Arg;
8195 break;
8196 }
8197 for (; i != NumElts; ++i) {
8198 SDValue Arg = ShAmtOp.getOperand(i);
8199 if (Arg.getOpcode() == ISD::UNDEF) continue;
8200 if (Arg != BaseShAmt) {
8201 return SDValue();
8202 }
8203 }
8204 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman543d2142009-04-27 18:41:29 +00008205 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
8206 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
8207 DAG.getIntPtrConstant(0));
Mon P Wanga91e9642009-01-28 08:12:05 +00008208 } else
sampo087d53c2009-01-26 03:15:31 +00008209 return SDValue();
sampo025b75c2009-01-26 00:52:55 +00008210
sampo087d53c2009-01-26 03:15:31 +00008211 if (EltVT.bitsGT(MVT::i32))
Chris Lattner472f1d52009-03-11 05:48:52 +00008212 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00008213 else if (EltVT.bitsLT(MVT::i32))
Chris Lattner472f1d52009-03-11 05:48:52 +00008214 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, BaseShAmt);
sampo025b75c2009-01-26 00:52:55 +00008215
sampo087d53c2009-01-26 03:15:31 +00008216 // The shift amount is identical so we can do a vector shift.
8217 SDValue ValOp = N->getOperand(0);
8218 switch (N->getOpcode()) {
8219 default:
8220 assert(0 && "Unknown shift opcode!");
8221 break;
8222 case ISD::SHL:
8223 if (VT == MVT::v2i64)
Chris Lattner472f1d52009-03-11 05:48:52 +00008224 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
sampo025b75c2009-01-26 00:52:55 +00008225 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8226 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00008227 if (VT == MVT::v4i32)
Chris Lattner472f1d52009-03-11 05:48:52 +00008228 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
sampo025b75c2009-01-26 00:52:55 +00008229 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8230 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00008231 if (VT == MVT::v8i16)
Chris Lattner472f1d52009-03-11 05:48:52 +00008232 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
sampo025b75c2009-01-26 00:52:55 +00008233 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8234 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00008235 break;
8236 case ISD::SRA:
8237 if (VT == MVT::v4i32)
Chris Lattner472f1d52009-03-11 05:48:52 +00008238 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
sampo025b75c2009-01-26 00:52:55 +00008239 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8240 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00008241 if (VT == MVT::v8i16)
Chris Lattner472f1d52009-03-11 05:48:52 +00008242 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
sampo025b75c2009-01-26 00:52:55 +00008243 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8244 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00008245 break;
8246 case ISD::SRL:
8247 if (VT == MVT::v2i64)
Chris Lattner472f1d52009-03-11 05:48:52 +00008248 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
sampo025b75c2009-01-26 00:52:55 +00008249 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8250 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00008251 if (VT == MVT::v4i32)
Chris Lattner472f1d52009-03-11 05:48:52 +00008252 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
sampo025b75c2009-01-26 00:52:55 +00008253 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8254 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00008255 if (VT == MVT::v8i16)
Chris Lattner472f1d52009-03-11 05:48:52 +00008256 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
sampo025b75c2009-01-26 00:52:55 +00008257 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8258 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00008259 break;
sampo025b75c2009-01-26 00:52:55 +00008260 }
8261 return SDValue();
8262}
8263
Chris Lattnerce84ae42008-02-22 02:09:43 +00008264/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00008265static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Chengc944c5d2009-03-12 05:59:15 +00008266 const X86Subtarget *Subtarget) {
Chris Lattnerce84ae42008-02-22 02:09:43 +00008267 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
8268 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00008269 // A preferable solution to the general problem is to figure out the right
8270 // places to insert EMMS. This qualifies as a quick hack.
Evan Chengc944c5d2009-03-12 05:59:15 +00008271
8272 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng40ee6e52008-05-08 00:57:18 +00008273 StoreSDNode *St = cast<StoreSDNode>(N);
Evan Chengc944c5d2009-03-12 05:59:15 +00008274 MVT VT = St->getValue().getValueType();
8275 if (VT.getSizeInBits() != 64)
8276 return SDValue();
8277
8278 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloat && Subtarget->hasSSE2();
8279 if ((VT.isVector() ||
8280 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesend112b802008-02-25 19:20:14 +00008281 isa<LoadSDNode>(St->getValue()) &&
8282 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
8283 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00008284 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00008285 LoadSDNode *Ld = 0;
8286 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00008287 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00008288 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00008289 // Must be a store of a load. We currently handle two cases: the load
8290 // is a direct child, and it's under an intervening TokenFactor. It is
8291 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00008292 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00008293 Ld = cast<LoadSDNode>(St->getChain());
8294 else if (St->getValue().hasOneUse() &&
8295 ChainVal->getOpcode() == ISD::TokenFactor) {
8296 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00008297 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00008298 TokenFactorIndex = i;
8299 Ld = cast<LoadSDNode>(St->getValue());
8300 } else
8301 Ops.push_back(ChainVal->getOperand(i));
8302 }
8303 }
Dale Johannesend112b802008-02-25 19:20:14 +00008304
Evan Chengc944c5d2009-03-12 05:59:15 +00008305 if (!Ld || !ISD::isNormalLoad(Ld))
8306 return SDValue();
Dale Johannesend112b802008-02-25 19:20:14 +00008307
Evan Chengc944c5d2009-03-12 05:59:15 +00008308 // If this is not the MMX case, i.e. we are just turning i64 load/store
8309 // into f64 load/store, avoid the transformation if there are multiple
8310 // uses of the loaded value.
8311 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
8312 return SDValue();
Dale Johannesend112b802008-02-25 19:20:14 +00008313
Evan Chengc944c5d2009-03-12 05:59:15 +00008314 DebugLoc LdDL = Ld->getDebugLoc();
8315 DebugLoc StDL = N->getDebugLoc();
8316 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
8317 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
8318 // pair instead.
8319 if (Subtarget->is64Bit() || F64IsLegal) {
8320 MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
8321 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
8322 Ld->getBasePtr(), Ld->getSrcValue(),
8323 Ld->getSrcValueOffset(), Ld->isVolatile(),
8324 Ld->getAlignment());
8325 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00008326 if (TokenFactorIndex != -1) {
Evan Chengc944c5d2009-03-12 05:59:15 +00008327 Ops.push_back(NewChain);
8328 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesend112b802008-02-25 19:20:14 +00008329 Ops.size());
8330 }
Evan Chengc944c5d2009-03-12 05:59:15 +00008331 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattnerce84ae42008-02-22 02:09:43 +00008332 St->getSrcValue(), St->getSrcValueOffset(),
8333 St->isVolatile(), St->getAlignment());
8334 }
Evan Chengc944c5d2009-03-12 05:59:15 +00008335
8336 // Otherwise, lower to two pairs of 32-bit loads / stores.
8337 SDValue LoAddr = Ld->getBasePtr();
8338 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
8339 DAG.getConstant(4, MVT::i32));
8340
8341 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
8342 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8343 Ld->isVolatile(), Ld->getAlignment());
8344 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
8345 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8346 Ld->isVolatile(),
8347 MinAlign(Ld->getAlignment(), 4));
8348
8349 SDValue NewChain = LoLd.getValue(1);
8350 if (TokenFactorIndex != -1) {
8351 Ops.push_back(LoLd);
8352 Ops.push_back(HiLd);
8353 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
8354 Ops.size());
8355 }
8356
8357 LoAddr = St->getBasePtr();
8358 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
8359 DAG.getConstant(4, MVT::i32));
8360
8361 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
8362 St->getSrcValue(), St->getSrcValueOffset(),
8363 St->isVolatile(), St->getAlignment());
8364 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
8365 St->getSrcValue(),
8366 St->getSrcValueOffset() + 4,
8367 St->isVolatile(),
8368 MinAlign(St->getAlignment(), 4));
8369 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00008370 }
Dan Gohman8181bd12008-07-27 21:46:04 +00008371 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00008372}
8373
Chris Lattner470d5dc2008-01-25 06:14:17 +00008374/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8375/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00008376static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00008377 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8378 // F[X]OR(0.0, x) -> x
8379 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00008380 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8381 if (C->getValueAPF().isPosZero())
8382 return N->getOperand(1);
8383 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8384 if (C->getValueAPF().isPosZero())
8385 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00008386 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00008387}
8388
8389/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00008390static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00008391 // FAND(0.0, x) -> 0.0
8392 // FAND(x, 0.0) -> 0.0
8393 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8394 if (C->getValueAPF().isPosZero())
8395 return N->getOperand(0);
8396 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8397 if (C->getValueAPF().isPosZero())
8398 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00008399 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00008400}
8401
Dan Gohman22cefb02009-01-29 01:59:02 +00008402static SDValue PerformBTCombine(SDNode *N,
8403 SelectionDAG &DAG,
8404 TargetLowering::DAGCombinerInfo &DCI) {
8405 // BT ignores high bits in the bit index operand.
8406 SDValue Op1 = N->getOperand(1);
8407 if (Op1.hasOneUse()) {
8408 unsigned BitWidth = Op1.getValueSizeInBits();
8409 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8410 APInt KnownZero, KnownOne;
8411 TargetLowering::TargetLoweringOpt TLO(DAG);
8412 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8413 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8414 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8415 DCI.CommitTargetLoweringOpt(TLO);
8416 }
8417 return SDValue();
8418}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008419
Dan Gohman8181bd12008-07-27 21:46:04 +00008420SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00008421 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008422 SelectionDAG &DAG = DCI.DAG;
8423 switch (N->getOpcode()) {
8424 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00008425 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
8426 case ISD::BUILD_VECTOR:
Dan Gohman22cefb02009-01-29 01:59:02 +00008427 return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00008428 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnere4577dc2009-03-12 06:52:53 +00008429 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng04ecee12009-03-28 05:57:29 +00008430 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
sampo025b75c2009-01-26 00:52:55 +00008431 case ISD::SHL:
8432 case ISD::SRA:
8433 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00008434 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00008435 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00008436 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8437 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohman22cefb02009-01-29 01:59:02 +00008438 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008439 }
8440
Dan Gohman8181bd12008-07-27 21:46:04 +00008441 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008442}
8443
8444//===----------------------------------------------------------------------===//
8445// X86 Inline Assembly Support
8446//===----------------------------------------------------------------------===//
8447
8448/// getConstraintType - Given a constraint letter, return the type of
8449/// constraint it is for this target.
8450X86TargetLowering::ConstraintType
8451X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8452 if (Constraint.size() == 1) {
8453 switch (Constraint[0]) {
8454 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +00008455 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +00008456 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008457 case 'r':
8458 case 'R':
8459 case 'l':
8460 case 'q':
8461 case 'Q':
8462 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00008463 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008464 case 'Y':
8465 return C_RegisterClass;
Dale Johannesenf190a032009-02-12 20:58:09 +00008466 case 'e':
8467 case 'Z':
8468 return C_Other;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008469 default:
8470 break;
8471 }
8472 }
8473 return TargetLowering::getConstraintType(Constraint);
8474}
8475
Dale Johannesene99fc902008-01-29 02:21:21 +00008476/// LowerXConstraint - try to replace an X constraint, which matches anything,
8477/// with another that has more specific requirements based on the type of the
8478/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00008479const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00008480LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00008481 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8482 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00008483 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00008484 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00008485 return "Y";
8486 if (Subtarget->hasSSE1())
8487 return "x";
8488 }
Scott Michel91099d62009-02-17 22:15:04 +00008489
Chris Lattnereca405c2008-04-26 23:02:14 +00008490 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00008491}
8492
Chris Lattnera531abc2007-08-25 00:47:38 +00008493/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8494/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00008495void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00008496 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00008497 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00008498 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00008499 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00008500 SDValue Result(0, 0);
Scott Michel91099d62009-02-17 22:15:04 +00008501
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008502 switch (Constraint) {
8503 default: break;
8504 case 'I':
8505 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008506 if (C->getZExtValue() <= 31) {
8507 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008508 break;
8509 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008510 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008511 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00008512 case 'J':
8513 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8514 if (C->getZExtValue() <= 63) {
8515 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8516 break;
8517 }
8518 }
8519 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008520 case 'N':
8521 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008522 if (C->getZExtValue() <= 255) {
8523 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008524 break;
8525 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008526 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008527 return;
Dale Johannesenf190a032009-02-12 20:58:09 +00008528 case 'e': {
8529 // 32-bit signed value
8530 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8531 const ConstantInt *CI = C->getConstantIntValue();
8532 if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) {
8533 // Widen to 64 bits here to get it sign extended.
8534 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
8535 break;
8536 }
8537 // FIXME gcc accepts some relocatable values here too, but only in certain
8538 // memory models; it's complicated.
8539 }
8540 return;
8541 }
8542 case 'Z': {
8543 // 32-bit unsigned value
8544 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8545 const ConstantInt *CI = C->getConstantIntValue();
8546 if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) {
8547 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8548 break;
8549 }
8550 }
8551 // FIXME gcc accepts some relocatable values here too, but only in certain
8552 // memory models; it's complicated.
8553 return;
8554 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008555 case 'i': {
8556 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00008557 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesenf190a032009-02-12 20:58:09 +00008558 // Widen to 64 bits here to get it sign extended.
8559 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattnera531abc2007-08-25 00:47:38 +00008560 break;
8561 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008562
8563 // If we are in non-pic codegen mode, we allow the address of a global (with
8564 // an optional displacement) to be used with 'i'.
8565 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
8566 int64_t Offset = 0;
Scott Michel91099d62009-02-17 22:15:04 +00008567
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008568 // Match either (GA) or (GA+C)
8569 if (GA) {
8570 Offset = GA->getOffset();
8571 } else if (Op.getOpcode() == ISD::ADD) {
8572 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8573 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8574 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008575 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008576 } else {
8577 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8578 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8579 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008580 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008581 else
8582 C = 0, GA = 0;
8583 }
8584 }
Scott Michel91099d62009-02-17 22:15:04 +00008585
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008586 if (GA) {
Scott Michel91099d62009-02-17 22:15:04 +00008587 if (hasMemory)
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00008588 Op = LowerGlobalAddress(GA->getGlobal(), Op.getDebugLoc(),
Dale Johannesenea996922009-02-04 20:06:27 +00008589 Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00008590 else
8591 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8592 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00008593 Result = Op;
8594 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008595 }
8596
8597 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00008598 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008599 }
8600 }
Scott Michel91099d62009-02-17 22:15:04 +00008601
Gabor Greif1c80d112008-08-28 21:40:38 +00008602 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00008603 Ops.push_back(Result);
8604 return;
8605 }
Evan Cheng7f250d62008-09-24 00:05:32 +00008606 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8607 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008608}
8609
8610std::vector<unsigned> X86TargetLowering::
8611getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00008612 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008613 if (Constraint.size() == 1) {
8614 // FIXME: not handling fp-stack yet!
8615 switch (Constraint[0]) { // GCC X86 Constraint Letters
8616 default: break; // Unknown constraint letter
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008617 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8618 case 'Q': // Q_REGS
8619 if (VT == MVT::i32)
8620 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8621 else if (VT == MVT::i16)
8622 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8623 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00008624 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00008625 else if (VT == MVT::i64)
8626 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8627 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008628 }
8629 }
8630
8631 return std::vector<unsigned>();
8632}
8633
8634std::pair<unsigned, const TargetRegisterClass*>
8635X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00008636 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008637 // First, see if this is a constraint that directly corresponds to an LLVM
8638 // register class.
8639 if (Constraint.size() == 1) {
8640 // GCC Constraint Letters
8641 switch (Constraint[0]) {
8642 default: break;
8643 case 'r': // GENERAL_REGS
8644 case 'R': // LEGACY_REGS
8645 case 'l': // INDEX_REGS
Chris Lattnerbbfea052008-10-17 18:15:05 +00008646 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008647 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00008648 if (VT == MVT::i16)
8649 return std::make_pair(0U, X86::GR16RegisterClass);
8650 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michel91099d62009-02-17 22:15:04 +00008651 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00008652 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00008653 case 'f': // FP Stack registers.
8654 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8655 // value to the correct fpstack register class.
8656 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8657 return std::make_pair(0U, X86::RFP32RegisterClass);
8658 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8659 return std::make_pair(0U, X86::RFP64RegisterClass);
8660 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008661 case 'y': // MMX_REGS if MMX allowed.
8662 if (!Subtarget->hasMMX()) break;
8663 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008664 case 'Y': // SSE_REGS if SSE2 allowed
8665 if (!Subtarget->hasSSE2()) break;
8666 // FALL THROUGH.
8667 case 'x': // SSE_REGS if SSE1 allowed
8668 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00008669
8670 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008671 default: break;
8672 // Scalar SSE types.
8673 case MVT::f32:
8674 case MVT::i32:
8675 return std::make_pair(0U, X86::FR32RegisterClass);
8676 case MVT::f64:
8677 case MVT::i64:
8678 return std::make_pair(0U, X86::FR64RegisterClass);
8679 // Vector types.
8680 case MVT::v16i8:
8681 case MVT::v8i16:
8682 case MVT::v4i32:
8683 case MVT::v2i64:
8684 case MVT::v4f32:
8685 case MVT::v2f64:
8686 return std::make_pair(0U, X86::VR128RegisterClass);
8687 }
8688 break;
8689 }
8690 }
Scott Michel91099d62009-02-17 22:15:04 +00008691
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008692 // Use the default implementation in TargetLowering to convert the register
8693 // constraint into a member of a register class.
8694 std::pair<unsigned, const TargetRegisterClass*> Res;
8695 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8696
8697 // Not found as a standard register?
8698 if (Res.second == 0) {
8699 // GCC calls "st(0)" just plain "st".
8700 if (StringsEqualNoCase("{st}", Constraint)) {
8701 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00008702 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008703 }
Dale Johannesen73920c02008-11-13 21:52:36 +00008704 // 'A' means EAX + EDX.
8705 if (Constraint == "A") {
8706 Res.first = X86::EAX;
8707 Res.second = X86::GRADRegisterClass;
8708 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008709 return Res;
8710 }
8711
8712 // Otherwise, check to see if this is a register class of the wrong value
8713 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8714 // turn into {ax},{dx}.
8715 if (Res.second->hasType(VT))
8716 return Res; // Correct type already, nothing to do.
8717
8718 // All of the single-register GCC register classes map their values onto
8719 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8720 // really want an 8-bit or 32-bit register, map to the appropriate register
8721 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00008722 if (Res.second == X86::GR16RegisterClass) {
8723 if (VT == MVT::i8) {
8724 unsigned DestReg = 0;
8725 switch (Res.first) {
8726 default: break;
8727 case X86::AX: DestReg = X86::AL; break;
8728 case X86::DX: DestReg = X86::DL; break;
8729 case X86::CX: DestReg = X86::CL; break;
8730 case X86::BX: DestReg = X86::BL; break;
8731 }
8732 if (DestReg) {
8733 Res.first = DestReg;
Duncan Sands553fb412009-04-21 09:44:39 +00008734 Res.second = X86::GR8RegisterClass;
Chris Lattnere9d7f792008-08-26 06:19:02 +00008735 }
8736 } else if (VT == MVT::i32) {
8737 unsigned DestReg = 0;
8738 switch (Res.first) {
8739 default: break;
8740 case X86::AX: DestReg = X86::EAX; break;
8741 case X86::DX: DestReg = X86::EDX; break;
8742 case X86::CX: DestReg = X86::ECX; break;
8743 case X86::BX: DestReg = X86::EBX; break;
8744 case X86::SI: DestReg = X86::ESI; break;
8745 case X86::DI: DestReg = X86::EDI; break;
8746 case X86::BP: DestReg = X86::EBP; break;
8747 case X86::SP: DestReg = X86::ESP; break;
8748 }
8749 if (DestReg) {
8750 Res.first = DestReg;
Duncan Sands553fb412009-04-21 09:44:39 +00008751 Res.second = X86::GR32RegisterClass;
Chris Lattnere9d7f792008-08-26 06:19:02 +00008752 }
8753 } else if (VT == MVT::i64) {
8754 unsigned DestReg = 0;
8755 switch (Res.first) {
8756 default: break;
8757 case X86::AX: DestReg = X86::RAX; break;
8758 case X86::DX: DestReg = X86::RDX; break;
8759 case X86::CX: DestReg = X86::RCX; break;
8760 case X86::BX: DestReg = X86::RBX; break;
8761 case X86::SI: DestReg = X86::RSI; break;
8762 case X86::DI: DestReg = X86::RDI; break;
8763 case X86::BP: DestReg = X86::RBP; break;
8764 case X86::SP: DestReg = X86::RSP; break;
8765 }
8766 if (DestReg) {
8767 Res.first = DestReg;
Duncan Sands553fb412009-04-21 09:44:39 +00008768 Res.second = X86::GR64RegisterClass;
Chris Lattnere9d7f792008-08-26 06:19:02 +00008769 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008770 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00008771 } else if (Res.second == X86::FR32RegisterClass ||
8772 Res.second == X86::FR64RegisterClass ||
8773 Res.second == X86::VR128RegisterClass) {
8774 // Handle references to XMM physical registers that got mapped into the
8775 // wrong class. This can happen with constraints like {xmm0} where the
8776 // target independent register mapper will just pick the first match it can
8777 // find, ignoring the required type.
8778 if (VT == MVT::f32)
8779 Res.second = X86::FR32RegisterClass;
8780 else if (VT == MVT::f64)
8781 Res.second = X86::FR64RegisterClass;
8782 else if (X86::VR128RegisterClass->hasType(VT))
8783 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008784 }
8785
8786 return Res;
8787}
Mon P Wang1448aad2008-10-30 08:01:45 +00008788
8789//===----------------------------------------------------------------------===//
8790// X86 Widen vector type
8791//===----------------------------------------------------------------------===//
8792
8793/// getWidenVectorType: given a vector type, returns the type to widen
8794/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8795/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +00008796/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +00008797/// scalarizing vs using the wider vector type.
8798
Dan Gohman0fe66c92009-01-15 17:34:08 +00008799MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang1448aad2008-10-30 08:01:45 +00008800 assert(VT.isVector());
8801 if (isTypeLegal(VT))
8802 return VT;
Scott Michel91099d62009-02-17 22:15:04 +00008803
Mon P Wang1448aad2008-10-30 08:01:45 +00008804 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8805 // type based on element type. This would speed up our search (though
8806 // it may not be worth it since the size of the list is relatively
8807 // small).
8808 MVT EltVT = VT.getVectorElementType();
8809 unsigned NElts = VT.getVectorNumElements();
Scott Michel91099d62009-02-17 22:15:04 +00008810
Mon P Wang1448aad2008-10-30 08:01:45 +00008811 // On X86, it make sense to widen any vector wider than 1
8812 if (NElts <= 1)
8813 return MVT::Other;
Scott Michel91099d62009-02-17 22:15:04 +00008814
8815 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
Mon P Wang1448aad2008-10-30 08:01:45 +00008816 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8817 MVT SVT = (MVT::SimpleValueType)nVT;
Scott Michel91099d62009-02-17 22:15:04 +00008818
8819 if (isTypeLegal(SVT) &&
8820 SVT.getVectorElementType() == EltVT &&
Mon P Wang1448aad2008-10-30 08:01:45 +00008821 SVT.getVectorNumElements() > NElts)
8822 return SVT;
8823 }
8824 return MVT::Other;
8825}