blob: 6dae08d9f99cc82f03f9db8bfbadcbf3dbda2d85 [file] [log] [blame]
Arnold Schwaighofera70fe792007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86InstrBuilder.h"
17#include "X86ISelLowering.h"
18#include "X86MachineFunctionInfo.h"
19#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
21#include "llvm/Constants.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/Function.h"
25#include "llvm/Intrinsics.h"
Evan Cheng75184a92007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/ADT/VectorExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/CodeGen/CallingConvLower.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000034#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Support/MathExtras.h"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000037#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Target/TargetOptions.h"
Evan Cheng75184a92007-12-11 01:46:18 +000039#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include "llvm/ADT/StringExtras.h"
Mon P Wang1f292322008-11-23 04:37:22 +000041#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042using namespace llvm;
43
Mon P Wang1f292322008-11-23 04:37:22 +000044static cl::opt<bool>
Mon P Wangba7e48e2008-11-24 02:10:43 +000045DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang1f292322008-11-23 04:37:22 +000046
Evan Cheng2aea0b42008-04-25 19:11:04 +000047// Forward declarations.
Dan Gohman8181bd12008-07-27 21:46:04 +000048static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG);
Evan Cheng2aea0b42008-04-25 19:11:04 +000049
Dan Gohmanb41dfba2008-05-14 01:58:56 +000050X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 : TargetLowering(TM) {
52 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000053 X86ScalarSSEf64 = Subtarget->hasSSE2();
54 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000056
Chris Lattnerdec9cb52008-01-24 08:07:48 +000057 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
59 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000060 TD = getTargetData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061
62 // Set up the TargetLowering object.
63
64 // X86 is weird, it always uses i8 for shift amounts and setcc results.
65 setShiftAmountType(MVT::i8);
Duncan Sands8cf4a822008-11-23 15:47:28 +000066 setBooleanContents(ZeroOrOneBooleanContent);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 setSchedulingPreference(SchedulingForRegPressure);
68 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
69 setStackPointerRegisterToSaveRestore(X86StackPtr);
70
71 if (Subtarget->isTargetDarwin()) {
72 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
73 setUseUnderscoreSetJmp(false);
74 setUseUnderscoreLongJmp(false);
75 } else if (Subtarget->isTargetMingw()) {
76 // MS runtime is weird: it exports _setjmp, but longjmp!
77 setUseUnderscoreSetJmp(true);
78 setUseUnderscoreLongJmp(false);
79 } else {
80 setUseUnderscoreSetJmp(true);
81 setUseUnderscoreLongJmp(true);
82 }
83
84 // Set up the register classes.
85 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
86 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
87 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
88 if (Subtarget->is64Bit())
89 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
90
Evan Cheng08c171a2008-10-14 21:26:46 +000091 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
Chris Lattner3bc08502008-01-17 19:59:44 +000093 // We don't accept any truncstore of integer registers.
94 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
95 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
96 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
97 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
98 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
Evan Cheng71343822008-10-15 02:05:31 +000099 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
100
101 // SETOEQ and SETUNE require checking two conditions.
102 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
103 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
104 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
105 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
106 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
107 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattner3bc08502008-01-17 19:59:44 +0000108
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
110 // operation.
111 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
112 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
113 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
114
115 if (Subtarget->is64Bit()) {
116 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
117 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
118 } else {
Dale Johannesena359b8b2008-10-21 20:50:01 +0000119 if (X86ScalarSSEf64) {
120 // We have an impenetrably clever algorithm for ui64->double only.
121 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling14a30ef2009-01-17 03:56:04 +0000122
123 // We have faster algorithm for ui32->single only.
Bill Wendling71702d62009-01-17 04:23:51 +0000124#if 0
Bill Wendling14a30ef2009-01-17 03:56:04 +0000125 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Bill Wendling71702d62009-01-17 04:23:51 +0000126#else
127 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
128#endif
Dale Johannesena359b8b2008-10-21 20:50:01 +0000129 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
131 }
132
133 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
134 // this operation.
135 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
136 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
137 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000138 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000140 // f32 and f64 cases are Legal, f80 case is not
141 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
142 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
144 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
145 }
146
Dale Johannesen958b08b2007-09-19 23:55:34 +0000147 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
148 // are Legal, f80 is custom lowered.
149 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
150 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151
152 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
153 // this operation.
154 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
155 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
156
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000157 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000159 // f32 and f64 cases are Legal, f80 case is not
160 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 } else {
162 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
163 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
164 }
165
166 // Handle FP_TO_UINT by promoting the destination to a larger signed
167 // conversion.
168 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
169 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
170 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
171
172 if (Subtarget->is64Bit()) {
173 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
174 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
175 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000176 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 // Expand FP_TO_UINT into a select.
178 // FIXME: We would like to use a Custom expander here eventually to do
179 // the optimal thing for SSE vs. the default expansion in the legalizer.
180 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
181 else
182 // With SSE3 we can use fisttpll to convert to a signed i64.
183 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
184 }
185
186 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000187 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
189 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
190 }
191
Dan Gohman8450d862008-02-18 19:34:53 +0000192 // Scalar integer divide and remainder are lowered to use operations that
193 // produce two results, to match the available instructions. This exposes
194 // the two-result form to trivial CSE, which is able to combine x/y and x%y
195 // into a single instruction.
196 //
197 // Scalar integer multiply-high is also lowered to use two-result
198 // operations, to match the available instructions. However, plain multiply
199 // (low) operations are left as Legal, as there are single-result
200 // instructions for this in x86. Using the two-result multiply instructions
201 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000202 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
203 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
204 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
205 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
206 setOperationAction(ISD::SREM , MVT::i8 , Expand);
207 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000208 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
209 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
210 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
211 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
212 setOperationAction(ISD::SREM , MVT::i16 , Expand);
213 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000214 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
215 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
216 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
217 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
218 setOperationAction(ISD::SREM , MVT::i32 , Expand);
219 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000220 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
221 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
222 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
223 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
224 setOperationAction(ISD::SREM , MVT::i64 , Expand);
225 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000226
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
228 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
229 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
230 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000232 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
233 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
234 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
236 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000237 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000239 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000240 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000241
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000243 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
244 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000246 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
247 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000249 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
250 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 if (Subtarget->is64Bit()) {
252 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000253 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
254 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 }
256
257 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
258 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
259
260 // These should be promoted to a larger select which is supported.
261 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
262 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
263 // X86 wants to expand cmov itself.
264 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
265 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
266 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
267 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000268 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
270 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
271 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
272 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
273 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000274 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 if (Subtarget->is64Bit()) {
276 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
277 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
278 }
279 // X86 ret instruction may pop stack.
280 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000281 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282
283 // Darwin ABI issue.
284 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
285 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
286 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
287 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000288 if (Subtarget->is64Bit())
289 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000290 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 if (Subtarget->is64Bit()) {
292 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
293 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
294 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000295 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 }
297 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
298 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
299 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
300 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000301 if (Subtarget->is64Bit()) {
302 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
303 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
304 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
305 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306
Evan Cheng8d51ab32008-03-10 19:38:10 +0000307 if (Subtarget->hasSSE1())
308 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000309
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000310 if (!Subtarget->hasSSE2())
311 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
312
Mon P Wang078a62d2008-05-05 19:05:59 +0000313 // Expand certain atomics
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000314 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
315 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
316 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
317 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000318
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000319 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
320 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
321 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
322 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000323
Dale Johannesenf160d802008-10-02 18:53:47 +0000324 if (!Subtarget->is64Bit()) {
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000325 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
326 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
327 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
328 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
329 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
330 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
331 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesenf160d802008-10-02 18:53:47 +0000332 }
333
Dan Gohman472d12c2008-06-30 20:59:49 +0000334 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
335 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 // FIXME - use subtarget debug flags
337 if (!Subtarget->isTargetDarwin() &&
338 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000339 !Subtarget->isTargetCygMing()) {
340 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
341 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
342 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343
344 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
345 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
346 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
347 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
348 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 setExceptionPointerRegister(X86::RAX);
350 setExceptionSelectorRegister(X86::RDX);
351 } else {
352 setExceptionPointerRegister(X86::EAX);
353 setExceptionSelectorRegister(X86::EDX);
354 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000355 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000356 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
357
Duncan Sands7407a9f2007-09-11 14:10:23 +0000358 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000359
Chris Lattner56b941f2008-01-15 21:58:22 +0000360 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000361
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
363 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000365 if (Subtarget->is64Bit()) {
366 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000368 } else {
369 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000371 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372
373 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
374 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
375 if (Subtarget->is64Bit())
376 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
377 if (Subtarget->isTargetCygMing())
378 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
379 else
380 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
381
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000382 if (X86ScalarSSEf64) {
383 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384 // Set up the FP register classes.
385 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
386 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
387
388 // Use ANDPD to simulate FABS.
389 setOperationAction(ISD::FABS , MVT::f64, Custom);
390 setOperationAction(ISD::FABS , MVT::f32, Custom);
391
392 // Use XORP to simulate FNEG.
393 setOperationAction(ISD::FNEG , MVT::f64, Custom);
394 setOperationAction(ISD::FNEG , MVT::f32, Custom);
395
396 // Use ANDPD and ORPD to simulate FCOPYSIGN.
397 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
398 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
399
400 // We don't support sin/cos/fmod
401 setOperationAction(ISD::FSIN , MVT::f64, Expand);
402 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 setOperationAction(ISD::FSIN , MVT::f32, Expand);
404 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405
406 // Expand FP immediates into loads from the stack, except for the special
407 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000408 addLegalFPImmediate(APFloat(+0.0)); // xorpd
409 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000410
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000411 // Floating truncations from f80 and extensions to f80 go through memory.
412 // If optimizing, we lie about this though and handle it in
413 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
414 if (Fast) {
415 setConvertAction(MVT::f32, MVT::f80, Expand);
416 setConvertAction(MVT::f64, MVT::f80, Expand);
417 setConvertAction(MVT::f80, MVT::f32, Expand);
418 setConvertAction(MVT::f80, MVT::f64, Expand);
419 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000420 } else if (X86ScalarSSEf32) {
421 // Use SSE for f32, x87 for f64.
422 // Set up the FP register classes.
423 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
424 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
425
426 // Use ANDPS to simulate FABS.
427 setOperationAction(ISD::FABS , MVT::f32, Custom);
428
429 // Use XORP to simulate FNEG.
430 setOperationAction(ISD::FNEG , MVT::f32, Custom);
431
432 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
433
434 // Use ANDPS and ORPS to simulate FCOPYSIGN.
435 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
436 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
437
438 // We don't support sin/cos/fmod
439 setOperationAction(ISD::FSIN , MVT::f32, Expand);
440 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000441
Nate Begemane2ba64f2008-02-14 08:57:00 +0000442 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000443 addLegalFPImmediate(APFloat(+0.0f)); // xorps
444 addLegalFPImmediate(APFloat(+0.0)); // FLD0
445 addLegalFPImmediate(APFloat(+1.0)); // FLD1
446 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
447 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
448
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000449 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
450 // this though and handle it in InstructionSelectPreprocess so that
451 // dagcombine2 can hack on these.
452 if (Fast) {
453 setConvertAction(MVT::f32, MVT::f64, Expand);
454 setConvertAction(MVT::f32, MVT::f80, Expand);
455 setConvertAction(MVT::f80, MVT::f32, Expand);
456 setConvertAction(MVT::f64, MVT::f32, Expand);
457 // And x87->x87 truncations also.
458 setConvertAction(MVT::f80, MVT::f64, Expand);
459 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000460
461 if (!UnsafeFPMath) {
462 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
463 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
464 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000466 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 // Set up the FP register classes.
468 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
469 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
470
471 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
472 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
473 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
474 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000475
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000476 // Floating truncations go through memory. If optimizing, we lie about
477 // this though and handle it in InstructionSelectPreprocess so that
478 // dagcombine2 can hack on these.
479 if (Fast) {
480 setConvertAction(MVT::f80, MVT::f32, Expand);
481 setConvertAction(MVT::f64, MVT::f32, Expand);
482 setConvertAction(MVT::f80, MVT::f64, Expand);
483 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484
485 if (!UnsafeFPMath) {
486 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
487 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
488 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000489 addLegalFPImmediate(APFloat(+0.0)); // FLD0
490 addLegalFPImmediate(APFloat(+1.0)); // FLD1
491 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
492 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000493 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
494 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
495 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
496 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 }
498
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000499 // Long double always uses X87.
500 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000501 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
502 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000503 {
Dale Johannesen6e547b42008-10-09 23:00:39 +0000504 bool ignored;
Chris Lattnerdd867392008-01-27 06:19:31 +0000505 APFloat TmpFlt(+0.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000506 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
507 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000508 addLegalFPImmediate(TmpFlt); // FLD0
509 TmpFlt.changeSign();
510 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
511 APFloat TmpFlt2(+1.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000512 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
513 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000514 addLegalFPImmediate(TmpFlt2); // FLD1
515 TmpFlt2.changeSign();
516 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
517 }
518
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000519 if (!UnsafeFPMath) {
520 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
521 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
522 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000523
Dan Gohman2f7b1982007-10-11 23:21:31 +0000524 // Always use a library call for pow.
525 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
526 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
527 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
528
Dale Johannesen92b33082008-09-04 00:47:13 +0000529 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000530 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000531 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000532 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000533 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
534
Mon P Wanga5a239f2008-11-06 05:31:54 +0000535 // First set operation action for all vector types to either promote
Mon P Wang1448aad2008-10-30 08:01:45 +0000536 // (for widening) or expand (for scalarization). Then we will selectively
537 // turn on ones that can be effectively codegen'd.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
539 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000540 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif825aa892008-08-28 23:19:51 +0000553 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
555 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands92c43912008-06-06 12:08:01 +0000556 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesen177edff2008-09-10 17:31:40 +0000578 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 }
584
Mon P Wang1f292322008-11-23 04:37:22 +0000585 if (!DisableMMX && Subtarget->hasMMX()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
587 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
588 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000589 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
591
592 // FIXME: add MMX packed arithmetics
593
594 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
595 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
596 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
597 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
598
599 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
600 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
601 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000602 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603
604 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
605 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
606
607 setOperationAction(ISD::AND, MVT::v8i8, Promote);
608 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
609 setOperationAction(ISD::AND, MVT::v4i16, Promote);
610 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
611 setOperationAction(ISD::AND, MVT::v2i32, Promote);
612 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
613 setOperationAction(ISD::AND, MVT::v1i64, Legal);
614
615 setOperationAction(ISD::OR, MVT::v8i8, Promote);
616 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
617 setOperationAction(ISD::OR, MVT::v4i16, Promote);
618 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
619 setOperationAction(ISD::OR, MVT::v2i32, Promote);
620 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
621 setOperationAction(ISD::OR, MVT::v1i64, Legal);
622
623 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
624 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
625 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
626 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
627 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
628 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
629 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
630
631 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
632 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
633 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
634 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
635 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
636 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000637 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
638 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
640
641 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
642 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
643 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000644 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
646
647 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
648 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
649 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
650 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
651
Evan Cheng759fe022008-07-22 18:39:19 +0000652 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
654 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000656
657 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang83edba52008-12-12 01:25:51 +0000658
659 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
660 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
661 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
662 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
663 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
664 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 }
666
667 if (Subtarget->hasSSE1()) {
668 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
669
670 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
671 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
672 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
673 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
674 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
675 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
677 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
678 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
679 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
680 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000681 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 }
683
684 if (Subtarget->hasSSE2()) {
685 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
686 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
687 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
688 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
689 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
690
691 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
692 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
693 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
694 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wang14edb092008-12-18 21:42:19 +0000695 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
697 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
698 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
699 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
700 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
701 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
702 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
703 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
704 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
705 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
706 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707
Nate Begeman03605a02008-07-17 16:51:19 +0000708 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
709 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
710 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
711 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000712
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
714 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
715 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
716 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
718
719 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000720 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
721 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000722 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000723 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000724 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000725 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
726 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
727 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000728 }
729 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
730 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
731 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
732 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000733 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000735 if (Subtarget->is64Bit()) {
736 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000737 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000738 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739
740 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
741 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000742 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
743 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
744 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
745 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
746 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
747 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
748 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
749 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
750 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
751 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 }
753
Chris Lattner3bc08502008-01-17 19:59:44 +0000754 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000755
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756 // Custom lower v2i64 and v2f64 selects.
757 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
758 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
759 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
760 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000761
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000762 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000763
764 if (Subtarget->hasSSE41()) {
765 // FIXME: Do we need to handle scalar-to-vector here?
766 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
767
768 // i8 and i16 vectors are custom , because the source register and source
769 // source memory operand types are not the same width. f32 vectors are
770 // custom since the immediate controlling the insert encodes additional
771 // information.
772 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
773 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangac2a3c52009-01-15 21:10:20 +0000774 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000775 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
776
777 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
778 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangac2a3c52009-01-15 21:10:20 +0000779 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng6c249332008-03-24 21:52:23 +0000780 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000781
782 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000783 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
784 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000785 }
786 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787
Nate Begeman03605a02008-07-17 16:51:19 +0000788 if (Subtarget->hasSSE42()) {
789 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
790 }
791
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792 // We want to custom lower some of our intrinsics.
793 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
794
Bill Wendling7e04be62008-12-09 22:08:41 +0000795 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling4c134df2008-11-24 19:21:46 +0000796 setOperationAction(ISD::SADDO, MVT::i32, Custom);
797 setOperationAction(ISD::SADDO, MVT::i64, Custom);
798 setOperationAction(ISD::UADDO, MVT::i32, Custom);
799 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling7e04be62008-12-09 22:08:41 +0000800 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
801 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
802 setOperationAction(ISD::USUBO, MVT::i32, Custom);
803 setOperationAction(ISD::USUBO, MVT::i64, Custom);
804 setOperationAction(ISD::SMULO, MVT::i32, Custom);
805 setOperationAction(ISD::SMULO, MVT::i64, Custom);
806 setOperationAction(ISD::UMULO, MVT::i32, Custom);
807 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Bill Wendling4c134df2008-11-24 19:21:46 +0000808
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 // We have target-specific dag combine patterns for the following nodes:
810 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000811 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000813 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814
815 computeRegisterProperties();
816
817 // FIXME: These should be based on subtarget info. Plus, the values should
818 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000819 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
820 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
821 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000823 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824}
825
Scott Michel502151f2008-03-10 15:42:14 +0000826
Duncan Sands4a361272009-01-01 15:52:00 +0000827MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel502151f2008-03-10 15:42:14 +0000828 return MVT::i8;
829}
830
831
Evan Cheng5a67b812008-01-23 23:17:41 +0000832/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
833/// the desired ByVal argument alignment.
834static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
835 if (MaxAlign == 16)
836 return;
837 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
838 if (VTy->getBitWidth() == 128)
839 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000840 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
841 unsigned EltAlign = 0;
842 getMaxByValAlign(ATy->getElementType(), EltAlign);
843 if (EltAlign > MaxAlign)
844 MaxAlign = EltAlign;
845 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
846 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
847 unsigned EltAlign = 0;
848 getMaxByValAlign(STy->getElementType(i), EltAlign);
849 if (EltAlign > MaxAlign)
850 MaxAlign = EltAlign;
851 if (MaxAlign == 16)
852 break;
853 }
854 }
855 return;
856}
857
858/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
859/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000860/// that contain SSE vectors are placed at 16-byte boundaries while the rest
861/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000862unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000863 if (Subtarget->is64Bit()) {
864 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000865 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000866 if (TyAlign > 8)
867 return TyAlign;
868 return 8;
869 }
870
Evan Cheng5a67b812008-01-23 23:17:41 +0000871 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000872 if (Subtarget->hasSSE1())
873 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000874 return Align;
875}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876
Evan Cheng8c590372008-05-15 08:39:06 +0000877/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000878/// and store operations as a result of memset, memcpy, and memmove
879/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000880/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000881MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000882X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
883 bool isSrcConst, bool isSrcStr) const {
Chris Lattnerf0bf1062008-10-28 05:49:35 +0000884 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
885 // linux. This is because the stack realignment code can't handle certain
886 // cases like PR2962. This should be removed when PR2962 is fixed.
887 if (Subtarget->getStackAlignment() >= 16) {
888 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
889 return MVT::v4i32;
890 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
891 return MVT::v4f32;
892 }
Evan Cheng8c590372008-05-15 08:39:06 +0000893 if (Subtarget->is64Bit() && Size >= 8)
894 return MVT::i64;
895 return MVT::i32;
896}
897
898
Evan Cheng6fb06762007-11-09 01:32:10 +0000899/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
900/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000901SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000902 SelectionDAG &DAG) const {
903 if (usesGlobalOffsetTable())
904 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
905 if (!Subtarget->isPICStyleRIPRel())
906 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
907 return Table;
908}
909
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000910//===----------------------------------------------------------------------===//
911// Return Value Calling Convention Implementation
912//===----------------------------------------------------------------------===//
913
914#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000915
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000917SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
919
920 SmallVector<CCValAssign, 16> RVLocs;
921 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
922 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
923 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000924 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000925
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000926 // If this is the first return lowered for this function, add the regs to the
927 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000928 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 for (unsigned i = 0; i != RVLocs.size(); ++i)
930 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000931 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000933 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000934
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000935 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000936 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000937 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000938 SDValue TailCall = Chain;
939 SDValue TargetAddress = TailCall.getOperand(1);
940 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000941 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000942 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000943 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000944 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000945 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
946 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000947 assert(StackAdjustment.getOpcode() == ISD::Constant &&
948 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000949
Dan Gohman8181bd12008-07-27 21:46:04 +0000950 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000951 Operands.push_back(Chain.getOperand(0));
952 Operands.push_back(TargetAddress);
953 Operands.push_back(StackAdjustment);
954 // Copy registers used by the call. Last operand is a flag so it is not
955 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000956 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000957 Operands.push_back(Chain.getOperand(i));
958 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000959 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
960 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000961 }
962
963 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000964 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000965
Dan Gohman8181bd12008-07-27 21:46:04 +0000966 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000967 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
968 // Operand #1 = Bytes To Pop
969 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
970
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000972 for (unsigned i = 0; i != RVLocs.size(); ++i) {
973 CCValAssign &VA = RVLocs[i];
974 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000975 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976
Chris Lattnerb56cc342008-03-11 03:23:40 +0000977 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
978 // the RET instruction and handled by the FP Stackifier.
979 if (RVLocs[i].getLocReg() == X86::ST0 ||
980 RVLocs[i].getLocReg() == X86::ST1) {
981 // If this is a copy from an xmm register to ST(0), use an FPExtend to
982 // change the value to the FP stack register class.
983 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
984 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
985 RetOps.push_back(ValToCopy);
986 // Don't emit a copytoreg.
987 continue;
988 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000989
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000990 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 Flag = Chain.getValue(1);
992 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000993
994 // The x86-64 ABI for returning structs by value requires that we copy
995 // the sret argument into %rax for the return. We saved the argument into
996 // a virtual register in the entry block, so now we copy the value out
997 // and into %rax.
998 if (Subtarget->is64Bit() &&
999 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1000 MachineFunction &MF = DAG.getMachineFunction();
1001 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1002 unsigned Reg = FuncInfo->getSRetReturnReg();
1003 if (!Reg) {
1004 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1005 FuncInfo->setSRetReturnReg(Reg);
1006 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001007 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001008
1009 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
1010 Flag = Chain.getValue(1);
1011 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012
Chris Lattnerb56cc342008-03-11 03:23:40 +00001013 RetOps[0] = Chain; // Update chain.
1014
1015 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +00001016 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +00001017 RetOps.push_back(Flag);
1018
1019 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020}
1021
1022
1023/// LowerCallResult - Lower the result values of an ISD::CALL into the
1024/// appropriate copies out of appropriate physical registers. This assumes that
1025/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1026/// being lowered. The returns a SDNode with the same number of values as the
1027/// ISD::CALL.
1028SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +00001029LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 unsigned CallingConv, SelectionDAG &DAG) {
1031
1032 // Assign locations to each value returned by this call.
1033 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +00001034 bool isVarArg = TheCall->isVarArg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
1036 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1037
Dan Gohman8181bd12008-07-27 21:46:04 +00001038 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039
1040 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001041 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00001042 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001043
1044 // If this is a call to a function that returns an fp value on the floating
1045 // point stack, but where we prefer to use the value in xmm registers, copy
1046 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Mon P Wang73a2c152008-08-21 19:54:16 +00001047 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1048 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001049 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1050 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001051 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001052
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001053 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1054 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001055 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001056 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001057
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001058 if (CopyVT != RVLocs[i].getValVT()) {
1059 // Round the F80 the right size, which also moves to the appropriate xmm
1060 // register.
1061 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1062 // This truncation won't change the value.
1063 DAG.getIntPtrConstant(1));
1064 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001065
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001066 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001067 }
Duncan Sands698842f2008-07-02 17:40:58 +00001068
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001069 // Merge everything together with a MERGE_VALUES node.
1070 ResultVals.push_back(Chain);
Duncan Sands42d7bb82008-12-01 11:41:29 +00001071 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(), &ResultVals[0],
1072 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073}
1074
1075
1076//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001077// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078//===----------------------------------------------------------------------===//
1079// StdCall calling convention seems to be standard for many Windows' API
1080// routines and around. It differs from C calling convention just a little:
1081// callee should clean up the stack, not caller. Symbols should be also
1082// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001083// For info on fast calling convention see Fast Calling Convention (tail call)
1084// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001085
1086/// AddLiveIn - This helper function adds the specified physical register to the
1087/// MachineFunction as a live in value. It also creates a corresponding virtual
1088/// register for it.
1089static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1090 const TargetRegisterClass *RC) {
1091 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001092 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1093 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001094 return VReg;
1095}
1096
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001097/// CallIsStructReturn - Determines whether a CALL node uses struct return
1098/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001099static bool CallIsStructReturn(CallSDNode *TheCall) {
1100 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001101 if (!NumOps)
1102 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001103
Dan Gohman705e3f72008-09-13 01:54:27 +00001104 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001105}
1106
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001107/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1108/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001109static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001110 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001111 if (!NumArgs)
1112 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001113
1114 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001115}
1116
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001117/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1118/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001119/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001120bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001121 if (IsVarArg)
1122 return false;
1123
Dan Gohman705e3f72008-09-13 01:54:27 +00001124 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001125 default:
1126 return false;
1127 case CallingConv::X86_StdCall:
1128 return !Subtarget->is64Bit();
1129 case CallingConv::X86_FastCall:
1130 return !Subtarget->is64Bit();
1131 case CallingConv::Fast:
1132 return PerformTailCallOpt;
1133 }
1134}
1135
Dan Gohman705e3f72008-09-13 01:54:27 +00001136/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1137/// given CallingConvention value.
1138CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001139 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001140 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001141 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001142 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1143 return CC_X86_64_TailCall;
1144 else
1145 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001146 }
1147
Gordon Henriksen18ace102008-01-05 16:56:59 +00001148 if (CC == CallingConv::X86_FastCall)
1149 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001150 else if (CC == CallingConv::Fast)
1151 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001152 else
1153 return CC_X86_32_C;
1154}
1155
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001156/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1157/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001158NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001159X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001160 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001161 if (CC == CallingConv::X86_FastCall)
1162 return FastCall;
1163 else if (CC == CallingConv::X86_StdCall)
1164 return StdCall;
1165 return None;
1166}
1167
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001168
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001169/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1170/// in a register before calling.
1171bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1172 return !IsTailCall && !Is64Bit &&
1173 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1174 Subtarget->isPICStyleGOT();
1175}
1176
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001177/// CallRequiresFnAddressInReg - Check whether the call requires the function
1178/// address to be loaded in a register.
1179bool
1180X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1181 return !Is64Bit && IsTailCall &&
1182 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1183 Subtarget->isPICStyleGOT();
1184}
1185
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001186/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1187/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001188/// the specific parameter attribute. The copy will be passed as a byval
1189/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001190static SDValue
1191CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001192 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001193 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001194 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001195 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001196}
1197
Dan Gohman8181bd12008-07-27 21:46:04 +00001198SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001199 const CCValAssign &VA,
1200 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001201 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001202 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001203 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001204 ISD::ArgFlagsTy Flags =
1205 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001206 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001207 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001208
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001209 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1210 // changed with more analysis.
1211 // In case of tail call optimization mark all arguments mutable. Since they
1212 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001213 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001214 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001215 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001216 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001217 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001218 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001219 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001220}
1221
Dan Gohman8181bd12008-07-27 21:46:04 +00001222SDValue
1223X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001224 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001225 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1226
1227 const Function* Fn = MF.getFunction();
1228 if (Fn->hasExternalLinkage() &&
1229 Subtarget->isTargetCygMing() &&
1230 Fn->getName() == "main")
1231 FuncInfo->setForceFramePointer(true);
1232
1233 // Decorate the function name.
1234 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1235
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001236 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001237 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001238 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001239 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001240 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001241 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001242
1243 assert(!(isVarArg && CC == CallingConv::Fast) &&
1244 "Var args not supported with calling convention fastcc");
1245
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001246 // Assign locations to all of the incoming arguments.
1247 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001248 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001249 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001250
Dan Gohman8181bd12008-07-27 21:46:04 +00001251 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001252 unsigned LastVal = ~0U;
1253 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1254 CCValAssign &VA = ArgLocs[i];
1255 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1256 // places.
1257 assert(VA.getValNo() != LastVal &&
1258 "Don't support value assigned to multiple locs yet");
1259 LastVal = VA.getValNo();
1260
1261 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001262 MVT RegVT = VA.getLocVT();
Devang Patelf3707e82009-01-05 17:31:22 +00001263 TargetRegisterClass *RC = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264 if (RegVT == MVT::i32)
1265 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001266 else if (Is64Bit && RegVT == MVT::i64)
1267 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001268 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001269 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001270 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001271 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001272 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001273 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001274 else if (RegVT.isVector()) {
1275 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001276 if (!Is64Bit)
1277 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1278 else {
1279 // Darwin calling convention passes MMX values in either GPRs or
1280 // XMMs in x86-64. Other targets pass them in memory.
1281 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1282 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1283 RegVT = MVT::v2i64;
1284 } else {
1285 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1286 RegVT = MVT::i64;
1287 }
1288 }
1289 } else {
1290 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001291 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001292
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001293 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001294 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001295
1296 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1297 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1298 // right size.
1299 if (VA.getLocInfo() == CCValAssign::SExt)
1300 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1301 DAG.getValueType(VA.getValVT()));
1302 else if (VA.getLocInfo() == CCValAssign::ZExt)
1303 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1304 DAG.getValueType(VA.getValVT()));
1305
1306 if (VA.getLocInfo() != CCValAssign::Full)
1307 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1308
Gordon Henriksen18ace102008-01-05 16:56:59 +00001309 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001310 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001311 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001312 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1313 else if (RC == X86::VR128RegisterClass) {
1314 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1315 DAG.getConstant(0, MVT::i64));
1316 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1317 }
1318 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001319
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320 ArgValues.push_back(ArgValue);
1321 } else {
1322 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001323 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001324 }
1325 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001326
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001327 // The x86-64 ABI for returning structs by value requires that we copy
1328 // the sret argument into %rax for the return. Save the argument into
1329 // a virtual register so that we can access it from the return points.
1330 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1331 MachineFunction &MF = DAG.getMachineFunction();
1332 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1333 unsigned Reg = FuncInfo->getSRetReturnReg();
1334 if (!Reg) {
1335 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1336 FuncInfo->setSRetReturnReg(Reg);
1337 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001338 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001339 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1340 }
1341
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001342 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001343 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001344 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001345 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001346
1347 // If the function takes variable number of arguments, make a frame index for
1348 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001349 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001350 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1351 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1352 }
1353 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001354 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1355
1356 // FIXME: We should really autogenerate these arrays
1357 static const unsigned GPR64ArgRegsWin64[] = {
1358 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001359 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001360 static const unsigned XMMArgRegsWin64[] = {
1361 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1362 };
1363 static const unsigned GPR64ArgRegs64Bit[] = {
1364 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1365 };
1366 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001367 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1368 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1369 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001370 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1371
1372 if (IsWin64) {
1373 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1374 GPR64ArgRegs = GPR64ArgRegsWin64;
1375 XMMArgRegs = XMMArgRegsWin64;
1376 } else {
1377 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1378 GPR64ArgRegs = GPR64ArgRegs64Bit;
1379 XMMArgRegs = XMMArgRegs64Bit;
1380 }
1381 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1382 TotalNumIntRegs);
1383 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1384 TotalNumXMMRegs);
1385
Gordon Henriksen18ace102008-01-05 16:56:59 +00001386 // For X86-64, if there are vararg parameters that are passed via
1387 // registers, then we must store them to their spots on the stack so they
1388 // may be loaded by deferencing the result of va_next.
1389 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001390 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1391 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1392 TotalNumXMMRegs * 16, 16);
1393
Gordon Henriksen18ace102008-01-05 16:56:59 +00001394 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001395 SmallVector<SDValue, 8> MemOps;
1396 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1397 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001398 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001399 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001400 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1401 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001402 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1403 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001404 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001405 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001406 MemOps.push_back(Store);
1407 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001408 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001409 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001410
Gordon Henriksen18ace102008-01-05 16:56:59 +00001411 // Now store the XMM (fp + vector) parameter registers.
1412 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001413 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001414 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001415 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1416 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001417 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1418 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001419 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001420 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001421 MemOps.push_back(Store);
1422 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001423 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001424 }
1425 if (!MemOps.empty())
1426 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1427 &MemOps[0], MemOps.size());
1428 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001429 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001430
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001431 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001432
Gordon Henriksen18ace102008-01-05 16:56:59 +00001433 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001434 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001435 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001436 BytesCallerReserves = 0;
1437 } else {
1438 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001439 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001440 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001441 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001442 BytesCallerReserves = StackSize;
1443 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001444
Gordon Henriksen18ace102008-01-05 16:56:59 +00001445 if (!Is64Bit) {
1446 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1447 if (CC == CallingConv::X86_FastCall)
1448 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1449 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001450
Anton Korobeynikove844e472007-08-15 17:12:32 +00001451 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001452
1453 // Return the new list of results.
Duncan Sands42d7bb82008-12-01 11:41:29 +00001454 return DAG.getNode(ISD::MERGE_VALUES, Op.getNode()->getVTList(),
1455 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001456}
1457
Dan Gohman8181bd12008-07-27 21:46:04 +00001458SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001459X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001460 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001461 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001462 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001463 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001464 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001465 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001466 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001467 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001468 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001469 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001470 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001471 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001472}
1473
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001474/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001475/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001476SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001477X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001478 SDValue &OutRetAddr,
1479 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001480 bool IsTailCall,
1481 bool Is64Bit,
1482 int FPDiff) {
1483 if (!IsTailCall || FPDiff==0) return Chain;
1484
1485 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001486 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001487 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001488
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001489 // Load the "old" Return address.
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001490 OutRetAddr = DAG.getLoad(VT, Chain, OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001491 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001492}
1493
1494/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1495/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001496static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001497EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001498 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001499 bool Is64Bit, int FPDiff) {
1500 // Store the return address to the appropriate stack slot.
1501 if (!FPDiff) return Chain;
1502 // Calculate the new stack slot for the return address.
1503 int SlotSize = Is64Bit ? 8 : 4;
1504 int NewReturnAddrFI =
1505 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001506 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001507 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001508 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001509 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001510 return Chain;
1511}
1512
Dan Gohman8181bd12008-07-27 21:46:04 +00001513SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001514 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001515 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1516 SDValue Chain = TheCall->getChain();
1517 unsigned CC = TheCall->getCallingConv();
1518 bool isVarArg = TheCall->isVarArg();
1519 bool IsTailCall = TheCall->isTailCall() &&
1520 CC == CallingConv::Fast && PerformTailCallOpt;
1521 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001522 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001523 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001524
1525 assert(!(isVarArg && CC == CallingConv::Fast) &&
1526 "Var args not supported with calling convention fastcc");
1527
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001528 // Analyze operands of the call, assigning locations to each operand.
1529 SmallVector<CCValAssign, 16> ArgLocs;
1530 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001531 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001532
1533 // Get a count of how many bytes are to be pushed on the stack.
1534 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001535 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001536 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001537
Gordon Henriksen18ace102008-01-05 16:56:59 +00001538 int FPDiff = 0;
1539 if (IsTailCall) {
1540 // Lower arguments at fp - stackoffset + fpdiff.
1541 unsigned NumBytesCallerPushed =
1542 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1543 FPDiff = NumBytesCallerPushed - NumBytes;
1544
1545 // Set the delta of movement of the returnaddr stackslot.
1546 // But only set if delta is greater than previous delta.
1547 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1548 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1549 }
1550
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001551 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001552
Dan Gohman8181bd12008-07-27 21:46:04 +00001553 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001554 // Load return adress for tail calls.
1555 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1556 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001557
Dan Gohman8181bd12008-07-27 21:46:04 +00001558 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1559 SmallVector<SDValue, 8> MemOpChains;
1560 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001561
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001562 // Walk the register/memloc assignments, inserting copies/loads. In the case
1563 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001564 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1565 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001566 SDValue Arg = TheCall->getArg(i);
1567 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1568 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001569
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001570 // Promote the value if needed.
1571 switch (VA.getLocInfo()) {
1572 default: assert(0 && "Unknown loc info!");
1573 case CCValAssign::Full: break;
1574 case CCValAssign::SExt:
1575 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1576 break;
1577 case CCValAssign::ZExt:
1578 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1579 break;
1580 case CCValAssign::AExt:
1581 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1582 break;
1583 }
1584
1585 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001586 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001587 MVT RegVT = VA.getLocVT();
1588 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001589 switch (VA.getLocReg()) {
1590 default:
1591 break;
1592 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1593 case X86::R8: {
1594 // Special case: passing MMX values in GPR registers.
1595 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1596 break;
1597 }
1598 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1599 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1600 // Special case: passing MMX values in XMM registers.
1601 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1602 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1603 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1604 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1605 getMOVLMask(2, DAG));
1606 break;
1607 }
1608 }
1609 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001610 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1611 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001612 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001613 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001614 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001615 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1616
Dan Gohman705e3f72008-09-13 01:54:27 +00001617 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1618 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001619 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001620 }
1621 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001622
1623 if (!MemOpChains.empty())
1624 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1625 &MemOpChains[0], MemOpChains.size());
1626
1627 // Build a sequence of copy-to-reg nodes chained together with token chain
1628 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001629 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001630 // Tail call byval lowering might overwrite argument registers so in case of
1631 // tail call optimization the copies to registers are lowered later.
1632 if (!IsTailCall)
1633 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1634 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1635 InFlag);
1636 InFlag = Chain.getValue(1);
1637 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001638
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001639 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001640 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001641 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1642 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1643 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1644 InFlag);
1645 InFlag = Chain.getValue(1);
1646 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001647 // If we are tail calling and generating PIC/GOT style code load the address
1648 // of the callee into ecx. The value in ecx is used as target of the tail
1649 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1650 // calls on PIC/GOT architectures. Normally we would just put the address of
1651 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1652 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001653 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001654 // Note: The actual moving to ecx is done further down.
1655 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001656 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001657 !G->getGlobal()->hasProtectedVisibility())
1658 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001659 else if (isa<ExternalSymbolSDNode>(Callee))
1660 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001661 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001662
Gordon Henriksen18ace102008-01-05 16:56:59 +00001663 if (Is64Bit && isVarArg) {
1664 // From AMD64 ABI document:
1665 // For calls that may call functions that use varargs or stdargs
1666 // (prototype-less calls or calls to functions containing ellipsis (...) in
1667 // the declaration) %al is used as hidden argument to specify the number
1668 // of SSE registers used. The contents of %al do not need to match exactly
1669 // the number of registers, but must be an ubound on the number of SSE
1670 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001671
1672 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001673 // Count the number of XMM registers allocated.
1674 static const unsigned XMMArgRegs[] = {
1675 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1676 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1677 };
1678 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1679
1680 Chain = DAG.getCopyToReg(Chain, X86::AL,
1681 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1682 InFlag = Chain.getValue(1);
1683 }
1684
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001685
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001686 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001687 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001688 SmallVector<SDValue, 8> MemOpChains2;
1689 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001690 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001691 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001692 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001693 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1694 CCValAssign &VA = ArgLocs[i];
1695 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001696 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001697 SDValue Arg = TheCall->getArg(i);
1698 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001699 // Create frame index.
1700 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001701 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001702 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001703 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001704
Duncan Sandsc93fae32008-03-21 09:14:45 +00001705 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001706 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001707 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001708 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001709 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1710 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1711
1712 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001713 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001714 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001715 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001716 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001717 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001718 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001719 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001720 }
1721 }
1722
1723 if (!MemOpChains2.empty())
1724 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001725 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001726
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001727 // Copy arguments to their registers.
1728 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1729 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1730 InFlag);
1731 InFlag = Chain.getValue(1);
1732 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001733 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001734
Gordon Henriksen18ace102008-01-05 16:56:59 +00001735 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001736 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1737 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001738 }
1739
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001740 // If the callee is a GlobalAddress node (quite common, every direct call is)
1741 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1742 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1743 // We should use extra load for direct calls to dllimported functions in
1744 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001745 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1746 getTargetMachine(), true))
Dan Gohman36322c72008-10-18 02:06:02 +00001747 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1748 G->getOffset());
Bill Wendlingfef06052008-09-16 21:48:12 +00001749 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1750 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001751 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001752 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001753
1754 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001755 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001756 Callee,InFlag);
1757 Callee = DAG.getRegister(Opc, getPointerTy());
1758 // Add register as live out.
1759 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001760 }
1761
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001762 // Returns a chain & a flag for retval copy to use.
1763 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001764 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001765
1766 if (IsTailCall) {
1767 Ops.push_back(Chain);
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001768 Ops.push_back(DAG.getIntPtrConstant(NumBytes, true));
1769 Ops.push_back(DAG.getIntPtrConstant(0, true));
Gabor Greif1c80d112008-08-28 21:40:38 +00001770 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001771 Ops.push_back(InFlag);
1772 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1773 InFlag = Chain.getValue(1);
1774
1775 // Returns a chain & a flag for retval copy to use.
1776 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1777 Ops.clear();
1778 }
1779
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001780 Ops.push_back(Chain);
1781 Ops.push_back(Callee);
1782
Gordon Henriksen18ace102008-01-05 16:56:59 +00001783 if (IsTailCall)
1784 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001785
Gordon Henriksen18ace102008-01-05 16:56:59 +00001786 // Add argument registers to the end of the list so that they are known live
1787 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001788 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1789 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1790 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001791
Evan Cheng8ba45e62008-03-18 23:36:35 +00001792 // Add an implicit use GOT pointer in EBX.
1793 if (!IsTailCall && !Is64Bit &&
1794 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1795 Subtarget->isPICStyleGOT())
1796 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1797
1798 // Add an implicit use of AL for x86 vararg functions.
1799 if (Is64Bit && isVarArg)
1800 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1801
Gabor Greif1c80d112008-08-28 21:40:38 +00001802 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001803 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001804
Gordon Henriksen18ace102008-01-05 16:56:59 +00001805 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001806 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001807 "Flag must be set. Depend on flag being set in LowerRET");
1808 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman705e3f72008-09-13 01:54:27 +00001809 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001810
Gabor Greif1c80d112008-08-28 21:40:38 +00001811 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001812 }
1813
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001814 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001815 InFlag = Chain.getValue(1);
1816
1817 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001818 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001819 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001820 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001821 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001822 // If this is is a call to a struct-return function, the callee
1823 // pops the hidden struct pointer, so we have to push it back.
1824 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001825 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001826 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001827 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001828
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001829 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001830 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001831 DAG.getIntPtrConstant(NumBytes, true),
1832 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1833 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001834 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001835 InFlag = Chain.getValue(1);
1836
1837 // Handle result values, copying them out of physregs into vregs that we
1838 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001839 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001840 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001841}
1842
1843
1844//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001845// Fast Calling Convention (tail call) implementation
1846//===----------------------------------------------------------------------===//
1847
1848// Like std call, callee cleans arguments, convention except that ECX is
1849// reserved for storing the tail called function address. Only 2 registers are
1850// free for argument passing (inreg). Tail call optimization is performed
1851// provided:
1852// * tailcallopt is enabled
1853// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001854// On X86_64 architecture with GOT-style position independent code only local
1855// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001856// To keep the stack aligned according to platform abi the function
1857// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1858// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001859// If a tail called function callee has more arguments than the caller the
1860// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001861// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001862// original REtADDR, but before the saved framepointer or the spilled registers
1863// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1864// stack layout:
1865// arg1
1866// arg2
1867// RETADDR
1868// [ new RETADDR
1869// move area ]
1870// (possible EBP)
1871// ESI
1872// EDI
1873// local1 ..
1874
1875/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1876/// for a 16 byte align requirement.
1877unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1878 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001879 MachineFunction &MF = DAG.getMachineFunction();
1880 const TargetMachine &TM = MF.getTarget();
1881 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1882 unsigned StackAlignment = TFI.getStackAlignment();
1883 uint64_t AlignMask = StackAlignment - 1;
1884 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001885 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001886 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1887 // Number smaller than 12 so just add the difference.
1888 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1889 } else {
1890 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1891 Offset = ((~AlignMask) & Offset) + StackAlignment +
1892 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001893 }
Evan Chengded8f902008-09-07 09:07:23 +00001894 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001895}
1896
1897/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001898/// following the call is a return. A function is eligible if caller/callee
1899/// calling conventions match, currently only fastcc supports tail calls, and
1900/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001901bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001902 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001903 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001904 if (!PerformTailCallOpt)
1905 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001906
Dan Gohman705e3f72008-09-13 01:54:27 +00001907 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001908 MachineFunction &MF = DAG.getMachineFunction();
1909 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001910 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001911 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001912 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001913 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001914 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001915 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001916 return true;
1917
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001918 // Can only do local tail calls (in same module, hidden or protected) on
1919 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001920 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1921 return G->getGlobal()->hasHiddenVisibility()
1922 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001923 }
1924 }
Evan Chenge7a87392007-11-02 01:26:22 +00001925
1926 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001927}
1928
Dan Gohmanca4857a2008-09-03 23:12:08 +00001929FastISel *
1930X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001931 MachineModuleInfo *mmo,
Devang Patelfcf1c752009-01-13 00:35:13 +00001932 DwarfWriter *dw,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001933 DenseMap<const Value *, unsigned> &vm,
1934 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001935 MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00001936 DenseMap<const AllocaInst *, int> &am
1937#ifndef NDEBUG
1938 , SmallSet<Instruction*, 8> &cil
1939#endif
1940 ) {
Devang Patelfcf1c752009-01-13 00:35:13 +00001941 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohman9dd43582008-10-14 23:54:11 +00001942#ifndef NDEBUG
1943 , cil
1944#endif
1945 );
Dan Gohman97805ee2008-08-19 21:32:53 +00001946}
1947
1948
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001949//===----------------------------------------------------------------------===//
1950// Other Lowering Hooks
1951//===----------------------------------------------------------------------===//
1952
1953
Dan Gohman8181bd12008-07-27 21:46:04 +00001954SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001955 MachineFunction &MF = DAG.getMachineFunction();
1956 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1957 int ReturnAddrIndex = FuncInfo->getRAIndex();
1958
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001959 if (ReturnAddrIndex == 0) {
1960 // Set up a frame object for the return address.
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001961 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001962 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001963 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001964 }
1965
1966 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1967}
1968
1969
Chris Lattnerebb91142008-12-24 23:53:05 +00001970/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
1971/// specific condition code, returning the condition code and the LHS/RHS of the
1972/// comparison to make.
1973static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1974 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001975 if (!isFP) {
1976 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1977 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1978 // X > -1 -> X == 0, jump !sign.
1979 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00001980 return X86::COND_NS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001981 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1982 // X < 0 -> X == 0, jump on sign.
Chris Lattnerebb91142008-12-24 23:53:05 +00001983 return X86::COND_S;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001984 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00001985 // X < 1 -> X <= 0
1986 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00001987 return X86::COND_LE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001988 }
1989 }
1990
1991 switch (SetCCOpcode) {
Chris Lattnerb8397512008-12-23 23:42:27 +00001992 default: assert(0 && "Invalid integer condition!");
Chris Lattnerebb91142008-12-24 23:53:05 +00001993 case ISD::SETEQ: return X86::COND_E;
1994 case ISD::SETGT: return X86::COND_G;
1995 case ISD::SETGE: return X86::COND_GE;
1996 case ISD::SETLT: return X86::COND_L;
1997 case ISD::SETLE: return X86::COND_LE;
1998 case ISD::SETNE: return X86::COND_NE;
1999 case ISD::SETULT: return X86::COND_B;
2000 case ISD::SETUGT: return X86::COND_A;
2001 case ISD::SETULE: return X86::COND_BE;
2002 case ISD::SETUGE: return X86::COND_AE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002003 }
Chris Lattnerb8397512008-12-23 23:42:27 +00002004 }
2005
2006 // First determine if it is required or is profitable to flip the operands.
Duncan Sandsc2a04622008-10-24 13:03:10 +00002007
Chris Lattnerb8397512008-12-23 23:42:27 +00002008 // If LHS is a foldable load, but RHS is not, flip the condition.
2009 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2010 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2011 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2012 std::swap(LHS, RHS);
Evan Chengfc937c92008-08-28 23:48:31 +00002013 }
2014
Chris Lattnerb8397512008-12-23 23:42:27 +00002015 switch (SetCCOpcode) {
2016 default: break;
2017 case ISD::SETOLT:
2018 case ISD::SETOLE:
2019 case ISD::SETUGT:
2020 case ISD::SETUGE:
2021 std::swap(LHS, RHS);
2022 break;
2023 }
2024
2025 // On a floating point condition, the flags are set as follows:
2026 // ZF PF CF op
2027 // 0 | 0 | 0 | X > Y
2028 // 0 | 0 | 1 | X < Y
2029 // 1 | 0 | 0 | X == Y
2030 // 1 | 1 | 1 | unordered
2031 switch (SetCCOpcode) {
Chris Lattnerebb91142008-12-24 23:53:05 +00002032 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattnerb8397512008-12-23 23:42:27 +00002033 case ISD::SETUEQ:
Chris Lattnerebb91142008-12-24 23:53:05 +00002034 case ISD::SETEQ: return X86::COND_E;
Chris Lattnerb8397512008-12-23 23:42:27 +00002035 case ISD::SETOLT: // flipped
2036 case ISD::SETOGT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002037 case ISD::SETGT: return X86::COND_A;
Chris Lattnerb8397512008-12-23 23:42:27 +00002038 case ISD::SETOLE: // flipped
2039 case ISD::SETOGE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002040 case ISD::SETGE: return X86::COND_AE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002041 case ISD::SETUGT: // flipped
2042 case ISD::SETULT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002043 case ISD::SETLT: return X86::COND_B;
Chris Lattnerb8397512008-12-23 23:42:27 +00002044 case ISD::SETUGE: // flipped
2045 case ISD::SETULE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002046 case ISD::SETLE: return X86::COND_BE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002047 case ISD::SETONE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002048 case ISD::SETNE: return X86::COND_NE;
2049 case ISD::SETUO: return X86::COND_P;
2050 case ISD::SETO: return X86::COND_NP;
Chris Lattnerb8397512008-12-23 23:42:27 +00002051 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002052}
2053
2054/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2055/// code. Current x86 isa includes the following FP cmov instructions:
2056/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2057static bool hasFPCMov(unsigned X86CC) {
2058 switch (X86CC) {
2059 default:
2060 return false;
2061 case X86::COND_B:
2062 case X86::COND_BE:
2063 case X86::COND_E:
2064 case X86::COND_P:
2065 case X86::COND_A:
2066 case X86::COND_AE:
2067 case X86::COND_NE:
2068 case X86::COND_NP:
2069 return true;
2070 }
2071}
2072
2073/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2074/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002075static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002076 if (Op.getOpcode() == ISD::UNDEF)
2077 return true;
2078
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002079 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002080 return (Val >= Low && Val < Hi);
2081}
2082
2083/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2084/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002085static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002086 if (Op.getOpcode() == ISD::UNDEF)
2087 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002088 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002089}
2090
2091/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2092/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2093bool X86::isPSHUFDMask(SDNode *N) {
2094 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2095
Dan Gohman7dc19012007-08-02 21:17:01 +00002096 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002097 return false;
2098
2099 // Check if the value doesn't reference the second vector.
2100 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002101 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002102 if (Arg.getOpcode() == ISD::UNDEF) continue;
2103 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002104 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002105 return false;
2106 }
2107
2108 return true;
2109}
2110
2111/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2112/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2113bool X86::isPSHUFHWMask(SDNode *N) {
2114 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2115
2116 if (N->getNumOperands() != 8)
2117 return false;
2118
2119 // Lower quadword copied in order.
2120 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002121 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002122 if (Arg.getOpcode() == ISD::UNDEF) continue;
2123 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002124 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002125 return false;
2126 }
2127
2128 // Upper quadword shuffled.
2129 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002130 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002131 if (Arg.getOpcode() == ISD::UNDEF) continue;
2132 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002133 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002134 if (Val < 4 || Val > 7)
2135 return false;
2136 }
2137
2138 return true;
2139}
2140
2141/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2142/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2143bool X86::isPSHUFLWMask(SDNode *N) {
2144 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2145
2146 if (N->getNumOperands() != 8)
2147 return false;
2148
2149 // Upper quadword copied in order.
2150 for (unsigned i = 4; i != 8; ++i)
2151 if (!isUndefOrEqual(N->getOperand(i), i))
2152 return false;
2153
2154 // Lower quadword shuffled.
2155 for (unsigned i = 0; i != 4; ++i)
2156 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2157 return false;
2158
2159 return true;
2160}
2161
2162/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2163/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002164static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002165 if (NumElems != 2 && NumElems != 4) return false;
2166
2167 unsigned Half = NumElems / 2;
2168 for (unsigned i = 0; i < Half; ++i)
2169 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2170 return false;
2171 for (unsigned i = Half; i < NumElems; ++i)
2172 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2173 return false;
2174
2175 return true;
2176}
2177
2178bool X86::isSHUFPMask(SDNode *N) {
2179 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2180 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2181}
2182
2183/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2184/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2185/// half elements to come from vector 1 (which would equal the dest.) and
2186/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002187static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002188 if (NumOps != 2 && NumOps != 4) return false;
2189
2190 unsigned Half = NumOps / 2;
2191 for (unsigned i = 0; i < Half; ++i)
2192 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2193 return false;
2194 for (unsigned i = Half; i < NumOps; ++i)
2195 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2196 return false;
2197 return true;
2198}
2199
2200static bool isCommutedSHUFP(SDNode *N) {
2201 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2202 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2203}
2204
2205/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2206/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2207bool X86::isMOVHLPSMask(SDNode *N) {
2208 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2209
2210 if (N->getNumOperands() != 4)
2211 return false;
2212
2213 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2214 return isUndefOrEqual(N->getOperand(0), 6) &&
2215 isUndefOrEqual(N->getOperand(1), 7) &&
2216 isUndefOrEqual(N->getOperand(2), 2) &&
2217 isUndefOrEqual(N->getOperand(3), 3);
2218}
2219
2220/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2221/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2222/// <2, 3, 2, 3>
2223bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2224 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2225
2226 if (N->getNumOperands() != 4)
2227 return false;
2228
2229 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2230 return isUndefOrEqual(N->getOperand(0), 2) &&
2231 isUndefOrEqual(N->getOperand(1), 3) &&
2232 isUndefOrEqual(N->getOperand(2), 2) &&
2233 isUndefOrEqual(N->getOperand(3), 3);
2234}
2235
2236/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2237/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2238bool X86::isMOVLPMask(SDNode *N) {
2239 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2240
2241 unsigned NumElems = N->getNumOperands();
2242 if (NumElems != 2 && NumElems != 4)
2243 return false;
2244
2245 for (unsigned i = 0; i < NumElems/2; ++i)
2246 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2247 return false;
2248
2249 for (unsigned i = NumElems/2; i < NumElems; ++i)
2250 if (!isUndefOrEqual(N->getOperand(i), i))
2251 return false;
2252
2253 return true;
2254}
2255
2256/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2257/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2258/// and MOVLHPS.
2259bool X86::isMOVHPMask(SDNode *N) {
2260 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2261
2262 unsigned NumElems = N->getNumOperands();
2263 if (NumElems != 2 && NumElems != 4)
2264 return false;
2265
2266 for (unsigned i = 0; i < NumElems/2; ++i)
2267 if (!isUndefOrEqual(N->getOperand(i), i))
2268 return false;
2269
2270 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002271 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002272 if (!isUndefOrEqual(Arg, i + NumElems))
2273 return false;
2274 }
2275
2276 return true;
2277}
2278
2279/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2280/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002281bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002282 bool V2IsSplat = false) {
2283 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2284 return false;
2285
2286 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002287 SDValue BitI = Elts[i];
2288 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002289 if (!isUndefOrEqual(BitI, j))
2290 return false;
2291 if (V2IsSplat) {
2292 if (isUndefOrEqual(BitI1, NumElts))
2293 return false;
2294 } else {
2295 if (!isUndefOrEqual(BitI1, j + NumElts))
2296 return false;
2297 }
2298 }
2299
2300 return true;
2301}
2302
2303bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2304 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2305 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2306}
2307
2308/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2309/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002310bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002311 bool V2IsSplat = false) {
2312 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2313 return false;
2314
2315 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002316 SDValue BitI = Elts[i];
2317 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002318 if (!isUndefOrEqual(BitI, j + NumElts/2))
2319 return false;
2320 if (V2IsSplat) {
2321 if (isUndefOrEqual(BitI1, NumElts))
2322 return false;
2323 } else {
2324 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2325 return false;
2326 }
2327 }
2328
2329 return true;
2330}
2331
2332bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2333 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2334 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2335}
2336
2337/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2338/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2339/// <0, 0, 1, 1>
2340bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2341 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2342
2343 unsigned NumElems = N->getNumOperands();
2344 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2345 return false;
2346
2347 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002348 SDValue BitI = N->getOperand(i);
2349 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002350
2351 if (!isUndefOrEqual(BitI, j))
2352 return false;
2353 if (!isUndefOrEqual(BitI1, j))
2354 return false;
2355 }
2356
2357 return true;
2358}
2359
2360/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2361/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2362/// <2, 2, 3, 3>
2363bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2364 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2365
2366 unsigned NumElems = N->getNumOperands();
2367 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2368 return false;
2369
2370 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002371 SDValue BitI = N->getOperand(i);
2372 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002373
2374 if (!isUndefOrEqual(BitI, j))
2375 return false;
2376 if (!isUndefOrEqual(BitI1, j))
2377 return false;
2378 }
2379
2380 return true;
2381}
2382
2383/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2384/// specifies a shuffle of elements that is suitable for input to MOVSS,
2385/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002386static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002387 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002388 return false;
2389
2390 if (!isUndefOrEqual(Elts[0], NumElts))
2391 return false;
2392
2393 for (unsigned i = 1; i < NumElts; ++i) {
2394 if (!isUndefOrEqual(Elts[i], i))
2395 return false;
2396 }
2397
2398 return true;
2399}
2400
2401bool X86::isMOVLMask(SDNode *N) {
2402 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2403 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2404}
2405
2406/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2407/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2408/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002409static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002410 bool V2IsSplat = false,
2411 bool V2IsUndef = false) {
2412 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2413 return false;
2414
2415 if (!isUndefOrEqual(Ops[0], 0))
2416 return false;
2417
2418 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002419 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002420 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2421 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2422 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2423 return false;
2424 }
2425
2426 return true;
2427}
2428
2429static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2430 bool V2IsUndef = false) {
2431 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2432 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2433 V2IsSplat, V2IsUndef);
2434}
2435
2436/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2437/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2438bool X86::isMOVSHDUPMask(SDNode *N) {
2439 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2440
2441 if (N->getNumOperands() != 4)
2442 return false;
2443
2444 // Expect 1, 1, 3, 3
2445 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002446 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002447 if (Arg.getOpcode() == ISD::UNDEF) continue;
2448 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002449 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002450 if (Val != 1) return false;
2451 }
2452
2453 bool HasHi = false;
2454 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002455 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002456 if (Arg.getOpcode() == ISD::UNDEF) continue;
2457 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002458 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002459 if (Val != 3) return false;
2460 HasHi = true;
2461 }
2462
2463 // Don't use movshdup if it can be done with a shufps.
2464 return HasHi;
2465}
2466
2467/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2468/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2469bool X86::isMOVSLDUPMask(SDNode *N) {
2470 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2471
2472 if (N->getNumOperands() != 4)
2473 return false;
2474
2475 // Expect 0, 0, 2, 2
2476 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002477 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002478 if (Arg.getOpcode() == ISD::UNDEF) continue;
2479 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002480 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002481 if (Val != 0) return false;
2482 }
2483
2484 bool HasHi = false;
2485 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002486 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002487 if (Arg.getOpcode() == ISD::UNDEF) continue;
2488 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002489 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002490 if (Val != 2) return false;
2491 HasHi = true;
2492 }
2493
2494 // Don't use movshdup if it can be done with a shufps.
2495 return HasHi;
2496}
2497
2498/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2499/// specifies a identity operation on the LHS or RHS.
2500static bool isIdentityMask(SDNode *N, bool RHS = false) {
2501 unsigned NumElems = N->getNumOperands();
2502 for (unsigned i = 0; i < NumElems; ++i)
2503 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2504 return false;
2505 return true;
2506}
2507
2508/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2509/// a splat of a single element.
2510static bool isSplatMask(SDNode *N) {
2511 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2512
2513 // This is a splat operation if each element of the permute is the same, and
2514 // if the value doesn't reference the second vector.
2515 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002516 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002517 unsigned i = 0;
2518 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002519 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002520 if (isa<ConstantSDNode>(Elt)) {
2521 ElementBase = Elt;
2522 break;
2523 }
2524 }
2525
Gabor Greif1c80d112008-08-28 21:40:38 +00002526 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002527 return false;
2528
2529 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002530 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002531 if (Arg.getOpcode() == ISD::UNDEF) continue;
2532 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2533 if (Arg != ElementBase) return false;
2534 }
2535
2536 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002537 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002538}
2539
Mon P Wang532c9632008-12-23 04:03:27 +00002540/// getSplatMaskEltNo - Given a splat mask, return the index to the element
2541/// we want to splat.
2542static SDValue getSplatMaskEltNo(SDNode *N) {
2543 assert(isSplatMask(N) && "Not a splat mask");
2544 unsigned NumElems = N->getNumOperands();
2545 SDValue ElementBase;
2546 unsigned i = 0;
2547 for (; i != NumElems; ++i) {
2548 SDValue Elt = N->getOperand(i);
2549 if (isa<ConstantSDNode>(Elt))
2550 return Elt;
2551 }
2552 assert(0 && " No splat value found!");
2553 return SDValue();
2554}
2555
2556
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002557/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2558/// a splat of a single element and it's a 2 or 4 element mask.
2559bool X86::isSplatMask(SDNode *N) {
2560 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2561
2562 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2563 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2564 return false;
2565 return ::isSplatMask(N);
2566}
2567
2568/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2569/// specifies a splat of zero element.
2570bool X86::isSplatLoMask(SDNode *N) {
2571 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2572
2573 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2574 if (!isUndefOrEqual(N->getOperand(i), 0))
2575 return false;
2576 return true;
2577}
2578
Evan Chenga2497eb2008-09-25 20:50:48 +00002579/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2580/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2581bool X86::isMOVDDUPMask(SDNode *N) {
2582 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2583
2584 unsigned e = N->getNumOperands() / 2;
2585 for (unsigned i = 0; i < e; ++i)
2586 if (!isUndefOrEqual(N->getOperand(i), i))
2587 return false;
2588 for (unsigned i = 0; i < e; ++i)
2589 if (!isUndefOrEqual(N->getOperand(e+i), i))
2590 return false;
2591 return true;
2592}
2593
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002594/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2595/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2596/// instructions.
2597unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2598 unsigned NumOperands = N->getNumOperands();
2599 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2600 unsigned Mask = 0;
2601 for (unsigned i = 0; i < NumOperands; ++i) {
2602 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002603 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002604 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002605 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002606 if (Val >= NumOperands) Val -= NumOperands;
2607 Mask |= Val;
2608 if (i != NumOperands - 1)
2609 Mask <<= Shift;
2610 }
2611
2612 return Mask;
2613}
2614
2615/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2616/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2617/// instructions.
2618unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2619 unsigned Mask = 0;
2620 // 8 nodes, but we only care about the last 4.
2621 for (unsigned i = 7; i >= 4; --i) {
2622 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002623 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002624 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002625 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002626 Mask |= (Val - 4);
2627 if (i != 4)
2628 Mask <<= 2;
2629 }
2630
2631 return Mask;
2632}
2633
2634/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2635/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2636/// instructions.
2637unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2638 unsigned Mask = 0;
2639 // 8 nodes, but we only care about the first 4.
2640 for (int i = 3; i >= 0; --i) {
2641 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002642 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002643 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002644 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002645 Mask |= Val;
2646 if (i != 0)
2647 Mask <<= 2;
2648 }
2649
2650 return Mask;
2651}
2652
2653/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2654/// specifies a 8 element shuffle that can be broken into a pair of
2655/// PSHUFHW and PSHUFLW.
2656static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2657 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2658
2659 if (N->getNumOperands() != 8)
2660 return false;
2661
2662 // Lower quadword shuffled.
2663 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002664 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665 if (Arg.getOpcode() == ISD::UNDEF) continue;
2666 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002667 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002668 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002669 return false;
2670 }
2671
2672 // Upper quadword shuffled.
2673 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002674 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002675 if (Arg.getOpcode() == ISD::UNDEF) continue;
2676 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002677 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002678 if (Val < 4 || Val > 7)
2679 return false;
2680 }
2681
2682 return true;
2683}
2684
Chris Lattnere6aa3862007-11-25 00:24:49 +00002685/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002686/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002687static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2688 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002689 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002690 MVT VT = Op.getValueType();
2691 MVT MaskVT = Mask.getValueType();
2692 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002693 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002694 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002695
2696 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002697 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002698 if (Arg.getOpcode() == ISD::UNDEF) {
2699 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2700 continue;
2701 }
2702 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002703 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002704 if (Val < NumElems)
2705 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2706 else
2707 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2708 }
2709
2710 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002711 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002712 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2713}
2714
Evan Chenga6769df2007-12-07 21:30:01 +00002715/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2716/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002717static
Dan Gohman8181bd12008-07-27 21:46:04 +00002718SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002719 MVT MaskVT = Mask.getValueType();
2720 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002721 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002722 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002723 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002724 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002725 if (Arg.getOpcode() == ISD::UNDEF) {
2726 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2727 continue;
2728 }
2729 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002730 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002731 if (Val < NumElems)
2732 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2733 else
2734 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2735 }
2736 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2737}
2738
2739
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002740/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2741/// match movhlps. The lower half elements should come from upper half of
2742/// V1 (and in order), and the upper half elements should come from the upper
2743/// half of V2 (and in order).
2744static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2745 unsigned NumElems = Mask->getNumOperands();
2746 if (NumElems != 4)
2747 return false;
2748 for (unsigned i = 0, e = 2; i != e; ++i)
2749 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2750 return false;
2751 for (unsigned i = 2; i != 4; ++i)
2752 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2753 return false;
2754 return true;
2755}
2756
2757/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002758/// is promoted to a vector. It also returns the LoadSDNode by reference if
2759/// required.
2760static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002761 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2762 return false;
2763 N = N->getOperand(0).getNode();
2764 if (!ISD::isNON_EXTLoad(N))
2765 return false;
2766 if (LD)
2767 *LD = cast<LoadSDNode>(N);
2768 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002769}
2770
2771/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2772/// match movlp{s|d}. The lower half elements should come from lower half of
2773/// V1 (and in order), and the upper half elements should come from the upper
2774/// half of V2 (and in order). And since V1 will become the source of the
2775/// MOVLP, it must be either a vector load or a scalar load to vector.
2776static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2777 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2778 return false;
2779 // Is V2 is a vector load, don't do this transformation. We will try to use
2780 // load folding shufps op.
2781 if (ISD::isNON_EXTLoad(V2))
2782 return false;
2783
2784 unsigned NumElems = Mask->getNumOperands();
2785 if (NumElems != 2 && NumElems != 4)
2786 return false;
2787 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2788 if (!isUndefOrEqual(Mask->getOperand(i), i))
2789 return false;
2790 for (unsigned i = NumElems/2; i != NumElems; ++i)
2791 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2792 return false;
2793 return true;
2794}
2795
2796/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2797/// all the same.
2798static bool isSplatVector(SDNode *N) {
2799 if (N->getOpcode() != ISD::BUILD_VECTOR)
2800 return false;
2801
Dan Gohman8181bd12008-07-27 21:46:04 +00002802 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002803 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2804 if (N->getOperand(i) != SplatValue)
2805 return false;
2806 return true;
2807}
2808
2809/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2810/// to an undef.
2811static bool isUndefShuffle(SDNode *N) {
2812 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2813 return false;
2814
Dan Gohman8181bd12008-07-27 21:46:04 +00002815 SDValue V1 = N->getOperand(0);
2816 SDValue V2 = N->getOperand(1);
2817 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002818 unsigned NumElems = Mask.getNumOperands();
2819 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002820 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002821 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002822 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002823 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2824 return false;
2825 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2826 return false;
2827 }
2828 }
2829 return true;
2830}
2831
2832/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2833/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002834static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002836 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002837 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002838 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002839}
2840
2841/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2842/// to an zero vector.
2843static bool isZeroShuffle(SDNode *N) {
2844 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2845 return false;
2846
Dan Gohman8181bd12008-07-27 21:46:04 +00002847 SDValue V1 = N->getOperand(0);
2848 SDValue V2 = N->getOperand(1);
2849 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002850 unsigned NumElems = Mask.getNumOperands();
2851 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002852 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002853 if (Arg.getOpcode() == ISD::UNDEF)
2854 continue;
2855
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002856 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002857 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002858 unsigned Opc = V1.getNode()->getOpcode();
2859 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002860 continue;
2861 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002862 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002863 return false;
2864 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002865 unsigned Opc = V2.getNode()->getOpcode();
2866 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002867 continue;
2868 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002869 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002870 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002871 }
2872 }
2873 return true;
2874}
2875
2876/// getZeroVector - Returns a vector of specified type with all zero elements.
2877///
Dan Gohman8181bd12008-07-27 21:46:04 +00002878static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002879 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002880
2881 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2882 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002883 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002884 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002885 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002886 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002887 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002888 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002889 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002890 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002891 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002892 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2893 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002894 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002895}
2896
Chris Lattnere6aa3862007-11-25 00:24:49 +00002897/// getOnesVector - Returns a vector of specified type with all bits set.
2898///
Dan Gohman8181bd12008-07-27 21:46:04 +00002899static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002900 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002901
2902 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2903 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002904 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2905 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002906 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002907 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2908 else // SSE
2909 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2910 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2911}
2912
2913
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002914/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2915/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002916static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002917 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2918
2919 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002920 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002921 unsigned NumElems = Mask.getNumOperands();
2922 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002923 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002924 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002925 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002926 if (Val > NumElems) {
2927 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2928 Changed = true;
2929 }
2930 }
2931 MaskVec.push_back(Arg);
2932 }
2933
2934 if (Changed)
2935 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2936 &MaskVec[0], MaskVec.size());
2937 return Mask;
2938}
2939
2940/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2941/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002942static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002943 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2944 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002945
Dan Gohman8181bd12008-07-27 21:46:04 +00002946 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002947 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2948 for (unsigned i = 1; i != NumElems; ++i)
2949 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2950 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2951}
2952
2953/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2954/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002955static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002956 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2957 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002958 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002959 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2960 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2961 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2962 }
2963 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2964}
2965
2966/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2967/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002968static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002969 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2970 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002971 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002972 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002973 for (unsigned i = 0; i != Half; ++i) {
2974 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2975 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2976 }
2977 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2978}
2979
Chris Lattner2d91b962008-03-09 01:05:04 +00002980/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2981/// element #0 of a vector with the specified index, leaving the rest of the
2982/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002983static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002984 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002985 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2986 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002987 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002988 // Element #0 of the result gets the elt we are replacing.
2989 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2990 for (unsigned i = 1; i != NumElems; ++i)
2991 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2992 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2993}
2994
Evan Chengbf8b2c52008-04-05 00:30:36 +00002995/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002996static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002997 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2998 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002999 if (PVT == VT)
3000 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00003001 SDValue V1 = Op.getOperand(0);
3002 SDValue Mask = Op.getOperand(2);
Mon P Wang532c9632008-12-23 04:03:27 +00003003 unsigned MaskNumElems = Mask.getNumOperands();
3004 unsigned NumElems = MaskNumElems;
Evan Chengbf8b2c52008-04-05 00:30:36 +00003005 // Special handling of v4f32 -> v4i32.
3006 if (VT != MVT::v4f32) {
Mon P Wang532c9632008-12-23 04:03:27 +00003007 // Find which element we want to splat.
3008 SDNode* EltNoNode = getSplatMaskEltNo(Mask.getNode()).getNode();
3009 unsigned EltNo = cast<ConstantSDNode>(EltNoNode)->getZExtValue();
3010 // unpack elements to the correct location
Evan Chengbf8b2c52008-04-05 00:30:36 +00003011 while (NumElems > 4) {
Mon P Wang532c9632008-12-23 04:03:27 +00003012 if (EltNo < NumElems/2) {
3013 Mask = getUnpacklMask(MaskNumElems, DAG);
3014 } else {
3015 Mask = getUnpackhMask(MaskNumElems, DAG);
3016 EltNo -= NumElems/2;
3017 }
Evan Chengbf8b2c52008-04-05 00:30:36 +00003018 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
3019 NumElems >>= 1;
3020 }
Mon P Wang532c9632008-12-23 04:03:27 +00003021 SDValue Cst = DAG.getConstant(EltNo, MVT::i32);
3022 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003023 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003024
Evan Chengbf8b2c52008-04-05 00:30:36 +00003025 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00003026 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00003027 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003028 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3029}
3030
Evan Chenga2497eb2008-09-25 20:50:48 +00003031/// isVectorLoad - Returns true if the node is a vector load, a scalar
3032/// load that's promoted to vector, or a load bitcasted.
3033static bool isVectorLoad(SDValue Op) {
3034 assert(Op.getValueType().isVector() && "Expected a vector type");
3035 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
3036 Op.getOpcode() == ISD::BIT_CONVERT) {
3037 return isa<LoadSDNode>(Op.getOperand(0));
3038 }
3039 return isa<LoadSDNode>(Op);
3040}
3041
3042
3043/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3044///
3045static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3046 SelectionDAG &DAG, bool HasSSE3) {
3047 // If we have sse3 and shuffle has more than one use or input is a load, then
3048 // use movddup. Otherwise, use movlhps.
3049 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3050 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3051 MVT VT = Op.getValueType();
3052 if (VT == PVT)
3053 return Op;
3054 unsigned NumElems = PVT.getVectorNumElements();
3055 if (NumElems == 2) {
3056 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3057 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
3058 } else {
3059 assert(NumElems == 4);
3060 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3061 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
3062 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst0, Cst1, Cst0, Cst1);
3063 }
3064
3065 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
3066 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
3067 DAG.getNode(ISD::UNDEF, PVT), Mask);
3068 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3069}
3070
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003071/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003072/// vector of zero or undef vector. This produces a shuffle where the low
3073/// element of V2 is swizzled into the zero/undef vector, landing at element
3074/// 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 +00003075static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003076 bool isZero, bool HasSSE2,
3077 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003078 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003079 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00003080 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003081 unsigned NumElems = V2.getValueType().getVectorNumElements();
3082 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3083 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003084 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003085 for (unsigned i = 0; i != NumElems; ++i)
3086 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3087 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3088 else
3089 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003090 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003091 &MaskVec[0], MaskVec.size());
3092 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3093}
3094
Evan Chengdea99362008-05-29 08:22:04 +00003095/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3096/// a shuffle that is zero.
3097static
Dan Gohman8181bd12008-07-27 21:46:04 +00003098unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003099 unsigned NumElems, bool Low,
3100 SelectionDAG &DAG) {
3101 unsigned NumZeros = 0;
3102 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003103 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003104 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003105 if (Idx.getOpcode() == ISD::UNDEF) {
3106 ++NumZeros;
3107 continue;
3108 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003109 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3110 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003111 ++NumZeros;
3112 else
3113 break;
3114 }
3115 return NumZeros;
3116}
3117
3118/// isVectorShift - Returns true if the shuffle can be implemented as a
3119/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003120static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3121 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003122 unsigned NumElems = Mask.getNumOperands();
3123
3124 isLeft = true;
3125 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3126 if (!NumZeros) {
3127 isLeft = false;
3128 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3129 if (!NumZeros)
3130 return false;
3131 }
3132
3133 bool SeenV1 = false;
3134 bool SeenV2 = false;
3135 for (unsigned i = NumZeros; i < NumElems; ++i) {
3136 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003137 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003138 if (Idx.getOpcode() == ISD::UNDEF)
3139 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003140 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003141 if (Index < NumElems)
3142 SeenV1 = true;
3143 else {
3144 Index -= NumElems;
3145 SeenV2 = true;
3146 }
3147 if (Index != Val)
3148 return false;
3149 }
3150 if (SeenV1 && SeenV2)
3151 return false;
3152
3153 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3154 ShAmt = NumZeros;
3155 return true;
3156}
3157
3158
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003159/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3160///
Dan Gohman8181bd12008-07-27 21:46:04 +00003161static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003162 unsigned NumNonZero, unsigned NumZero,
3163 SelectionDAG &DAG, TargetLowering &TLI) {
3164 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003165 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003166
Dan Gohman8181bd12008-07-27 21:46:04 +00003167 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003168 bool First = true;
3169 for (unsigned i = 0; i < 16; ++i) {
3170 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3171 if (ThisIsNonZero && First) {
3172 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003173 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003174 else
3175 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3176 First = false;
3177 }
3178
3179 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003180 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003181 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3182 if (LastIsNonZero) {
3183 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3184 }
3185 if (ThisIsNonZero) {
3186 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3187 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3188 ThisElt, DAG.getConstant(8, MVT::i8));
3189 if (LastIsNonZero)
3190 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3191 } else
3192 ThisElt = LastElt;
3193
Gabor Greif1c80d112008-08-28 21:40:38 +00003194 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003195 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003196 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003197 }
3198 }
3199
3200 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3201}
3202
3203/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3204///
Dan Gohman8181bd12008-07-27 21:46:04 +00003205static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003206 unsigned NumNonZero, unsigned NumZero,
3207 SelectionDAG &DAG, TargetLowering &TLI) {
3208 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003209 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003210
Dan Gohman8181bd12008-07-27 21:46:04 +00003211 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003212 bool First = true;
3213 for (unsigned i = 0; i < 8; ++i) {
3214 bool isNonZero = (NonZeros & (1 << i)) != 0;
3215 if (isNonZero) {
3216 if (First) {
3217 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003218 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003219 else
3220 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3221 First = false;
3222 }
3223 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003224 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003225 }
3226 }
3227
3228 return V;
3229}
3230
Evan Chengdea99362008-05-29 08:22:04 +00003231/// getVShift - Return a vector logical shift node.
3232///
Dan Gohman8181bd12008-07-27 21:46:04 +00003233static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003234 unsigned NumBits, SelectionDAG &DAG,
3235 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003236 bool isMMX = VT.getSizeInBits() == 64;
3237 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003238 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3239 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3240 return DAG.getNode(ISD::BIT_CONVERT, VT,
3241 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003242 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003243}
3244
Dan Gohman8181bd12008-07-27 21:46:04 +00003245SDValue
3246X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003247 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003248 if (ISD::isBuildVectorAllZeros(Op.getNode())
3249 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003250 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3251 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3252 // eliminated on x86-32 hosts.
3253 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3254 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003255
Gabor Greif1c80d112008-08-28 21:40:38 +00003256 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003257 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003258 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003259 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003260
Duncan Sands92c43912008-06-06 12:08:01 +00003261 MVT VT = Op.getValueType();
3262 MVT EVT = VT.getVectorElementType();
3263 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003264
3265 unsigned NumElems = Op.getNumOperands();
3266 unsigned NumZero = 0;
3267 unsigned NumNonZero = 0;
3268 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003269 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003270 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003271 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003272 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003273 if (Elt.getOpcode() == ISD::UNDEF)
3274 continue;
3275 Values.insert(Elt);
3276 if (Elt.getOpcode() != ISD::Constant &&
3277 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003278 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003279 if (isZeroNode(Elt))
3280 NumZero++;
3281 else {
3282 NonZeros |= (1 << i);
3283 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003284 }
3285 }
3286
3287 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003288 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3289 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003290 }
3291
Chris Lattner66a4dda2008-03-09 05:42:06 +00003292 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003293 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003294 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003295 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003296
Chris Lattner2d91b962008-03-09 01:05:04 +00003297 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3298 // the value are obviously zero, truncate the value to i32 and do the
3299 // insertion that way. Only do this if the value is non-constant or if the
3300 // value is a constant being inserted into element 0. It is cheaper to do
3301 // a constant pool load than it is to do a movd + shuffle.
3302 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3303 (!IsAllConstants || Idx == 0)) {
3304 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3305 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003306 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3307 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003308
3309 // Truncate the value (which may itself be a constant) to i32, and
3310 // convert it to a vector with movd (S2V+shuffle to zero extend).
3311 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3312 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003313 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3314 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003315
3316 // Now we have our 32-bit value zero extended in the low element of
3317 // a vector. If Idx != 0, swizzle it into place.
3318 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003319 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003320 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3321 getSwapEltZeroMask(VecElts, Idx, DAG)
3322 };
3323 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3324 }
3325 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3326 }
3327 }
3328
Chris Lattnerac914892008-03-08 22:59:52 +00003329 // If we have a constant or non-constant insertion into the low element of
3330 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3331 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3332 // depending on what the source datatype is. Because we can only get here
3333 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3334 if (Idx == 0 &&
3335 // Don't do this for i64 values on x86-32.
3336 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003337 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003338 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003339 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3340 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003341 }
Evan Chengdea99362008-05-29 08:22:04 +00003342
3343 // Is it a vector logical left shift?
3344 if (NumElems == 2 && Idx == 1 &&
3345 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003346 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003347 return getVShift(true, VT,
3348 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3349 NumBits/2, DAG, *this);
3350 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003351
3352 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003353 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003354
Chris Lattnerac914892008-03-08 22:59:52 +00003355 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3356 // is a non-constant being inserted into an element other than the low one,
3357 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3358 // movd/movss) to move this into the low element, then shuffle it into
3359 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003360 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003361 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3362
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003363 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003364 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3365 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003366 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3367 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003368 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003369 for (unsigned i = 0; i < NumElems; i++)
3370 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003371 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003372 &MaskVec[0], MaskVec.size());
3373 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3374 DAG.getNode(ISD::UNDEF, VT), Mask);
3375 }
3376 }
3377
Chris Lattner66a4dda2008-03-09 05:42:06 +00003378 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3379 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003380 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003381
Dan Gohman21463242007-07-24 22:55:08 +00003382 // A vector full of immediates; various special cases are already
3383 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003384 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003385 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003386
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003387 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003388 if (EVTBits == 64) {
3389 if (NumNonZero == 1) {
3390 // One half is zero or undef.
3391 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003392 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003393 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003394 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3395 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003396 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003397 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003398 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003399
3400 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3401 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003402 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003403 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003404 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003405 }
3406
3407 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003408 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003409 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003410 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003411 }
3412
3413 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003414 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003415 V.resize(NumElems);
3416 if (NumElems == 4 && NumZero > 0) {
3417 for (unsigned i = 0; i < 4; ++i) {
3418 bool isZero = !(NonZeros & (1 << i));
3419 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003420 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003421 else
3422 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3423 }
3424
3425 for (unsigned i = 0; i < 2; ++i) {
3426 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3427 default: break;
3428 case 0:
3429 V[i] = V[i*2]; // Must be a zero vector.
3430 break;
3431 case 1:
3432 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3433 getMOVLMask(NumElems, DAG));
3434 break;
3435 case 2:
3436 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3437 getMOVLMask(NumElems, DAG));
3438 break;
3439 case 3:
3440 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3441 getUnpacklMask(NumElems, DAG));
3442 break;
3443 }
3444 }
3445
Duncan Sands92c43912008-06-06 12:08:01 +00003446 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3447 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003448 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003449 bool Reverse = (NonZeros & 0x3) == 2;
3450 for (unsigned i = 0; i < 2; ++i)
3451 if (Reverse)
3452 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3453 else
3454 MaskVec.push_back(DAG.getConstant(i, EVT));
3455 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3456 for (unsigned i = 0; i < 2; ++i)
3457 if (Reverse)
3458 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3459 else
3460 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003461 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003462 &MaskVec[0], MaskVec.size());
3463 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3464 }
3465
3466 if (Values.size() > 2) {
3467 // Expand into a number of unpckl*.
3468 // e.g. for v4f32
3469 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3470 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3471 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003472 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003473 for (unsigned i = 0; i < NumElems; ++i)
3474 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3475 NumElems >>= 1;
3476 while (NumElems != 0) {
3477 for (unsigned i = 0; i < NumElems; ++i)
3478 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3479 UnpckMask);
3480 NumElems >>= 1;
3481 }
3482 return V[0];
3483 }
3484
Dan Gohman8181bd12008-07-27 21:46:04 +00003485 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003486}
3487
Evan Chengfca29242007-12-07 08:07:39 +00003488static
Dan Gohman8181bd12008-07-27 21:46:04 +00003489SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003490 SDValue PermMask, SelectionDAG &DAG,
3491 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003492 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003493 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3494 MVT MaskEVT = MaskVT.getVectorElementType();
3495 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003496 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3497 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003498
3499 // First record which half of which vector the low elements come from.
3500 SmallVector<unsigned, 4> LowQuad(4);
3501 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003502 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003503 if (Elt.getOpcode() == ISD::UNDEF)
3504 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003505 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003506 int QuadIdx = EltIdx / 4;
3507 ++LowQuad[QuadIdx];
3508 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003509
Evan Cheng75184a92007-12-11 01:46:18 +00003510 int BestLowQuad = -1;
3511 unsigned MaxQuad = 1;
3512 for (unsigned i = 0; i < 4; ++i) {
3513 if (LowQuad[i] > MaxQuad) {
3514 BestLowQuad = i;
3515 MaxQuad = LowQuad[i];
3516 }
Evan Chengfca29242007-12-07 08:07:39 +00003517 }
3518
Evan Cheng75184a92007-12-11 01:46:18 +00003519 // Record which half of which vector the high elements come from.
3520 SmallVector<unsigned, 4> HighQuad(4);
3521 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003522 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003523 if (Elt.getOpcode() == ISD::UNDEF)
3524 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003525 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003526 int QuadIdx = EltIdx / 4;
3527 ++HighQuad[QuadIdx];
3528 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003529
Evan Cheng75184a92007-12-11 01:46:18 +00003530 int BestHighQuad = -1;
3531 MaxQuad = 1;
3532 for (unsigned i = 0; i < 4; ++i) {
3533 if (HighQuad[i] > MaxQuad) {
3534 BestHighQuad = i;
3535 MaxQuad = HighQuad[i];
3536 }
3537 }
3538
3539 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3540 if (BestLowQuad != -1 || BestHighQuad != -1) {
3541 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003542 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003543
Evan Cheng75184a92007-12-11 01:46:18 +00003544 if (BestLowQuad != -1)
3545 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3546 else
3547 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003548
Evan Cheng75184a92007-12-11 01:46:18 +00003549 if (BestHighQuad != -1)
3550 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3551 else
3552 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003553
Dan Gohman8181bd12008-07-27 21:46:04 +00003554 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003555 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3556 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3557 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3558 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3559
3560 // Now sort high and low parts separately.
3561 BitVector InOrder(8);
3562 if (BestLowQuad != -1) {
3563 // Sort lower half in order using PSHUFLW.
3564 MaskVec.clear();
3565 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003566
Evan Cheng75184a92007-12-11 01:46:18 +00003567 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003568 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003569 if (Elt.getOpcode() == ISD::UNDEF) {
3570 MaskVec.push_back(Elt);
3571 InOrder.set(i);
3572 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003573 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003574 if (EltIdx != i)
3575 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003576
Evan Cheng75184a92007-12-11 01:46:18 +00003577 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003578
Evan Cheng75184a92007-12-11 01:46:18 +00003579 // If this element is in the right place after this shuffle, then
3580 // remember it.
3581 if ((int)(EltIdx / 4) == BestLowQuad)
3582 InOrder.set(i);
3583 }
3584 }
3585 if (AnyOutOrder) {
3586 for (unsigned i = 4; i != 8; ++i)
3587 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003588 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003589 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3590 }
3591 }
3592
3593 if (BestHighQuad != -1) {
3594 // Sort high half in order using PSHUFHW if possible.
3595 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003596
Evan Cheng75184a92007-12-11 01:46:18 +00003597 for (unsigned i = 0; i != 4; ++i)
3598 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003599
Evan Cheng75184a92007-12-11 01:46:18 +00003600 bool AnyOutOrder = false;
3601 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003602 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003603 if (Elt.getOpcode() == ISD::UNDEF) {
3604 MaskVec.push_back(Elt);
3605 InOrder.set(i);
3606 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003607 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003608 if (EltIdx != i)
3609 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003610
Evan Cheng75184a92007-12-11 01:46:18 +00003611 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003612
Evan Cheng75184a92007-12-11 01:46:18 +00003613 // If this element is in the right place after this shuffle, then
3614 // remember it.
3615 if ((int)(EltIdx / 4) == BestHighQuad)
3616 InOrder.set(i);
3617 }
3618 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003619
Evan Cheng75184a92007-12-11 01:46:18 +00003620 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003621 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003622 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3623 }
3624 }
3625
3626 // The other elements are put in the right place using pextrw and pinsrw.
3627 for (unsigned i = 0; i != 8; ++i) {
3628 if (InOrder[i])
3629 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003630 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003631 if (Elt.getOpcode() == ISD::UNDEF)
3632 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003633 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003634 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003635 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3636 DAG.getConstant(EltIdx, PtrVT))
3637 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3638 DAG.getConstant(EltIdx - 8, PtrVT));
3639 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3640 DAG.getConstant(i, PtrVT));
3641 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003642
Evan Cheng75184a92007-12-11 01:46:18 +00003643 return NewV;
3644 }
3645
Bill Wendling2c7cd592008-08-21 22:35:37 +00003646 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3647 // few as possible. First, let's find out how many elements are already in the
3648 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003649 unsigned V1InOrder = 0;
3650 unsigned V1FromV1 = 0;
3651 unsigned V2InOrder = 0;
3652 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003653 SmallVector<SDValue, 8> V1Elts;
3654 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003655 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003656 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003657 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003658 V1Elts.push_back(Elt);
3659 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003660 ++V1InOrder;
3661 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003662 continue;
3663 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003664 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003665 if (EltIdx == i) {
3666 V1Elts.push_back(Elt);
3667 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3668 ++V1InOrder;
3669 } else if (EltIdx == i+8) {
3670 V1Elts.push_back(Elt);
3671 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3672 ++V2InOrder;
3673 } else if (EltIdx < 8) {
3674 V1Elts.push_back(Elt);
Mon P Wang532c9632008-12-23 04:03:27 +00003675 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003676 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003677 } else {
Mon P Wang532c9632008-12-23 04:03:27 +00003678 V1Elts.push_back(Elt);
Evan Cheng75184a92007-12-11 01:46:18 +00003679 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3680 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003681 }
3682 }
3683
3684 if (V2InOrder > V1InOrder) {
3685 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3686 std::swap(V1, V2);
3687 std::swap(V1Elts, V2Elts);
3688 std::swap(V1FromV1, V2FromV2);
3689 }
3690
Evan Cheng75184a92007-12-11 01:46:18 +00003691 if ((V1FromV1 + V1InOrder) != 8) {
3692 // Some elements are from V2.
3693 if (V1FromV1) {
3694 // If there are elements that are from V1 but out of place,
3695 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003696 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003697 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003698 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003699 if (Elt.getOpcode() == ISD::UNDEF) {
3700 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3701 continue;
3702 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003703 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003704 if (EltIdx >= 8)
3705 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3706 else
3707 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3708 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003709 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003710 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003711 }
Evan Cheng75184a92007-12-11 01:46:18 +00003712
3713 NewV = V1;
3714 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003715 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003716 if (Elt.getOpcode() == ISD::UNDEF)
3717 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003718 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003719 if (EltIdx < 8)
3720 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003721 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003722 DAG.getConstant(EltIdx - 8, PtrVT));
3723 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3724 DAG.getConstant(i, PtrVT));
3725 }
3726 return NewV;
3727 } else {
3728 // All elements are from V1.
3729 NewV = V1;
3730 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003731 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003732 if (Elt.getOpcode() == ISD::UNDEF)
3733 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003734 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003735 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003736 DAG.getConstant(EltIdx, PtrVT));
3737 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3738 DAG.getConstant(i, PtrVT));
3739 }
3740 return NewV;
3741 }
3742}
3743
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003744/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3745/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3746/// done when every pair / quad of shuffle mask elements point to elements in
3747/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003748/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3749static
Dan Gohman8181bd12008-07-27 21:46:04 +00003750SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003751 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003752 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003753 TargetLowering &TLI) {
3754 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003755 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003756 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003757 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003758 MVT NewVT = MaskVT;
3759 switch (VT.getSimpleVT()) {
3760 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003761 case MVT::v4f32: NewVT = MVT::v2f64; break;
3762 case MVT::v4i32: NewVT = MVT::v2i64; break;
3763 case MVT::v8i16: NewVT = MVT::v4i32; break;
3764 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003765 }
3766
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003767 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003768 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003769 NewVT = MVT::v2i64;
3770 else
3771 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003772 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003773 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003774 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003775 for (unsigned i = 0; i < NumElems; i += Scale) {
3776 unsigned StartIdx = ~0U;
3777 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003778 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003779 if (Elt.getOpcode() == ISD::UNDEF)
3780 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003781 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003782 if (StartIdx == ~0U)
3783 StartIdx = EltIdx - (EltIdx % Scale);
3784 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003785 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003786 }
3787 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003788 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003789 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003790 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003791 }
3792
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003793 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3794 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3795 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3796 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3797 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003798}
3799
Evan Chenge9b9c672008-05-09 21:53:03 +00003800/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003801///
Dan Gohman8181bd12008-07-27 21:46:04 +00003802static SDValue getVZextMovL(MVT VT, MVT OpVT,
3803 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003804 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003805 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3806 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003807 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003808 LD = dyn_cast<LoadSDNode>(SrcOp);
3809 if (!LD) {
3810 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3811 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003812 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003813 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3814 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3815 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3816 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3817 // PR2108
3818 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3819 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003820 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003821 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003822 SrcOp.getOperand(0)
3823 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003824 }
3825 }
3826 }
3827
3828 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003829 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003830 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3831}
3832
Evan Chengf50554e2008-07-22 21:13:36 +00003833/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3834/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003835static SDValue
3836LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3837 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003838 MVT MaskVT = PermMask.getValueType();
3839 MVT MaskEVT = MaskVT.getVectorElementType();
3840 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003841 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003842 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003843 unsigned NumHi = 0;
3844 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003845 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003846 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003847 if (Elt.getOpcode() == ISD::UNDEF) {
3848 Locs[i] = std::make_pair(-1, -1);
3849 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003850 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003851 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003852 if (Val < 4) {
3853 Locs[i] = std::make_pair(0, NumLo);
3854 Mask1[NumLo] = Elt;
3855 NumLo++;
3856 } else {
3857 Locs[i] = std::make_pair(1, NumHi);
3858 if (2+NumHi < 4)
3859 Mask1[2+NumHi] = Elt;
3860 NumHi++;
3861 }
3862 }
3863 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003864
Evan Chengf50554e2008-07-22 21:13:36 +00003865 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003866 // If no more than two elements come from either vector. This can be
3867 // implemented with two shuffles. First shuffle gather the elements.
3868 // The second shuffle, which takes the first shuffle as both of its
3869 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003870 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3871 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3872 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003873
Dan Gohman8181bd12008-07-27 21:46:04 +00003874 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003875 for (unsigned i = 0; i != 4; ++i) {
3876 if (Locs[i].first == -1)
3877 continue;
3878 else {
3879 unsigned Idx = (i < 2) ? 0 : 4;
3880 Idx += Locs[i].first * 2 + Locs[i].second;
3881 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3882 }
3883 }
3884
3885 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3886 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3887 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003888 } else if (NumLo == 3 || NumHi == 3) {
3889 // Otherwise, we must have three elements from one vector, call it X, and
3890 // one element from the other, call it Y. First, use a shufps to build an
3891 // intermediate vector with the one element from Y and the element from X
3892 // that will be in the same half in the final destination (the indexes don't
3893 // matter). Then, use a shufps to build the final vector, taking the half
3894 // containing the element from Y from the intermediate, and the other half
3895 // from X.
3896 if (NumHi == 3) {
3897 // Normalize it so the 3 elements come from V1.
3898 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3899 std::swap(V1, V2);
3900 }
3901
3902 // Find the element from V2.
3903 unsigned HiIndex;
3904 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003905 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003906 if (Elt.getOpcode() == ISD::UNDEF)
3907 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003908 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003909 if (Val >= 4)
3910 break;
3911 }
3912
3913 Mask1[0] = PermMask.getOperand(HiIndex);
3914 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3915 Mask1[2] = PermMask.getOperand(HiIndex^1);
3916 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3917 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3918 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3919
3920 if (HiIndex >= 2) {
3921 Mask1[0] = PermMask.getOperand(0);
3922 Mask1[1] = PermMask.getOperand(1);
3923 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3924 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3925 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3926 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3927 } else {
3928 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3929 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3930 Mask1[2] = PermMask.getOperand(2);
3931 Mask1[3] = PermMask.getOperand(3);
3932 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003933 Mask1[2] =
3934 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3935 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003936 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003937 Mask1[3] =
3938 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3939 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003940 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3941 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3942 }
Evan Chengf50554e2008-07-22 21:13:36 +00003943 }
3944
3945 // Break it into (shuffle shuffle_hi, shuffle_lo).
3946 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003947 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3948 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3949 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003950 unsigned MaskIdx = 0;
3951 unsigned LoIdx = 0;
3952 unsigned HiIdx = 2;
3953 for (unsigned i = 0; i != 4; ++i) {
3954 if (i == 2) {
3955 MaskPtr = &HiMask;
3956 MaskIdx = 1;
3957 LoIdx = 0;
3958 HiIdx = 2;
3959 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003960 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003961 if (Elt.getOpcode() == ISD::UNDEF) {
3962 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003963 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003964 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3965 (*MaskPtr)[LoIdx] = Elt;
3966 LoIdx++;
3967 } else {
3968 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3969 (*MaskPtr)[HiIdx] = Elt;
3970 HiIdx++;
3971 }
3972 }
3973
Dan Gohman8181bd12008-07-27 21:46:04 +00003974 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003975 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3976 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003977 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003978 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3979 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003980 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003981 for (unsigned i = 0; i != 4; ++i) {
3982 if (Locs[i].first == -1) {
3983 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3984 } else {
3985 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3986 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3987 }
3988 }
3989 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3990 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3991 &MaskOps[0], MaskOps.size()));
3992}
3993
Dan Gohman8181bd12008-07-27 21:46:04 +00003994SDValue
3995X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3996 SDValue V1 = Op.getOperand(0);
3997 SDValue V2 = Op.getOperand(1);
3998 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003999 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004000 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00004001 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004002 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4003 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
4004 bool V1IsSplat = false;
4005 bool V2IsSplat = false;
4006
Gabor Greif1c80d112008-08-28 21:40:38 +00004007 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004008 return DAG.getNode(ISD::UNDEF, VT);
4009
Gabor Greif1c80d112008-08-28 21:40:38 +00004010 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00004011 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004012
Gabor Greif1c80d112008-08-28 21:40:38 +00004013 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004014 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00004015 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004016 return V2;
4017
Evan Chengae6c9212008-09-25 23:35:16 +00004018 // Canonicalize movddup shuffles.
4019 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Chengbdd9d9f2008-10-06 21:13:08 +00004020 VT.getSizeInBits() == 128 &&
Evan Chengae6c9212008-09-25 23:35:16 +00004021 X86::isMOVDDUPMask(PermMask.getNode()))
4022 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
4023
Gabor Greif1c80d112008-08-28 21:40:38 +00004024 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004025 if (isMMX || NumElems < 4) return Op;
4026 // Promote it to a v4{if}32 splat.
4027 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004028 }
4029
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004030 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4031 // do it!
4032 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004033 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004034 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004035 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
4036 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4037 // FIXME: Figure out a cleaner way to do this.
4038 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004039 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004040 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004041 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004042 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004043 SDValue NewV1 = NewOp.getOperand(0);
4044 SDValue NewV2 = NewOp.getOperand(1);
4045 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00004046 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004047 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00004048 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004049 }
4050 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004051 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004052 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004053 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004054 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004055 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00004056 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004057 }
4058 }
4059
Evan Chengdea99362008-05-29 08:22:04 +00004060 // Check if this can be converted into a logical shift.
4061 bool isLeft = false;
4062 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004063 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00004064 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4065 if (isShift && ShVal.hasOneUse()) {
4066 // If the shifted value has multiple uses, it may be cheaper to use
4067 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004068 MVT EVT = VT.getVectorElementType();
4069 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004070 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4071 }
4072
Gabor Greif1c80d112008-08-28 21:40:38 +00004073 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004074 if (V1IsUndef)
4075 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004076 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004077 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004078 if (!isMMX)
4079 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004080 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004081
Gabor Greif1c80d112008-08-28 21:40:38 +00004082 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4083 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4084 X86::isMOVHLPSMask(PermMask.getNode()) ||
4085 X86::isMOVHPMask(PermMask.getNode()) ||
4086 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004087 return Op;
4088
Gabor Greif1c80d112008-08-28 21:40:38 +00004089 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4090 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004091 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4092
Evan Chengdea99362008-05-29 08:22:04 +00004093 if (isShift) {
4094 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004095 MVT EVT = VT.getVectorElementType();
4096 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004097 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4098 }
4099
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004100 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004101 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4102 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004103 V1IsSplat = isSplatVector(V1.getNode());
4104 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004105
4106 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004107 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4108 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4109 std::swap(V1IsSplat, V2IsSplat);
4110 std::swap(V1IsUndef, V2IsUndef);
4111 Commuted = true;
4112 }
4113
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004114 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004115 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004116 if (V2IsUndef) return V1;
4117 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4118 if (V2IsSplat) {
4119 // V2 is a splat, so the mask may be malformed. That is, it may point
4120 // to any V2 element. The instruction selectior won't like this. Get
4121 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004122 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004123 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004124 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4125 }
4126 return Op;
4127 }
4128
Gabor Greif1c80d112008-08-28 21:40:38 +00004129 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4130 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4131 X86::isUNPCKLMask(PermMask.getNode()) ||
4132 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004133 return Op;
4134
4135 if (V2IsSplat) {
4136 // Normalize mask so all entries that point to V2 points to its first
4137 // element then try to match unpck{h|l} again. If match, return a
4138 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004139 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004140 if (NewMask.getNode() != PermMask.getNode()) {
4141 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004142 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004143 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004144 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004145 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004146 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4147 }
4148 }
4149 }
4150
4151 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004152 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004153 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4154
4155 if (Commuted) {
4156 // Commute is back and try unpck* again.
4157 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004158 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4159 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4160 X86::isUNPCKLMask(PermMask.getNode()) ||
4161 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004162 return Op;
4163 }
4164
Evan Chengbf8b2c52008-04-05 00:30:36 +00004165 // Try PSHUF* first, then SHUFP*.
4166 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4167 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004168 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004169 if (V2.getOpcode() != ISD::UNDEF)
4170 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4171 DAG.getNode(ISD::UNDEF, VT), PermMask);
4172 return Op;
4173 }
4174
4175 if (!isMMX) {
4176 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004177 (X86::isPSHUFDMask(PermMask.getNode()) ||
4178 X86::isPSHUFHWMask(PermMask.getNode()) ||
4179 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004180 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004181 if (VT == MVT::v4f32) {
4182 RVT = MVT::v4i32;
4183 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4184 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4185 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4186 } else if (V2.getOpcode() != ISD::UNDEF)
4187 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4188 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4189 if (RVT != VT)
4190 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004191 return Op;
4192 }
4193
Evan Chengbf8b2c52008-04-05 00:30:36 +00004194 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004195 if (X86::isSHUFPMask(PermMask.getNode()) ||
4196 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004197 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004198 }
4199
Evan Cheng75184a92007-12-11 01:46:18 +00004200 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4201 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004202 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004203 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004204 return NewOp;
4205 }
4206
Evan Chengf50554e2008-07-22 21:13:36 +00004207 // Handle all 4 wide cases with a number of shuffles except for MMX.
4208 if (NumElems == 4 && !isMMX)
4209 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004210
Dan Gohman8181bd12008-07-27 21:46:04 +00004211 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004212}
4213
Dan Gohman8181bd12008-07-27 21:46:04 +00004214SDValue
4215X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004216 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004217 MVT VT = Op.getValueType();
4218 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004219 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004220 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004221 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004222 DAG.getValueType(VT));
4223 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004224 } else if (VT.getSizeInBits() == 16) {
Evan Chengf9393b32009-01-02 05:29:08 +00004225 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4226 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4227 if (Idx == 0)
4228 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4229 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4230 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32,
4231 Op.getOperand(0)),
4232 Op.getOperand(1)));
Dan Gohman8181bd12008-07-27 21:46:04 +00004233 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004234 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004235 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004236 DAG.getValueType(VT));
4237 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004238 } else if (VT == MVT::f32) {
4239 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4240 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004241 // result has a single use which is a store or a bitcast to i32. And in
4242 // the case of a store, it's not worth it if the index is a constant 0,
4243 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004244 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004245 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004246 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004247 if ((User->getOpcode() != ISD::STORE ||
4248 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4249 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004250 (User->getOpcode() != ISD::BIT_CONVERT ||
4251 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004252 return SDValue();
4253 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004254 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4255 Op.getOperand(1));
4256 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004257 } else if (VT == MVT::i32) {
4258 // ExtractPS works with constant index.
4259 if (isa<ConstantSDNode>(Op.getOperand(1)))
4260 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004261 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004262 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004263}
4264
4265
Dan Gohman8181bd12008-07-27 21:46:04 +00004266SDValue
4267X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004268 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004269 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004270
Evan Cheng6c249332008-03-24 21:52:23 +00004271 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004272 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004273 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004274 return Res;
4275 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004276
Duncan Sands92c43912008-06-06 12:08:01 +00004277 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004278 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004279 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004280 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004281 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004282 if (Idx == 0)
4283 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4284 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4285 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4286 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004287 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004288 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004289 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004290 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004291 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004292 DAG.getValueType(VT));
4293 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004294 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004295 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004296 if (Idx == 0)
4297 return Op;
4298 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004299 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004300 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004301 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004302 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004303 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004304 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004305 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004306 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004307 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004308 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004309 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004310 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004311 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004312 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4313 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4314 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004315 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004316 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004317 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4318 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4319 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004320 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004321 if (Idx == 0)
4322 return Op;
4323
4324 // UNPCKHPD the element to the lowest double word, then movsd.
4325 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4326 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004327 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004328 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004329 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004330 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004331 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004332 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004333 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004334 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004335 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4336 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4337 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004338 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004339 }
4340
Dan Gohman8181bd12008-07-27 21:46:04 +00004341 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004342}
4343
Dan Gohman8181bd12008-07-27 21:46:04 +00004344SDValue
4345X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004346 MVT VT = Op.getValueType();
4347 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004348
Dan Gohman8181bd12008-07-27 21:46:04 +00004349 SDValue N0 = Op.getOperand(0);
4350 SDValue N1 = Op.getOperand(1);
4351 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004352
Dan Gohman5a7af042008-08-14 22:53:18 +00004353 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4354 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004355 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004356 : X86ISD::PINSRW;
4357 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4358 // argument.
4359 if (N1.getValueType() != MVT::i32)
4360 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4361 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004362 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begemand77e59e2008-02-11 04:19:36 +00004363 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004364 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004365 // Bits [7:6] of the constant are the source select. This will always be
4366 // zero here. The DAG Combiner may combine an extract_elt index into these
4367 // bits. For example (insert (extract, 3), 2) could be matched by putting
4368 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4369 // Bits [5:4] of the constant are the destination select. This is the
4370 // value of the incoming immediate.
4371 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4372 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004373 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begemand77e59e2008-02-11 04:19:36 +00004374 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004375 } else if (EVT == MVT::i32) {
4376 // InsertPS works with constant index.
4377 if (isa<ConstantSDNode>(N2))
4378 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004379 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004380 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004381}
4382
Dan Gohman8181bd12008-07-27 21:46:04 +00004383SDValue
4384X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004385 MVT VT = Op.getValueType();
4386 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004387
4388 if (Subtarget->hasSSE41())
4389 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4390
Evan Chenge12a7eb2007-12-12 07:55:34 +00004391 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004392 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004393
Dan Gohman8181bd12008-07-27 21:46:04 +00004394 SDValue N0 = Op.getOperand(0);
4395 SDValue N1 = Op.getOperand(1);
4396 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004397
Duncan Sands92c43912008-06-06 12:08:01 +00004398 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004399 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4400 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004401 if (N1.getValueType() != MVT::i32)
4402 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4403 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004404 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004405 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004406 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004407 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408}
4409
Dan Gohman8181bd12008-07-27 21:46:04 +00004410SDValue
4411X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004412 if (Op.getValueType() == MVT::v2f32)
4413 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4414 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4415 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4416 Op.getOperand(0))));
4417
Dan Gohman8181bd12008-07-27 21:46:04 +00004418 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004419 MVT VT = MVT::v2i32;
4420 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004421 default: break;
4422 case MVT::v16i8:
4423 case MVT::v8i16:
4424 VT = MVT::v4i32;
4425 break;
4426 }
4427 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4428 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004429}
4430
Bill Wendlingfef06052008-09-16 21:48:12 +00004431// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4432// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4433// one of the above mentioned nodes. It has to be wrapped because otherwise
4434// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4435// be used to form addressing mode. These wrapped nodes will be selected
4436// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004437SDValue
4438X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004439 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004440 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004441 getPointerTy(),
4442 CP->getAlignment());
4443 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4444 // With PIC, the address is actually $g + Offset.
4445 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4446 !Subtarget->isPICStyleRIPRel()) {
4447 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4448 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4449 Result);
4450 }
4451
4452 return Result;
4453}
4454
Dan Gohman8181bd12008-07-27 21:46:04 +00004455SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004456X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
Dan Gohman36322c72008-10-18 02:06:02 +00004457 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00004458 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00004459 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4460 bool ExtraLoadRequired =
4461 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4462
4463 // Create the TargetGlobalAddress node, folding in the constant
4464 // offset if it is legal.
4465 SDValue Result;
Dan Gohman3d5257c2008-10-21 03:38:42 +00004466 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +00004467 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4468 Offset = 0;
4469 } else
4470 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004471 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00004472
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004473 // With PIC, the address is actually $g + Offset.
Dan Gohman36322c72008-10-18 02:06:02 +00004474 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004475 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4476 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4477 Result);
4478 }
4479
4480 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4481 // load the value at address GV, not the value of GV itself. This means that
4482 // the GlobalAddress must be in the base or index register of the address, not
4483 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4484 // The same applies for external symbols during PIC codegen
Dan Gohman36322c72008-10-18 02:06:02 +00004485 if (ExtraLoadRequired)
Dan Gohman12a9c082008-02-06 22:27:42 +00004486 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004487 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004488
Dan Gohman36322c72008-10-18 02:06:02 +00004489 // If there was a non-zero offset that we didn't fold, create an explicit
4490 // addition for it.
4491 if (Offset != 0)
4492 Result = DAG.getNode(ISD::ADD, getPointerTy(), Result,
4493 DAG.getConstant(Offset, getPointerTy()));
4494
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004495 return Result;
4496}
4497
Evan Cheng7f250d62008-09-24 00:05:32 +00004498SDValue
4499X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4500 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00004501 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
4502 return LowerGlobalAddress(GV, Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00004503}
4504
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004505// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004506static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004507LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004508 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004509 SDValue InFlag;
4510 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004511 DAG.getNode(X86ISD::GlobalBaseReg,
4512 PtrVT), InFlag);
4513 InFlag = Chain.getValue(1);
4514
4515 // emit leal symbol@TLSGD(,%ebx,1), %eax
4516 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004517 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004518 GA->getValueType(0),
4519 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004520 SDValue Ops[] = { Chain, TGA, InFlag };
4521 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004522 InFlag = Result.getValue(2);
4523 Chain = Result.getValue(1);
4524
4525 // call ___tls_get_addr. This function receives its argument in
4526 // the register EAX.
4527 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4528 InFlag = Chain.getValue(1);
4529
4530 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004531 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004532 DAG.getTargetExternalSymbol("___tls_get_addr",
4533 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004534 DAG.getRegister(X86::EAX, PtrVT),
4535 DAG.getRegister(X86::EBX, PtrVT),
4536 InFlag };
4537 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4538 InFlag = Chain.getValue(1);
4539
4540 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4541}
4542
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004543// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004544static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004545LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004546 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004547 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004548
4549 // emit leaq symbol@TLSGD(%rip), %rdi
4550 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004551 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004552 GA->getValueType(0),
4553 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004554 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4555 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004556 Chain = Result.getValue(1);
4557 InFlag = Result.getValue(2);
4558
aslb204cd52008-08-16 12:58:29 +00004559 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004560 // the register RDI.
4561 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4562 InFlag = Chain.getValue(1);
4563
4564 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004565 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004566 DAG.getTargetExternalSymbol("__tls_get_addr",
4567 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004568 DAG.getRegister(X86::RDI, PtrVT),
4569 InFlag };
4570 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4571 InFlag = Chain.getValue(1);
4572
4573 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4574}
4575
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004576// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4577// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004578static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004579 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004580 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004581 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004582 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4583 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004584 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585 GA->getValueType(0),
4586 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004587 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004588
4589 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004590 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004591 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004592
4593 // The address of the thread local variable is the add of the thread
4594 // pointer with the offset of the variable.
4595 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4596}
4597
Dan Gohman8181bd12008-07-27 21:46:04 +00004598SDValue
4599X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004600 // TODO: implement the "local dynamic" model
4601 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004602 assert(Subtarget->isTargetELF() &&
4603 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004604 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4605 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4606 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004607 if (Subtarget->is64Bit()) {
4608 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4609 } else {
4610 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4611 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4612 else
4613 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4614 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004615}
4616
Dan Gohman8181bd12008-07-27 21:46:04 +00004617SDValue
4618X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004619 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4620 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004621 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4622 // With PIC, the address is actually $g + Offset.
4623 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4624 !Subtarget->isPICStyleRIPRel()) {
4625 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4626 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4627 Result);
4628 }
4629
4630 return Result;
4631}
4632
Dan Gohman8181bd12008-07-27 21:46:04 +00004633SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004634 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004635 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004636 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4637 // With PIC, the address is actually $g + Offset.
4638 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4639 !Subtarget->isPICStyleRIPRel()) {
4640 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4641 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4642 Result);
4643 }
4644
4645 return Result;
4646}
4647
Chris Lattner62814a32007-10-17 06:02:13 +00004648/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4649/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004650SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004651 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004652 MVT VT = Op.getValueType();
4653 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004654 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004655 SDValue ShOpLo = Op.getOperand(0);
4656 SDValue ShOpHi = Op.getOperand(1);
4657 SDValue ShAmt = Op.getOperand(2);
4658 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004659 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4660 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004661
Dan Gohman8181bd12008-07-27 21:46:04 +00004662 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004663 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004664 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4665 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004666 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004667 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4668 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004669 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004670
Dan Gohman8181bd12008-07-27 21:46:04 +00004671 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004672 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004673 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004674 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004675
Dan Gohman8181bd12008-07-27 21:46:04 +00004676 SDValue Hi, Lo;
4677 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4678 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4679 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004680
Chris Lattner62814a32007-10-17 06:02:13 +00004681 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004682 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4683 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004684 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004685 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4686 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004687 }
4688
Dan Gohman8181bd12008-07-27 21:46:04 +00004689 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004690 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004691}
4692
Dan Gohman8181bd12008-07-27 21:46:04 +00004693SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004694 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004695 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004696 "Unknown SINT_TO_FP to lower!");
4697
4698 // These are really Legal; caller falls through into that case.
4699 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004700 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004701 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4702 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004703 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004704
Duncan Sands92c43912008-06-06 12:08:01 +00004705 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004706 MachineFunction &MF = DAG.getMachineFunction();
4707 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004708 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4709 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004710 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004711 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004712
4713 // Build the FILD
4714 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004715 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004716 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004717 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4718 else
4719 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004720 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004721 Ops.push_back(Chain);
4722 Ops.push_back(StackSlot);
4723 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004724 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004725 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004726
Dale Johannesen2fc20782007-09-14 22:26:36 +00004727 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004728 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004729 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004730
4731 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4732 // shouldn't be necessary except that RFP cannot be live across
4733 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4734 MachineFunction &MF = DAG.getMachineFunction();
4735 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004736 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004737 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004738 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004739 Ops.push_back(Chain);
4740 Ops.push_back(Result);
4741 Ops.push_back(StackSlot);
4742 Ops.push_back(DAG.getValueType(Op.getValueType()));
4743 Ops.push_back(InFlag);
4744 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004745 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004746 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004747 }
4748
4749 return Result;
4750}
4751
Bill Wendling14a30ef2009-01-17 03:56:04 +00004752// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4753SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4754 // This algorithm is not obvious. Here it is in C code, more or less:
4755 /*
4756 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4757 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4758 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesenfb019af2008-10-21 23:07:49 +00004759
Bill Wendling14a30ef2009-01-17 03:56:04 +00004760 // Copy ints to xmm registers.
4761 __m128i xh = _mm_cvtsi32_si128( hi );
4762 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004763
Bill Wendling14a30ef2009-01-17 03:56:04 +00004764 // Combine into low half of a single xmm register.
4765 __m128i x = _mm_unpacklo_epi32( xh, xl );
4766 __m128d d;
4767 double sd;
Dale Johannesenfb019af2008-10-21 23:07:49 +00004768
Bill Wendling14a30ef2009-01-17 03:56:04 +00004769 // Merge in appropriate exponents to give the integer bits the right
4770 // magnitude.
4771 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004772
Bill Wendling14a30ef2009-01-17 03:56:04 +00004773 // Subtract away the biases to deal with the IEEE-754 double precision
4774 // implicit 1.
4775 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004776
Bill Wendling14a30ef2009-01-17 03:56:04 +00004777 // All conversions up to here are exact. The correctly rounded result is
4778 // calculated using the current rounding mode using the following
4779 // horizontal add.
4780 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4781 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4782 // store doesn't really need to be here (except
4783 // maybe to zero the other double)
4784 return sd;
4785 }
4786 */
Dale Johannesenfb019af2008-10-21 23:07:49 +00004787
Dale Johannesena359b8b2008-10-21 20:50:01 +00004788 // Build some magic constants.
Bill Wendling14a30ef2009-01-17 03:56:04 +00004789 std::vector<Constant*> CV0;
Dale Johannesena359b8b2008-10-21 20:50:01 +00004790 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4791 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4792 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4793 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4794 Constant *C0 = ConstantVector::get(CV0);
4795 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 4);
4796
Bill Wendling14a30ef2009-01-17 03:56:04 +00004797 std::vector<Constant*> CV1;
Dale Johannesena359b8b2008-10-21 20:50:01 +00004798 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4799 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4800 Constant *C1 = ConstantVector::get(CV1);
4801 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 4);
4802
4803 SmallVector<SDValue, 4> MaskVec;
4804 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
4805 MaskVec.push_back(DAG.getConstant(4, MVT::i32));
4806 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
4807 MaskVec.push_back(DAG.getConstant(5, MVT::i32));
4808 SDValue UnpcklMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, &MaskVec[0],
4809 MaskVec.size());
4810 SmallVector<SDValue, 4> MaskVec2;
Duncan Sandsca872ca2008-10-22 11:24:12 +00004811 MaskVec2.push_back(DAG.getConstant(1, MVT::i32));
4812 MaskVec2.push_back(DAG.getConstant(0, MVT::i32));
4813 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec2[0],
Dale Johannesena359b8b2008-10-21 20:50:01 +00004814 MaskVec2.size());
4815
4816 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004817 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4818 Op.getOperand(0),
4819 DAG.getIntPtrConstant(1)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004820 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004821 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4822 Op.getOperand(0),
4823 DAG.getIntPtrConstant(0)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004824 SDValue Unpck1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4825 XR1, XR2, UnpcklMask);
4826 SDValue CLod0 = DAG.getLoad(MVT::v4i32, DAG.getEntryNode(), CPIdx0,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004827 PseudoSourceValue::getConstantPool(), 0,
4828 false, 16);
Dale Johannesena359b8b2008-10-21 20:50:01 +00004829 SDValue Unpck2 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004830 Unpck1, CLod0, UnpcklMask);
Dale Johannesena359b8b2008-10-21 20:50:01 +00004831 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Unpck2);
4832 SDValue CLod1 = DAG.getLoad(MVT::v2f64, CLod0.getValue(1), CPIdx1,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004833 PseudoSourceValue::getConstantPool(), 0,
4834 false, 16);
Dale Johannesena359b8b2008-10-21 20:50:01 +00004835 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::v2f64, XR2F, CLod1);
Bill Wendling14a30ef2009-01-17 03:56:04 +00004836
Dale Johannesena359b8b2008-10-21 20:50:01 +00004837 // Add the halves; easiest way is to swap them into another reg first.
4838 SDValue Shuf = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2f64,
4839 Sub, Sub, ShufMask);
4840 SDValue Add = DAG.getNode(ISD::FADD, MVT::v2f64, Shuf, Sub);
4841 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64, Add,
4842 DAG.getIntPtrConstant(0));
4843}
4844
Bill Wendling14a30ef2009-01-17 03:56:04 +00004845// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4846SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
4847 // FP constant to bias correct the final result.
4848 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4849 MVT::f64);
4850
4851 // Load the 32-bit value into an XMM register.
4852 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
4853 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4854 Op.getOperand(0),
4855 DAG.getIntPtrConstant(0)));
4856
4857 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64,
4858 DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Load),
4859 DAG.getIntPtrConstant(0));
4860
4861 // Or the load with the bias.
4862 SDValue Or = DAG.getNode(ISD::OR, MVT::v2i64,
4863 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64,
4864 DAG.getNode(ISD::SCALAR_TO_VECTOR,
4865 MVT::v2f64, Bias)),
4866 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64,
4867 DAG.getNode(ISD::SCALAR_TO_VECTOR,
4868 MVT::v2f64, Load)));
4869 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64,
4870 DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Or),
4871 DAG.getIntPtrConstant(0));
4872
4873 // Subtract the bias.
4874 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Or, Bias);
4875
4876 // Handle final rounding.
4877 return DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub,
4878 DAG.getIntPtrConstant(0));
4879}
4880
4881SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4882 MVT SrcVT = Op.getOperand(0).getValueType();
4883
4884 if (SrcVT == MVT::i64) {
4885 // We only handle SSE2 f64 target here; caller can handle the rest.
4886 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4887 return SDValue();
4888
4889 return LowerUINT_TO_FP_i64(Op, DAG);
4890 } else if (SrcVT == MVT::i32) {
4891 // We only handle SSE2 f32 target here; caller can handle the rest.
4892 if (Op.getValueType() != MVT::f32 || !X86ScalarSSEf32)
4893 return SDValue();
4894
4895 return LowerUINT_TO_FP_i32(Op, DAG);
4896 }
4897
4898 assert(0 && "Unknown UINT_TO_FP to lower!");
4899 return SDValue();
4900}
4901
Dan Gohman8181bd12008-07-27 21:46:04 +00004902std::pair<SDValue,SDValue> X86TargetLowering::
4903FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004904 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4905 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004906 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004907
Dale Johannesen2fc20782007-09-14 22:26:36 +00004908 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004909 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004910 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004911 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004912 if (Subtarget->is64Bit() &&
4913 Op.getValueType() == MVT::i64 &&
4914 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004915 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004916
Evan Cheng05441e62007-10-15 20:11:21 +00004917 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4918 // stack slot.
4919 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004920 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004921 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004922 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004923 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004924 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004925 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4926 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4927 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4928 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004929 }
4930
Dan Gohman8181bd12008-07-27 21:46:04 +00004931 SDValue Chain = DAG.getEntryNode();
4932 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004933 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004934 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004935 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004936 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004937 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004938 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004939 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4940 };
4941 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4942 Chain = Value.getValue(1);
4943 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4944 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4945 }
4946
4947 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004948 SDValue Ops[] = { Chain, Value, StackSlot };
4949 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004950
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004951 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004952}
4953
Dan Gohman8181bd12008-07-27 21:46:04 +00004954SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4955 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4956 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004957 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004958
4959 // Load the result.
4960 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4961}
4962
Dan Gohman8181bd12008-07-27 21:46:04 +00004963SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004964 MVT VT = Op.getValueType();
4965 MVT EltVT = VT;
4966 if (VT.isVector())
4967 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004968 std::vector<Constant*> CV;
4969 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004970 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004971 CV.push_back(C);
4972 CV.push_back(C);
4973 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004974 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004975 CV.push_back(C);
4976 CV.push_back(C);
4977 CV.push_back(C);
4978 CV.push_back(C);
4979 }
Dan Gohman11821702007-07-27 17:16:43 +00004980 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004981 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4982 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004983 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004984 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004985 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4986}
4987
Dan Gohman8181bd12008-07-27 21:46:04 +00004988SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004989 MVT VT = Op.getValueType();
4990 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004991 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004992 if (VT.isVector()) {
4993 EltVT = VT.getVectorElementType();
4994 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004995 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004996 std::vector<Constant*> CV;
4997 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004998 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004999 CV.push_back(C);
5000 CV.push_back(C);
5001 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005002 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005003 CV.push_back(C);
5004 CV.push_back(C);
5005 CV.push_back(C);
5006 CV.push_back(C);
5007 }
Dan Gohman11821702007-07-27 17:16:43 +00005008 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005009 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
5010 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005011 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005012 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00005013 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00005014 return DAG.getNode(ISD::BIT_CONVERT, VT,
5015 DAG.getNode(ISD::XOR, MVT::v2i64,
5016 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
5017 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
5018 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00005019 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
5020 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005021}
5022
Dan Gohman8181bd12008-07-27 21:46:04 +00005023SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
5024 SDValue Op0 = Op.getOperand(0);
5025 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00005026 MVT VT = Op.getValueType();
5027 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005028
5029 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005030 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005031 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
5032 SrcVT = VT;
5033 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005034 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005035 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00005036 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005037 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005038 }
5039
5040 // At this point the operands and the result should have the same
5041 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005042
5043 // First get the sign bit of second operand.
5044 std::vector<Constant*> CV;
5045 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005046 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
5047 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005048 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005049 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
5050 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5051 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5052 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005053 }
Dan Gohman11821702007-07-27 17:16:43 +00005054 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005055 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
5056 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005057 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005058 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00005059 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005060
5061 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005062 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005063 // Op0 is MVT::f32, Op1 is MVT::f64.
5064 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
5065 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
5066 DAG.getConstant(32, MVT::i32));
5067 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
5068 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00005069 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005070 }
5071
5072 // Clear first operand sign bit.
5073 CV.clear();
5074 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005075 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
5076 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005077 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005078 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
5079 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5080 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5081 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005082 }
Dan Gohman11821702007-07-27 17:16:43 +00005083 C = ConstantVector::get(CV);
5084 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00005085 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005086 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005087 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00005088 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005089
5090 // Or the value with the sign bit.
5091 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
5092}
5093
Dan Gohman8181bd12008-07-27 21:46:04 +00005094SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00005095 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00005096 SDValue Op0 = Op.getOperand(0);
5097 SDValue Op1 = Op.getOperand(1);
Chris Lattner77a62312008-12-25 05:34:37 +00005098 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5099
5100 // Lower (X & (1 << N)) == 0 to BT.
5101 // Lower ((X >>u N) & 1) != 0 to BT.
5102 // Lower ((X >>s N) & 1) != 0 to BT.
Dan Gohman13dd9522009-01-13 23:25:30 +00005103 if (Op0.getOpcode() == ISD::AND &&
5104 Op0.hasOneUse() &&
5105 Op1.getOpcode() == ISD::Constant &&
Chris Lattner77a62312008-12-25 05:34:37 +00005106 Op0.getOperand(1).getOpcode() == ISD::Constant &&
5107 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
5108 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5109 ConstantSDNode *CmpRHS = cast<ConstantSDNode>(Op1);
5110 SDValue AndLHS = Op0.getOperand(0);
5111 if (CmpRHS->getZExtValue() == 0 && AndRHS->getZExtValue() == 1 &&
5112 AndLHS.getOpcode() == ISD::SRL) {
5113 SDValue LHS = AndLHS.getOperand(0);
5114 SDValue RHS = AndLHS.getOperand(1);
Evan Cheng950aac02007-09-25 01:57:46 +00005115
Chris Lattner77a62312008-12-25 05:34:37 +00005116 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5117 // instruction. Since the shift amount is in-range-or-undefined, we know
5118 // that doing a bittest on the i16 value is ok. We extend to i32 because
5119 // the encoding for the i16 version is larger than the i32 version.
5120 if (LHS.getValueType() == MVT::i8)
5121 LHS = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, LHS);
5122
5123 // If the operand types disagree, extend the shift amount to match. Since
5124 // BT ignores high bits (like shifts) we can use anyextend.
5125 if (LHS.getValueType() != RHS.getValueType())
5126 RHS = DAG.getNode(ISD::ANY_EXTEND, LHS.getValueType(), RHS);
5127
5128 SDValue BT = DAG.getNode(X86ISD::BT, MVT::i32, LHS, RHS);
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005129 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Chris Lattner77a62312008-12-25 05:34:37 +00005130 return DAG.getNode(X86ISD::SETCC, MVT::i8,
5131 DAG.getConstant(Cond, MVT::i8), BT);
5132 }
5133 }
5134
5135 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5136 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Chris Lattner60435922008-12-24 00:11:37 +00005137
Chris Lattner77a62312008-12-25 05:34:37 +00005138 SDValue Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Chris Lattner60435922008-12-24 00:11:37 +00005139 return DAG.getNode(X86ISD::SETCC, MVT::i8,
5140 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005141}
5142
Dan Gohman8181bd12008-07-27 21:46:04 +00005143SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5144 SDValue Cond;
5145 SDValue Op0 = Op.getOperand(0);
5146 SDValue Op1 = Op.getOperand(1);
5147 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00005148 MVT VT = Op.getValueType();
5149 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5150 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5151
5152 if (isFP) {
5153 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00005154 MVT VT0 = Op0.getValueType();
5155 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5156 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00005157 bool Swap = false;
5158
5159 switch (SetCCOpcode) {
5160 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005161 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00005162 case ISD::SETEQ: SSECC = 0; break;
5163 case ISD::SETOGT:
5164 case ISD::SETGT: Swap = true; // Fallthrough
5165 case ISD::SETLT:
5166 case ISD::SETOLT: SSECC = 1; break;
5167 case ISD::SETOGE:
5168 case ISD::SETGE: Swap = true; // Fallthrough
5169 case ISD::SETLE:
5170 case ISD::SETOLE: SSECC = 2; break;
5171 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005172 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00005173 case ISD::SETNE: SSECC = 4; break;
5174 case ISD::SETULE: Swap = true;
5175 case ISD::SETUGE: SSECC = 5; break;
5176 case ISD::SETULT: Swap = true;
5177 case ISD::SETUGT: SSECC = 6; break;
5178 case ISD::SETO: SSECC = 7; break;
5179 }
5180 if (Swap)
5181 std::swap(Op0, Op1);
5182
Nate Begeman6357f9d2008-07-25 19:05:58 +00005183 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00005184 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00005185 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005186 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005187 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5188 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5189 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
5190 }
5191 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005192 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005193 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5194 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5195 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
5196 }
5197 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00005198 }
5199 // Handle all other FP comparisons here.
5200 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
5201 }
5202
5203 // We are handling one of the integer comparisons here. Since SSE only has
5204 // GT and EQ comparisons for integer, swapping operands and multiple
5205 // operations may be required for some comparisons.
5206 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5207 bool Swap = false, Invert = false, FlipSigns = false;
5208
5209 switch (VT.getSimpleVT()) {
5210 default: break;
5211 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5212 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5213 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5214 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5215 }
5216
5217 switch (SetCCOpcode) {
5218 default: break;
5219 case ISD::SETNE: Invert = true;
5220 case ISD::SETEQ: Opc = EQOpc; break;
5221 case ISD::SETLT: Swap = true;
5222 case ISD::SETGT: Opc = GTOpc; break;
5223 case ISD::SETGE: Swap = true;
5224 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5225 case ISD::SETULT: Swap = true;
5226 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5227 case ISD::SETUGE: Swap = true;
5228 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5229 }
5230 if (Swap)
5231 std::swap(Op0, Op1);
5232
5233 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5234 // bits of the inputs before performing those operations.
5235 if (FlipSigns) {
5236 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005237 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
5238 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
5239 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005240 SignBits.size());
5241 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
5242 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
5243 }
5244
Dan Gohman8181bd12008-07-27 21:46:04 +00005245 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005246
5247 // If the logical-not of the result is required, perform that now.
5248 if (Invert) {
5249 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005250 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
5251 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
5252 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005253 NegOnes.size());
5254 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
5255 }
5256 return Result;
5257}
Evan Cheng950aac02007-09-25 01:57:46 +00005258
Evan Chengd580f022008-12-03 08:38:43 +00005259// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
5260static bool isX86LogicalCmp(unsigned Opc) {
5261 return Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI;
5262}
5263
Dan Gohman8181bd12008-07-27 21:46:04 +00005264SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005265 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005266 SDValue Cond = Op.getOperand(0);
5267 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005268
5269 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005270 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005271
Evan Cheng50d37ab2007-10-08 22:16:29 +00005272 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5273 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005274 if (Cond.getOpcode() == X86ISD::SETCC) {
5275 CC = Cond.getOperand(0);
5276
Dan Gohman8181bd12008-07-27 21:46:04 +00005277 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005278 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005279 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005280
Evan Cheng50d37ab2007-10-08 22:16:29 +00005281 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005282 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005283 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005284 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005285
Evan Chengd580f022008-12-03 08:38:43 +00005286 if (isX86LogicalCmp(Opc) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005287 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005288 addTest = false;
5289 }
5290 }
5291
5292 if (addTest) {
5293 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00005294 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005295 }
5296
Duncan Sands92c43912008-06-06 12:08:01 +00005297 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005298 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005299 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005300 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5301 // condition is true.
5302 Ops.push_back(Op.getOperand(2));
5303 Ops.push_back(Op.getOperand(1));
5304 Ops.push_back(CC);
5305 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00005306 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005307}
5308
Evan Chengd580f022008-12-03 08:38:43 +00005309// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5310// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5311// from the AND / OR.
5312static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5313 Opc = Op.getOpcode();
5314 if (Opc != ISD::OR && Opc != ISD::AND)
5315 return false;
5316 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5317 Op.getOperand(0).hasOneUse() &&
5318 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5319 Op.getOperand(1).hasOneUse());
5320}
5321
Dan Gohman8181bd12008-07-27 21:46:04 +00005322SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005323 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005324 SDValue Chain = Op.getOperand(0);
5325 SDValue Cond = Op.getOperand(1);
5326 SDValue Dest = Op.getOperand(2);
5327 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005328
5329 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005330 Cond = LowerSETCC(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005331#if 0
5332 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingf5399032008-12-12 21:15:41 +00005333 else if (Cond.getOpcode() == X86ISD::ADD ||
5334 Cond.getOpcode() == X86ISD::SUB ||
5335 Cond.getOpcode() == X86ISD::SMUL ||
5336 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling7e04be62008-12-09 22:08:41 +00005337 Cond = LowerXALUO(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005338#endif
5339
Evan Cheng50d37ab2007-10-08 22:16:29 +00005340 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5341 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005342 if (Cond.getOpcode() == X86ISD::SETCC) {
5343 CC = Cond.getOperand(0);
5344
Dan Gohman8181bd12008-07-27 21:46:04 +00005345 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005346 unsigned Opc = Cmp.getOpcode();
Chris Lattner77a62312008-12-25 05:34:37 +00005347 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
5348 if (isX86LogicalCmp(Opc) || Opc == X86ISD::BT) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005349 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005350 addTest = false;
Bill Wendlingd3511522008-12-02 01:06:39 +00005351 } else {
Evan Chengd580f022008-12-03 08:38:43 +00005352 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling809e7bd2008-12-03 08:32:02 +00005353 default: break;
5354 case X86::COND_O:
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005355 case X86::COND_B:
Chris Lattner77a62312008-12-25 05:34:37 +00005356 // These can only come from an arithmetic instruction with overflow,
5357 // e.g. SADDO, UADDO.
Bill Wendling809e7bd2008-12-03 08:32:02 +00005358 Cond = Cond.getNode()->getOperand(1);
5359 addTest = false;
5360 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00005361 }
Evan Cheng950aac02007-09-25 01:57:46 +00005362 }
Evan Chengd580f022008-12-03 08:38:43 +00005363 } else {
5364 unsigned CondOpc;
5365 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5366 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5367 unsigned Opc = Cmp.getOpcode();
5368 if (CondOpc == ISD::OR) {
5369 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5370 // two branches instead of an explicit OR instruction with a
5371 // separate test.
5372 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5373 isX86LogicalCmp(Opc)) {
5374 CC = Cond.getOperand(0).getOperand(0);
5375 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5376 Chain, Dest, CC, Cmp);
5377 CC = Cond.getOperand(1).getOperand(0);
5378 Cond = Cmp;
5379 addTest = false;
5380 }
5381 } else { // ISD::AND
5382 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5383 // two branches instead of an explicit AND instruction with a
5384 // separate test. However, we only do this if this block doesn't
5385 // have a fall-through edge, because this requires an explicit
5386 // jmp when the condition is false.
5387 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5388 isX86LogicalCmp(Opc) &&
5389 Op.getNode()->hasOneUse()) {
5390 X86::CondCode CCode =
5391 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5392 CCode = X86::GetOppositeBranchCondition(CCode);
5393 CC = DAG.getConstant(CCode, MVT::i8);
5394 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5395 // Look for an unconditional branch following this conditional branch.
5396 // We need this because we need to reverse the successors in order
5397 // to implement FCMP_OEQ.
5398 if (User.getOpcode() == ISD::BR) {
5399 SDValue FalseBB = User.getOperand(1);
5400 SDValue NewBR =
5401 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5402 assert(NewBR == User);
5403 Dest = FalseBB;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005404
Evan Chengd580f022008-12-03 08:38:43 +00005405 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5406 Chain, Dest, CC, Cmp);
5407 X86::CondCode CCode =
5408 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5409 CCode = X86::GetOppositeBranchCondition(CCode);
5410 CC = DAG.getConstant(CCode, MVT::i8);
5411 Cond = Cmp;
5412 addTest = false;
5413 }
5414 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005415 }
5416 }
Evan Cheng950aac02007-09-25 01:57:46 +00005417 }
5418
5419 if (addTest) {
5420 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005421 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005422 }
Evan Cheng621216e2007-09-29 00:00:36 +00005423 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005424 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005425}
5426
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005427
5428// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5429// Calls to _alloca is needed to probe the stack when allocating more than 4k
5430// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5431// that the guard pages used by the OS virtual memory manager are allocated in
5432// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005433SDValue
5434X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005435 SelectionDAG &DAG) {
5436 assert(Subtarget->isTargetCygMing() &&
5437 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005438
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005439 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005440 SDValue Chain = Op.getOperand(0);
5441 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005442 // FIXME: Ensure alignment here
5443
Dan Gohman8181bd12008-07-27 21:46:04 +00005444 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005445
Duncan Sands92c43912008-06-06 12:08:01 +00005446 MVT IntPtr = getPointerTy();
5447 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005448
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005449 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005450
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005451 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5452 Flag = Chain.getValue(1);
5453
5454 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005455 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005456 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005457 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005458 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005459 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005460 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005461 Flag = Chain.getValue(1);
5462
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005463 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005464 DAG.getIntPtrConstant(0, true),
5465 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005466 Flag);
5467
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005468 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005469
Dan Gohman8181bd12008-07-27 21:46:04 +00005470 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005471 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005472}
5473
Dan Gohman8181bd12008-07-27 21:46:04 +00005474SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005475X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005476 SDValue Chain,
5477 SDValue Dst, SDValue Src,
5478 SDValue Size, unsigned Align,
5479 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005480 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005481 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005482
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005483 // If not DWORD aligned or size is more than the threshold, call the library.
5484 // The libc version is likely to be faster for these cases. It can use the
5485 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005486 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005487 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005488 ConstantSize->getZExtValue() >
5489 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005490 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005491
5492 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005493 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005494
Bill Wendling4b2e3782008-10-01 00:59:58 +00005495 if (const char *bzeroEntry = V &&
5496 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5497 MVT IntPtr = getPointerTy();
5498 const Type *IntPtrTy = TD->getIntPtrType();
5499 TargetLowering::ArgListTy Args;
5500 TargetLowering::ArgListEntry Entry;
5501 Entry.Node = Dst;
5502 Entry.Ty = IntPtrTy;
5503 Args.push_back(Entry);
5504 Entry.Node = Size;
5505 Args.push_back(Entry);
5506 std::pair<SDValue,SDValue> CallResult =
5507 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5508 CallingConv::C, false,
5509 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG);
5510 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005511 }
5512
Dan Gohmane8b391e2008-04-12 04:36:06 +00005513 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005514 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005515 }
5516
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005517 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005518 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005519 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005520 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005521 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005522 unsigned BytesLeft = 0;
5523 bool TwoRepStos = false;
5524 if (ValC) {
5525 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005526 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005527
5528 // If the value is a constant, then we can potentially use larger sets.
5529 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005530 case 2: // WORD aligned
5531 AVT = MVT::i16;
5532 ValReg = X86::AX;
5533 Val = (Val << 8) | Val;
5534 break;
5535 case 0: // DWORD aligned
5536 AVT = MVT::i32;
5537 ValReg = X86::EAX;
5538 Val = (Val << 8) | Val;
5539 Val = (Val << 16) | Val;
5540 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5541 AVT = MVT::i64;
5542 ValReg = X86::RAX;
5543 Val = (Val << 32) | Val;
5544 }
5545 break;
5546 default: // Byte aligned
5547 AVT = MVT::i8;
5548 ValReg = X86::AL;
5549 Count = DAG.getIntPtrConstant(SizeVal);
5550 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005551 }
5552
Duncan Sandsec142ee2008-06-08 20:54:56 +00005553 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005554 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005555 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5556 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005557 }
5558
5559 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5560 InFlag);
5561 InFlag = Chain.getValue(1);
5562 } else {
5563 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005564 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005565 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005566 InFlag = Chain.getValue(1);
5567 }
5568
5569 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5570 Count, InFlag);
5571 InFlag = Chain.getValue(1);
5572 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005573 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005574 InFlag = Chain.getValue(1);
5575
5576 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005577 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005578 Ops.push_back(Chain);
5579 Ops.push_back(DAG.getValueType(AVT));
5580 Ops.push_back(InFlag);
5581 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5582
5583 if (TwoRepStos) {
5584 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005585 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005586 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005587 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005588 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5589 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5590 Left, InFlag);
5591 InFlag = Chain.getValue(1);
5592 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5593 Ops.clear();
5594 Ops.push_back(Chain);
5595 Ops.push_back(DAG.getValueType(MVT::i8));
5596 Ops.push_back(InFlag);
5597 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5598 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005599 // Handle the last 1 - 7 bytes.
5600 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005601 MVT AddrVT = Dst.getValueType();
5602 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005603
5604 Chain = DAG.getMemset(Chain,
5605 DAG.getNode(ISD::ADD, AddrVT, Dst,
5606 DAG.getConstant(Offset, AddrVT)),
5607 Src,
5608 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005609 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005610 }
5611
Dan Gohmane8b391e2008-04-12 04:36:06 +00005612 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005613 return Chain;
5614}
5615
Dan Gohman8181bd12008-07-27 21:46:04 +00005616SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005617X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005618 SDValue Chain, SDValue Dst, SDValue Src,
5619 SDValue Size, unsigned Align,
5620 bool AlwaysInline,
5621 const Value *DstSV, uint64_t DstSVOff,
5622 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005623 // This requires the copy size to be a constant, preferrably
5624 // within a subtarget-specific limit.
5625 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5626 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005627 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005628 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005629 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005630 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005631
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005632 /// If not DWORD aligned, call the library.
5633 if ((Align & 3) != 0)
5634 return SDValue();
5635
5636 // DWORD aligned
5637 MVT AVT = MVT::i32;
5638 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005639 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005640
Duncan Sands92c43912008-06-06 12:08:01 +00005641 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005642 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005643 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005644 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005645
Dan Gohman8181bd12008-07-27 21:46:04 +00005646 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005647 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5648 Count, InFlag);
5649 InFlag = Chain.getValue(1);
5650 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005651 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005652 InFlag = Chain.getValue(1);
5653 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005654 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005655 InFlag = Chain.getValue(1);
5656
5657 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005658 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005659 Ops.push_back(Chain);
5660 Ops.push_back(DAG.getValueType(AVT));
5661 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005662 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005663
Dan Gohman8181bd12008-07-27 21:46:04 +00005664 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005665 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005666 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005667 // Handle the last 1 - 7 bytes.
5668 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005669 MVT DstVT = Dst.getValueType();
5670 MVT SrcVT = Src.getValueType();
5671 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005672 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005673 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005674 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005675 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005676 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005677 DAG.getConstant(BytesLeft, SizeVT),
5678 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005679 DstSV, DstSVOff + Offset,
5680 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005681 }
5682
Dan Gohmane8b391e2008-04-12 04:36:06 +00005683 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005684}
5685
Dan Gohman8181bd12008-07-27 21:46:04 +00005686SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005687 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005688
5689 if (!Subtarget->is64Bit()) {
5690 // vastart just stores the address of the VarArgsFrameIndex slot into the
5691 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005692 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005693 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005694 }
5695
5696 // __va_list_tag:
5697 // gp_offset (0 - 6 * 8)
5698 // fp_offset (48 - 48 + 8 * 16)
5699 // overflow_arg_area (point to parameters coming in memory).
5700 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005701 SmallVector<SDValue, 8> MemOps;
5702 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005703 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005704 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005705 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005706 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005707 MemOps.push_back(Store);
5708
5709 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005710 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005711 Store = DAG.getStore(Op.getOperand(0),
5712 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005713 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005714 MemOps.push_back(Store);
5715
5716 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005717 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005718 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005719 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005720 MemOps.push_back(Store);
5721
5722 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005723 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005724 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005725 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005726 MemOps.push_back(Store);
5727 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5728}
5729
Dan Gohman8181bd12008-07-27 21:46:04 +00005730SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005731 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5732 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005733 SDValue Chain = Op.getOperand(0);
5734 SDValue SrcPtr = Op.getOperand(1);
5735 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005736
5737 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5738 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005739 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005740}
5741
Dan Gohman8181bd12008-07-27 21:46:04 +00005742SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005743 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005744 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005745 SDValue Chain = Op.getOperand(0);
5746 SDValue DstPtr = Op.getOperand(1);
5747 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005748 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5749 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005750
Dan Gohman840ff5c2008-04-18 20:55:41 +00005751 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5752 DAG.getIntPtrConstant(24), 8, false,
5753 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005754}
5755
Dan Gohman8181bd12008-07-27 21:46:04 +00005756SDValue
5757X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005758 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005759 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005760 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005761 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005762 case Intrinsic::x86_sse_comieq_ss:
5763 case Intrinsic::x86_sse_comilt_ss:
5764 case Intrinsic::x86_sse_comile_ss:
5765 case Intrinsic::x86_sse_comigt_ss:
5766 case Intrinsic::x86_sse_comige_ss:
5767 case Intrinsic::x86_sse_comineq_ss:
5768 case Intrinsic::x86_sse_ucomieq_ss:
5769 case Intrinsic::x86_sse_ucomilt_ss:
5770 case Intrinsic::x86_sse_ucomile_ss:
5771 case Intrinsic::x86_sse_ucomigt_ss:
5772 case Intrinsic::x86_sse_ucomige_ss:
5773 case Intrinsic::x86_sse_ucomineq_ss:
5774 case Intrinsic::x86_sse2_comieq_sd:
5775 case Intrinsic::x86_sse2_comilt_sd:
5776 case Intrinsic::x86_sse2_comile_sd:
5777 case Intrinsic::x86_sse2_comigt_sd:
5778 case Intrinsic::x86_sse2_comige_sd:
5779 case Intrinsic::x86_sse2_comineq_sd:
5780 case Intrinsic::x86_sse2_ucomieq_sd:
5781 case Intrinsic::x86_sse2_ucomilt_sd:
5782 case Intrinsic::x86_sse2_ucomile_sd:
5783 case Intrinsic::x86_sse2_ucomigt_sd:
5784 case Intrinsic::x86_sse2_ucomige_sd:
5785 case Intrinsic::x86_sse2_ucomineq_sd: {
5786 unsigned Opc = 0;
5787 ISD::CondCode CC = ISD::SETCC_INVALID;
5788 switch (IntNo) {
5789 default: break;
5790 case Intrinsic::x86_sse_comieq_ss:
5791 case Intrinsic::x86_sse2_comieq_sd:
5792 Opc = X86ISD::COMI;
5793 CC = ISD::SETEQ;
5794 break;
5795 case Intrinsic::x86_sse_comilt_ss:
5796 case Intrinsic::x86_sse2_comilt_sd:
5797 Opc = X86ISD::COMI;
5798 CC = ISD::SETLT;
5799 break;
5800 case Intrinsic::x86_sse_comile_ss:
5801 case Intrinsic::x86_sse2_comile_sd:
5802 Opc = X86ISD::COMI;
5803 CC = ISD::SETLE;
5804 break;
5805 case Intrinsic::x86_sse_comigt_ss:
5806 case Intrinsic::x86_sse2_comigt_sd:
5807 Opc = X86ISD::COMI;
5808 CC = ISD::SETGT;
5809 break;
5810 case Intrinsic::x86_sse_comige_ss:
5811 case Intrinsic::x86_sse2_comige_sd:
5812 Opc = X86ISD::COMI;
5813 CC = ISD::SETGE;
5814 break;
5815 case Intrinsic::x86_sse_comineq_ss:
5816 case Intrinsic::x86_sse2_comineq_sd:
5817 Opc = X86ISD::COMI;
5818 CC = ISD::SETNE;
5819 break;
5820 case Intrinsic::x86_sse_ucomieq_ss:
5821 case Intrinsic::x86_sse2_ucomieq_sd:
5822 Opc = X86ISD::UCOMI;
5823 CC = ISD::SETEQ;
5824 break;
5825 case Intrinsic::x86_sse_ucomilt_ss:
5826 case Intrinsic::x86_sse2_ucomilt_sd:
5827 Opc = X86ISD::UCOMI;
5828 CC = ISD::SETLT;
5829 break;
5830 case Intrinsic::x86_sse_ucomile_ss:
5831 case Intrinsic::x86_sse2_ucomile_sd:
5832 Opc = X86ISD::UCOMI;
5833 CC = ISD::SETLE;
5834 break;
5835 case Intrinsic::x86_sse_ucomigt_ss:
5836 case Intrinsic::x86_sse2_ucomigt_sd:
5837 Opc = X86ISD::UCOMI;
5838 CC = ISD::SETGT;
5839 break;
5840 case Intrinsic::x86_sse_ucomige_ss:
5841 case Intrinsic::x86_sse2_ucomige_sd:
5842 Opc = X86ISD::UCOMI;
5843 CC = ISD::SETGE;
5844 break;
5845 case Intrinsic::x86_sse_ucomineq_ss:
5846 case Intrinsic::x86_sse2_ucomineq_sd:
5847 Opc = X86ISD::UCOMI;
5848 CC = ISD::SETNE;
5849 break;
5850 }
5851
Dan Gohman8181bd12008-07-27 21:46:04 +00005852 SDValue LHS = Op.getOperand(1);
5853 SDValue RHS = Op.getOperand(2);
Chris Lattnerebb91142008-12-24 23:53:05 +00005854 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman8181bd12008-07-27 21:46:04 +00005855 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5856 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005857 DAG.getConstant(X86CC, MVT::i8), Cond);
5858 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005859 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005860
5861 // Fix vector shift instructions where the last operand is a non-immediate
5862 // i32 value.
5863 case Intrinsic::x86_sse2_pslli_w:
5864 case Intrinsic::x86_sse2_pslli_d:
5865 case Intrinsic::x86_sse2_pslli_q:
5866 case Intrinsic::x86_sse2_psrli_w:
5867 case Intrinsic::x86_sse2_psrli_d:
5868 case Intrinsic::x86_sse2_psrli_q:
5869 case Intrinsic::x86_sse2_psrai_w:
5870 case Intrinsic::x86_sse2_psrai_d:
5871 case Intrinsic::x86_mmx_pslli_w:
5872 case Intrinsic::x86_mmx_pslli_d:
5873 case Intrinsic::x86_mmx_pslli_q:
5874 case Intrinsic::x86_mmx_psrli_w:
5875 case Intrinsic::x86_mmx_psrli_d:
5876 case Intrinsic::x86_mmx_psrli_q:
5877 case Intrinsic::x86_mmx_psrai_w:
5878 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005879 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005880 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005881 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005882
5883 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005884 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005885 switch (IntNo) {
5886 case Intrinsic::x86_sse2_pslli_w:
5887 NewIntNo = Intrinsic::x86_sse2_psll_w;
5888 break;
5889 case Intrinsic::x86_sse2_pslli_d:
5890 NewIntNo = Intrinsic::x86_sse2_psll_d;
5891 break;
5892 case Intrinsic::x86_sse2_pslli_q:
5893 NewIntNo = Intrinsic::x86_sse2_psll_q;
5894 break;
5895 case Intrinsic::x86_sse2_psrli_w:
5896 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5897 break;
5898 case Intrinsic::x86_sse2_psrli_d:
5899 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5900 break;
5901 case Intrinsic::x86_sse2_psrli_q:
5902 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5903 break;
5904 case Intrinsic::x86_sse2_psrai_w:
5905 NewIntNo = Intrinsic::x86_sse2_psra_w;
5906 break;
5907 case Intrinsic::x86_sse2_psrai_d:
5908 NewIntNo = Intrinsic::x86_sse2_psra_d;
5909 break;
5910 default: {
5911 ShAmtVT = MVT::v2i32;
5912 switch (IntNo) {
5913 case Intrinsic::x86_mmx_pslli_w:
5914 NewIntNo = Intrinsic::x86_mmx_psll_w;
5915 break;
5916 case Intrinsic::x86_mmx_pslli_d:
5917 NewIntNo = Intrinsic::x86_mmx_psll_d;
5918 break;
5919 case Intrinsic::x86_mmx_pslli_q:
5920 NewIntNo = Intrinsic::x86_mmx_psll_q;
5921 break;
5922 case Intrinsic::x86_mmx_psrli_w:
5923 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5924 break;
5925 case Intrinsic::x86_mmx_psrli_d:
5926 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5927 break;
5928 case Intrinsic::x86_mmx_psrli_q:
5929 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5930 break;
5931 case Intrinsic::x86_mmx_psrai_w:
5932 NewIntNo = Intrinsic::x86_mmx_psra_w;
5933 break;
5934 case Intrinsic::x86_mmx_psrai_d:
5935 NewIntNo = Intrinsic::x86_mmx_psra_d;
5936 break;
5937 default: abort(); // Can't reach here.
5938 }
5939 break;
5940 }
5941 }
Duncan Sands92c43912008-06-06 12:08:01 +00005942 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005943 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5944 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5945 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5946 DAG.getConstant(NewIntNo, MVT::i32),
5947 Op.getOperand(1), ShAmt);
5948 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005949 }
5950}
5951
Dan Gohman8181bd12008-07-27 21:46:04 +00005952SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling6ddc87b2009-01-16 19:25:27 +00005953 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5954
5955 if (Depth > 0) {
5956 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
5957 SDValue Offset =
5958 DAG.getConstant(TD->getPointerSize(),
5959 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
5960 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(),
5961 DAG.getNode(ISD::ADD, getPointerTy(), FrameAddr, Offset),
5962 NULL, 0);
5963 }
5964
5965 // Just load the return address.
Dan Gohman8181bd12008-07-27 21:46:04 +00005966 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005967 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5968}
5969
Dan Gohman8181bd12008-07-27 21:46:04 +00005970SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00005971 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
5972 MFI->setFrameAddressIsTaken(true);
5973 MVT VT = Op.getValueType();
5974 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5975 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
5976 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), FrameReg, VT);
5977 while (Depth--)
5978 FrameAddr = DAG.getLoad(VT, DAG.getEntryNode(), FrameAddr, NULL, 0);
5979 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005980}
5981
Dan Gohman8181bd12008-07-27 21:46:04 +00005982SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005983 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005984 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005985}
5986
Dan Gohman8181bd12008-07-27 21:46:04 +00005987SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005988{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005989 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005990 SDValue Chain = Op.getOperand(0);
5991 SDValue Offset = Op.getOperand(1);
5992 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005993
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005994 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5995 getPointerTy());
5996 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005997
Dan Gohman8181bd12008-07-27 21:46:04 +00005998 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005999 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006000 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
6001 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006002 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
6003 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006004
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006005 return DAG.getNode(X86ISD::EH_RETURN,
6006 MVT::Other,
6007 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006008}
6009
Dan Gohman8181bd12008-07-27 21:46:04 +00006010SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006011 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006012 SDValue Root = Op.getOperand(0);
6013 SDValue Trmp = Op.getOperand(1); // trampoline
6014 SDValue FPtr = Op.getOperand(2); // nested function
6015 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006016
Dan Gohman12a9c082008-02-06 22:27:42 +00006017 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006018
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006019 const X86InstrInfo *TII =
6020 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6021
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006022 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006023 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006024
6025 // Large code-model.
6026
6027 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6028 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6029
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006030 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6031 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006032
6033 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6034
6035 // Load the pointer to the nested function into R11.
6036 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00006037 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006038 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006039 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006040
6041 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00006042 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006043
6044 // Load the 'nest' parameter value into R10.
6045 // R10 is specified in X86CallingConv.td
6046 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
6047 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
6048 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006049 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006050
6051 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00006052 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006053
6054 // Jump to the nested function.
6055 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
6056 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
6057 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006058 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006059
6060 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
6061 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
6062 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006063 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006064
Dan Gohman8181bd12008-07-27 21:46:04 +00006065 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006066 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00006067 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006068 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00006069 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006070 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6071 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00006072 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006073
6074 switch (CC) {
6075 default:
6076 assert(0 && "Unsupported calling convention");
6077 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006078 case CallingConv::X86_StdCall: {
6079 // Pass 'nest' parameter in ECX.
6080 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006081 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006082
6083 // Check that ECX wasn't needed by an 'inreg' parameter.
6084 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00006085 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006086
Chris Lattner1c8733e2008-03-12 17:45:29 +00006087 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006088 unsigned InRegCount = 0;
6089 unsigned Idx = 1;
6090
6091 for (FunctionType::param_iterator I = FTy->param_begin(),
6092 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00006093 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006094 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006095 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006096
6097 if (InRegCount > 2) {
6098 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6099 abort();
6100 }
6101 }
6102 break;
6103 }
6104 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00006105 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006106 // Pass 'nest' parameter in EAX.
6107 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006108 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006109 break;
6110 }
6111
Dan Gohman8181bd12008-07-27 21:46:04 +00006112 SDValue OutChains[4];
6113 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006114
6115 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
6116 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
6117
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006118 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006119 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00006120 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00006121 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006122
6123 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006124 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006125
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006126 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006127 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
6128 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006129 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006130
6131 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006132 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006133
Dan Gohman8181bd12008-07-27 21:46:04 +00006134 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00006135 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00006136 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006137 }
6138}
6139
Dan Gohman8181bd12008-07-27 21:46:04 +00006140SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006141 /*
6142 The rounding mode is in bits 11:10 of FPSR, and has the following
6143 settings:
6144 00 Round to nearest
6145 01 Round to -inf
6146 10 Round to +inf
6147 11 Round to 0
6148
6149 FLT_ROUNDS, on the other hand, expects the following:
6150 -1 Undefined
6151 0 Round to 0
6152 1 Round to nearest
6153 2 Round to +inf
6154 3 Round to -inf
6155
6156 To perform the conversion, we do:
6157 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6158 */
6159
6160 MachineFunction &MF = DAG.getMachineFunction();
6161 const TargetMachine &TM = MF.getTarget();
6162 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6163 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00006164 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006165
6166 // Save FP Control Word to stack slot
6167 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00006168 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006169
Dan Gohman8181bd12008-07-27 21:46:04 +00006170 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00006171 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006172
6173 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00006174 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006175
6176 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00006177 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006178 DAG.getNode(ISD::SRL, MVT::i16,
6179 DAG.getNode(ISD::AND, MVT::i16,
6180 CWD, DAG.getConstant(0x800, MVT::i16)),
6181 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006182 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006183 DAG.getNode(ISD::SRL, MVT::i16,
6184 DAG.getNode(ISD::AND, MVT::i16,
6185 CWD, DAG.getConstant(0x400, MVT::i16)),
6186 DAG.getConstant(9, MVT::i8));
6187
Dan Gohman8181bd12008-07-27 21:46:04 +00006188 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006189 DAG.getNode(ISD::AND, MVT::i16,
6190 DAG.getNode(ISD::ADD, MVT::i16,
6191 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
6192 DAG.getConstant(1, MVT::i16)),
6193 DAG.getConstant(3, MVT::i16));
6194
6195
Duncan Sands92c43912008-06-06 12:08:01 +00006196 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006197 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6198}
6199
Dan Gohman8181bd12008-07-27 21:46:04 +00006200SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006201 MVT VT = Op.getValueType();
6202 MVT OpVT = VT;
6203 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006204
6205 Op = Op.getOperand(0);
6206 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006207 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00006208 OpVT = MVT::i32;
6209 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6210 }
Evan Cheng48679f42007-12-14 02:13:44 +00006211
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006212 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6213 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6214 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
6215
6216 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006217 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006218 Ops.push_back(Op);
6219 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6220 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6221 Ops.push_back(Op.getValue(1));
6222 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6223
6224 // Finally xor with NumBits-1.
6225 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
6226
Evan Cheng48679f42007-12-14 02:13:44 +00006227 if (VT == MVT::i8)
6228 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6229 return Op;
6230}
6231
Dan Gohman8181bd12008-07-27 21:46:04 +00006232SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006233 MVT VT = Op.getValueType();
6234 MVT OpVT = VT;
6235 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006236
6237 Op = Op.getOperand(0);
6238 if (VT == MVT::i8) {
6239 OpVT = MVT::i32;
6240 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6241 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006242
6243 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6244 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6245 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
6246
6247 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006248 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006249 Ops.push_back(Op);
6250 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6251 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6252 Ops.push_back(Op.getValue(1));
6253 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6254
Evan Cheng48679f42007-12-14 02:13:44 +00006255 if (VT == MVT::i8)
6256 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6257 return Op;
6258}
6259
Mon P Wang14edb092008-12-18 21:42:19 +00006260SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6261 MVT VT = Op.getValueType();
6262 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
6263
6264 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6265 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6266 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6267 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6268 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6269 //
6270 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6271 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6272 // return AloBlo + AloBhi + AhiBlo;
6273
6274 SDValue A = Op.getOperand(0);
6275 SDValue B = Op.getOperand(1);
6276
6277 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6278 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6279 A, DAG.getConstant(32, MVT::i32));
6280 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6281 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6282 B, DAG.getConstant(32, MVT::i32));
6283 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6284 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6285 A, B);
6286 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6287 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6288 A, Bhi);
6289 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6290 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6291 Ahi, B);
6292 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6293 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6294 AloBhi, DAG.getConstant(32, MVT::i32));
6295 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6296 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6297 AhiBlo, DAG.getConstant(32, MVT::i32));
6298 SDValue Res = DAG.getNode(ISD::ADD, VT, AloBlo, AloBhi);
6299 Res = DAG.getNode(ISD::ADD, VT, Res, AhiBlo);
6300 return Res;
6301}
6302
6303
Bill Wendling7e04be62008-12-09 22:08:41 +00006304SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6305 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6306 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendlingd3511522008-12-02 01:06:39 +00006307 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6308 // has only one use.
Bill Wendlingd06b4202008-11-26 22:37:40 +00006309 SDNode *N = Op.getNode();
Bill Wendlingd3511522008-12-02 01:06:39 +00006310 SDValue LHS = N->getOperand(0);
6311 SDValue RHS = N->getOperand(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00006312 unsigned BaseOp = 0;
6313 unsigned Cond = 0;
6314
6315 switch (Op.getOpcode()) {
6316 default: assert(0 && "Unknown ovf instruction!");
6317 case ISD::SADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006318 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00006319 Cond = X86::COND_O;
6320 break;
6321 case ISD::UADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006322 BaseOp = X86ISD::ADD;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006323 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006324 break;
6325 case ISD::SSUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006326 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00006327 Cond = X86::COND_O;
6328 break;
6329 case ISD::USUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006330 BaseOp = X86ISD::SUB;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006331 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006332 break;
6333 case ISD::SMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006334 BaseOp = X86ISD::SMUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00006335 Cond = X86::COND_O;
6336 break;
6337 case ISD::UMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006338 BaseOp = X86ISD::UMUL;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006339 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006340 break;
6341 }
Bill Wendlingd06b4202008-11-26 22:37:40 +00006342
Bill Wendlingd3511522008-12-02 01:06:39 +00006343 // Also sets EFLAGS.
6344 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Bill Wendling7e04be62008-12-09 22:08:41 +00006345 SDValue Sum = DAG.getNode(BaseOp, VTs, LHS, RHS);
Bill Wendlingd06b4202008-11-26 22:37:40 +00006346
Bill Wendlingd3511522008-12-02 01:06:39 +00006347 SDValue SetCC =
6348 DAG.getNode(X86ISD::SETCC, N->getValueType(1),
Bill Wendling35f1a9d2008-12-10 02:01:32 +00006349 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendlingd06b4202008-11-26 22:37:40 +00006350
Bill Wendlingd3511522008-12-02 01:06:39 +00006351 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6352 return Sum;
Bill Wendling4c134df2008-11-24 19:21:46 +00006353}
6354
Dan Gohman8181bd12008-07-27 21:46:04 +00006355SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006356 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00006357 unsigned Reg = 0;
6358 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006359 switch(T.getSimpleVT()) {
6360 default:
6361 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006362 case MVT::i8: Reg = X86::AL; size = 1; break;
6363 case MVT::i16: Reg = X86::AX; size = 2; break;
6364 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00006365 case MVT::i64:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006366 assert(Subtarget->is64Bit() && "Node not type legal!");
6367 Reg = X86::RAX; size = 8;
Andrew Lenharth81580822008-03-05 01:15:49 +00006368 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00006369 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006370 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00006371 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00006372 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006373 Op.getOperand(1),
6374 Op.getOperand(3),
6375 DAG.getTargetConstant(size, MVT::i8),
6376 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006377 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006378 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
6379 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006380 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
6381 return cpOut;
6382}
6383
Duncan Sands7d9834b2008-12-01 11:39:25 +00006384SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif825aa892008-08-28 23:19:51 +00006385 SelectionDAG &DAG) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006386 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharth81580822008-03-05 01:15:49 +00006387 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006388 SDValue TheChain = Op.getOperand(0);
6389 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6390 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
6391 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX, MVT::i64,
6392 rax.getValue(2));
6393 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
6394 DAG.getConstant(32, MVT::i8));
6395 SDValue Ops[] = {
6396 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp),
6397 rdx.getValue(1)
6398 };
6399 return DAG.getMergeValues(Ops, 2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006400}
6401
Dale Johannesen9011d872008-09-29 22:25:26 +00006402SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6403 SDNode *Node = Op.getNode();
6404 MVT T = Node->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006405 SDValue negOp = DAG.getNode(ISD::SUB, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006406 DAG.getConstant(0, T), Node->getOperand(2));
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006407 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD,
6408 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen9011d872008-09-29 22:25:26 +00006409 Node->getOperand(0),
6410 Node->getOperand(1), negOp,
6411 cast<AtomicSDNode>(Node)->getSrcValue(),
6412 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006413}
6414
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006415/// LowerOperation - Provide custom lowering hooks for some operations.
6416///
Dan Gohman8181bd12008-07-27 21:46:04 +00006417SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006418 switch (Op.getOpcode()) {
6419 default: assert(0 && "Should not custom lower this!");
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006420 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6421 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006422 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6423 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6424 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6425 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6426 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6427 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6428 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6429 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006430 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006431 case ISD::SHL_PARTS:
6432 case ISD::SRA_PARTS:
6433 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6434 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00006435 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006436 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6437 case ISD::FABS: return LowerFABS(Op, DAG);
6438 case ISD::FNEG: return LowerFNEG(Op, DAG);
6439 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006440 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006441 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006442 case ISD::SELECT: return LowerSELECT(Op, DAG);
6443 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006444 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6445 case ISD::CALL: return LowerCALL(Op, DAG);
6446 case ISD::RET: return LowerRET(Op, DAG);
6447 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006448 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006449 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006450 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6451 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6452 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6453 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6454 case ISD::FRAME_TO_ARGS_OFFSET:
6455 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6456 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6457 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006458 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006459 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006460 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6461 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wang14edb092008-12-18 21:42:19 +00006462 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling7e04be62008-12-09 22:08:41 +00006463 case ISD::SADDO:
6464 case ISD::UADDO:
6465 case ISD::SSUBO:
6466 case ISD::USUBO:
6467 case ISD::SMULO:
6468 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006469 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006470 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006471}
6472
Duncan Sands7d9834b2008-12-01 11:39:25 +00006473void X86TargetLowering::
6474ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6475 SelectionDAG &DAG, unsigned NewOp) {
6476 MVT T = Node->getValueType(0);
6477 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6478
6479 SDValue Chain = Node->getOperand(0);
6480 SDValue In1 = Node->getOperand(1);
6481 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6482 Node->getOperand(2), DAG.getIntPtrConstant(0));
6483 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6484 Node->getOperand(2), DAG.getIntPtrConstant(1));
6485 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6486 // have a MemOperand. Pass the info through as a normal operand.
6487 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6488 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6489 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
6490 SDValue Result = DAG.getNode(NewOp, Tys, Ops, 5);
6491 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
6492 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6493 Results.push_back(Result.getValue(2));
6494}
6495
Duncan Sandsac496a12008-07-04 11:47:58 +00006496/// ReplaceNodeResults - Replace a node with an illegal result type
6497/// with a new node built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00006498void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6499 SmallVectorImpl<SDValue>&Results,
6500 SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006501 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006502 default:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006503 assert(false && "Do not know how to custom type legalize this operation!");
6504 return;
6505 case ISD::FP_TO_SINT: {
6506 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
6507 SDValue FIST = Vals.first, StackSlot = Vals.second;
6508 if (FIST.getNode() != 0) {
6509 MVT VT = N->getValueType(0);
6510 // Return a load from the stack slot.
6511 Results.push_back(DAG.getLoad(VT, FIST, StackSlot, NULL, 0));
6512 }
6513 return;
6514 }
6515 case ISD::READCYCLECOUNTER: {
6516 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6517 SDValue TheChain = N->getOperand(0);
6518 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6519 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
6520 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX, MVT::i32,
6521 eax.getValue(2));
6522 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6523 SDValue Ops[] = { eax, edx };
6524 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2));
6525 Results.push_back(edx.getValue(1));
6526 return;
6527 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006528 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006529 MVT T = N->getValueType(0);
6530 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6531 SDValue cpInL, cpInH;
6532 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6533 DAG.getConstant(0, MVT::i32));
6534 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6535 DAG.getConstant(1, MVT::i32));
6536 cpInL = DAG.getCopyToReg(N->getOperand(0), X86::EAX, cpInL, SDValue());
6537 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX, cpInH,
6538 cpInL.getValue(1));
6539 SDValue swapInL, swapInH;
6540 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6541 DAG.getConstant(0, MVT::i32));
6542 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6543 DAG.getConstant(1, MVT::i32));
6544 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX, swapInL,
6545 cpInH.getValue(1));
6546 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX, swapInH,
6547 swapInL.getValue(1));
6548 SDValue Ops[] = { swapInH.getValue(0),
6549 N->getOperand(1),
6550 swapInH.getValue(1) };
6551 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6552 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
6553 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
6554 Result.getValue(1));
6555 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
6556 cpOutL.getValue(2));
6557 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
6558 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6559 Results.push_back(cpOutH.getValue(1));
6560 return;
6561 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006562 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006563 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6564 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006565 case ISD::ATOMIC_LOAD_AND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006566 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6567 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006568 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006569 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6570 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006571 case ISD::ATOMIC_LOAD_OR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006572 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6573 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006574 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006575 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6576 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006577 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006578 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6579 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006580 case ISD::ATOMIC_SWAP:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006581 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6582 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006583 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006584}
6585
6586const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6587 switch (Opcode) {
6588 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006589 case X86ISD::BSF: return "X86ISD::BSF";
6590 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006591 case X86ISD::SHLD: return "X86ISD::SHLD";
6592 case X86ISD::SHRD: return "X86ISD::SHRD";
6593 case X86ISD::FAND: return "X86ISD::FAND";
6594 case X86ISD::FOR: return "X86ISD::FOR";
6595 case X86ISD::FXOR: return "X86ISD::FXOR";
6596 case X86ISD::FSRL: return "X86ISD::FSRL";
6597 case X86ISD::FILD: return "X86ISD::FILD";
6598 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6599 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6600 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6601 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6602 case X86ISD::FLD: return "X86ISD::FLD";
6603 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006604 case X86ISD::CALL: return "X86ISD::CALL";
6605 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6606 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohman7fe9b7f2008-12-23 22:45:23 +00006607 case X86ISD::BT: return "X86ISD::BT";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006608 case X86ISD::CMP: return "X86ISD::CMP";
6609 case X86ISD::COMI: return "X86ISD::COMI";
6610 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6611 case X86ISD::SETCC: return "X86ISD::SETCC";
6612 case X86ISD::CMOV: return "X86ISD::CMOV";
6613 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6614 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6615 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6616 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006617 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6618 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006619 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006620 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006621 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6622 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006623 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6624 case X86ISD::FMAX: return "X86ISD::FMAX";
6625 case X86ISD::FMIN: return "X86ISD::FMIN";
6626 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6627 case X86ISD::FRCP: return "X86ISD::FRCP";
6628 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6629 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6630 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006631 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006632 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006633 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6634 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006635 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6636 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6637 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6638 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6639 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6640 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006641 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6642 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006643 case X86ISD::VSHL: return "X86ISD::VSHL";
6644 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006645 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6646 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6647 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6648 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6649 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6650 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6651 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6652 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6653 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6654 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingae034ed2008-12-12 00:56:36 +00006655 case X86ISD::ADD: return "X86ISD::ADD";
6656 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingf5399032008-12-12 21:15:41 +00006657 case X86ISD::SMUL: return "X86ISD::SMUL";
6658 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006659 }
6660}
6661
6662// isLegalAddressingMode - Return true if the addressing mode represented
6663// by AM is legal for this target, for a load/store of the specified type.
6664bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6665 const Type *Ty) const {
6666 // X86 supports extremely general addressing modes.
6667
6668 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6669 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6670 return false;
6671
6672 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006673 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006674 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6675 return false;
Dale Johannesen64660e92008-12-05 21:47:27 +00006676 // If BaseGV requires a register, we cannot also have a BaseReg.
6677 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6678 AM.HasBaseReg)
6679 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006680
6681 // X86-64 only supports addr of globals in small code model.
6682 if (Subtarget->is64Bit()) {
6683 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6684 return false;
6685 // If lower 4G is not available, then we must use rip-relative addressing.
6686 if (AM.BaseOffs || AM.Scale > 1)
6687 return false;
6688 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006689 }
6690
6691 switch (AM.Scale) {
6692 case 0:
6693 case 1:
6694 case 2:
6695 case 4:
6696 case 8:
6697 // These scales always work.
6698 break;
6699 case 3:
6700 case 5:
6701 case 9:
6702 // These scales are formed with basereg+scalereg. Only accept if there is
6703 // no basereg yet.
6704 if (AM.HasBaseReg)
6705 return false;
6706 break;
6707 default: // Other stuff never works.
6708 return false;
6709 }
6710
6711 return true;
6712}
6713
6714
Evan Cheng27a820a2007-10-26 01:56:11 +00006715bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6716 if (!Ty1->isInteger() || !Ty2->isInteger())
6717 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006718 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6719 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006720 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006721 return false;
6722 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006723}
6724
Duncan Sands92c43912008-06-06 12:08:01 +00006725bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6726 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006727 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006728 unsigned NumBits1 = VT1.getSizeInBits();
6729 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006730 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006731 return false;
6732 return Subtarget->is64Bit() || NumBits1 < 64;
6733}
Evan Cheng27a820a2007-10-26 01:56:11 +00006734
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006735/// isShuffleMaskLegal - Targets can use this to indicate that they only
6736/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6737/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6738/// are assumed to be legal.
6739bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006740X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006741 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006742 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006743 return (Mask.getNode()->getNumOperands() <= 4 ||
6744 isIdentityMask(Mask.getNode()) ||
6745 isIdentityMask(Mask.getNode(), true) ||
6746 isSplatMask(Mask.getNode()) ||
6747 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6748 X86::isUNPCKLMask(Mask.getNode()) ||
6749 X86::isUNPCKHMask(Mask.getNode()) ||
6750 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6751 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006752}
6753
Dan Gohman48d5f062008-04-09 20:09:42 +00006754bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006755X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006756 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006757 unsigned NumElts = BVOps.size();
6758 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006759 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006760 if (NumElts == 2) return true;
6761 if (NumElts == 4) {
6762 return (isMOVLMask(&BVOps[0], 4) ||
6763 isCommutedMOVL(&BVOps[0], 4, true) ||
6764 isSHUFPMask(&BVOps[0], 4) ||
6765 isCommutedSHUFP(&BVOps[0], 4));
6766 }
6767 return false;
6768}
6769
6770//===----------------------------------------------------------------------===//
6771// X86 Scheduler Hooks
6772//===----------------------------------------------------------------------===//
6773
Mon P Wang078a62d2008-05-05 19:05:59 +00006774// private utility function
6775MachineBasicBlock *
6776X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6777 MachineBasicBlock *MBB,
6778 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006779 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006780 unsigned LoadOpc,
6781 unsigned CXchgOpc,
6782 unsigned copyOpc,
6783 unsigned notOpc,
6784 unsigned EAXreg,
6785 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006786 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006787 // For the atomic bitwise operator, we generate
6788 // thisMBB:
6789 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006790 // ld t1 = [bitinstr.addr]
6791 // op t2 = t1, [bitinstr.val]
6792 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006793 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6794 // bz newMBB
6795 // fallthrough -->nextMBB
6796 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6797 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006798 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006799 ++MBBIter;
6800
6801 /// First build the CFG
6802 MachineFunction *F = MBB->getParent();
6803 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006804 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6805 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6806 F->insert(MBBIter, newMBB);
6807 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006808
6809 // Move all successors to thisMBB to nextMBB
6810 nextMBB->transferSuccessors(thisMBB);
6811
6812 // Update thisMBB to fall through to newMBB
6813 thisMBB->addSuccessor(newMBB);
6814
6815 // newMBB jumps to itself and fall through to nextMBB
6816 newMBB->addSuccessor(nextMBB);
6817 newMBB->addSuccessor(newMBB);
6818
6819 // Insert instructions into newMBB based on incoming instruction
6820 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6821 MachineOperand& destOper = bInstr->getOperand(0);
6822 MachineOperand* argOpers[6];
6823 int numArgs = bInstr->getNumOperands() - 1;
6824 for (int i=0; i < numArgs; ++i)
6825 argOpers[i] = &bInstr->getOperand(i+1);
6826
6827 // x86 address has 4 operands: base, index, scale, and displacement
6828 int lastAddrIndx = 3; // [0,3]
6829 int valArgIndx = 4;
6830
Dale Johannesend20e4452008-08-19 18:47:28 +00006831 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6832 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006833 for (int i=0; i <= lastAddrIndx; ++i)
6834 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006835
Dale Johannesend20e4452008-08-19 18:47:28 +00006836 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006837 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006838 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006839 }
6840 else
6841 tt = t1;
6842
Dale Johannesend20e4452008-08-19 18:47:28 +00006843 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006844 assert((argOpers[valArgIndx]->isReg() ||
6845 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006846 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006847 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006848 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6849 else
6850 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006851 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006852 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006853
Dale Johannesend20e4452008-08-19 18:47:28 +00006854 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006855 MIB.addReg(t1);
6856
Dale Johannesend20e4452008-08-19 18:47:28 +00006857 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006858 for (int i=0; i <= lastAddrIndx; ++i)
6859 (*MIB).addOperand(*argOpers[i]);
6860 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006861 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6862 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6863
Dale Johannesend20e4452008-08-19 18:47:28 +00006864 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6865 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006866
6867 // insert branch
6868 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6869
Dan Gohman221a4372008-07-07 23:14:23 +00006870 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006871 return nextMBB;
6872}
6873
Dale Johannesen44eb5372008-10-03 19:41:08 +00006874// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00006875MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00006876X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
6877 MachineBasicBlock *MBB,
6878 unsigned regOpcL,
6879 unsigned regOpcH,
6880 unsigned immOpcL,
6881 unsigned immOpcH,
6882 bool invSrc) {
6883 // For the atomic bitwise operator, we generate
6884 // thisMBB (instructions are in pairs, except cmpxchg8b)
6885 // ld t1,t2 = [bitinstr.addr]
6886 // newMBB:
6887 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
6888 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006889 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00006890 // mov ECX, EBX <- t5, t6
6891 // mov EAX, EDX <- t1, t2
6892 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
6893 // mov t3, t4 <- EAX, EDX
6894 // bz newMBB
6895 // result in out1, out2
6896 // fallthrough -->nextMBB
6897
6898 const TargetRegisterClass *RC = X86::GR32RegisterClass;
6899 const unsigned LoadOpc = X86::MOV32rm;
6900 const unsigned copyOpc = X86::MOV32rr;
6901 const unsigned NotOpc = X86::NOT32r;
6902 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6903 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6904 MachineFunction::iterator MBBIter = MBB;
6905 ++MBBIter;
6906
6907 /// First build the CFG
6908 MachineFunction *F = MBB->getParent();
6909 MachineBasicBlock *thisMBB = MBB;
6910 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6911 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6912 F->insert(MBBIter, newMBB);
6913 F->insert(MBBIter, nextMBB);
6914
6915 // Move all successors to thisMBB to nextMBB
6916 nextMBB->transferSuccessors(thisMBB);
6917
6918 // Update thisMBB to fall through to newMBB
6919 thisMBB->addSuccessor(newMBB);
6920
6921 // newMBB jumps to itself and fall through to nextMBB
6922 newMBB->addSuccessor(nextMBB);
6923 newMBB->addSuccessor(newMBB);
6924
6925 // Insert instructions into newMBB based on incoming instruction
6926 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
6927 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
6928 MachineOperand& dest1Oper = bInstr->getOperand(0);
6929 MachineOperand& dest2Oper = bInstr->getOperand(1);
6930 MachineOperand* argOpers[6];
6931 for (int i=0; i < 6; ++i)
6932 argOpers[i] = &bInstr->getOperand(i+2);
6933
6934 // x86 address has 4 operands: base, index, scale, and displacement
6935 int lastAddrIndx = 3; // [0,3]
6936
6937 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6938 MachineInstrBuilder MIB = BuildMI(thisMBB, TII->get(LoadOpc), t1);
6939 for (int i=0; i <= lastAddrIndx; ++i)
6940 (*MIB).addOperand(*argOpers[i]);
6941 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
6942 MIB = BuildMI(thisMBB, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006943 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00006944 for (int i=0; i <= lastAddrIndx-1; ++i)
6945 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006946 MachineOperand newOp3 = *(argOpers[3]);
6947 if (newOp3.isImm())
6948 newOp3.setImm(newOp3.getImm()+4);
6949 else
6950 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00006951 (*MIB).addOperand(newOp3);
6952
6953 // t3/4 are defined later, at the bottom of the loop
6954 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
6955 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
6956 BuildMI(newMBB, TII->get(X86::PHI), dest1Oper.getReg())
6957 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
6958 BuildMI(newMBB, TII->get(X86::PHI), dest2Oper.getReg())
6959 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
6960
6961 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
6962 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
6963 if (invSrc) {
6964 MIB = BuildMI(newMBB, TII->get(NotOpc), tt1).addReg(t1);
6965 MIB = BuildMI(newMBB, TII->get(NotOpc), tt2).addReg(t2);
6966 } else {
6967 tt1 = t1;
6968 tt2 = t2;
6969 }
6970
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006971 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00006972 "invalid operand");
6973 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
6974 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006975 if (argOpers[4]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006976 MIB = BuildMI(newMBB, TII->get(regOpcL), t5);
6977 else
6978 MIB = BuildMI(newMBB, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006979 if (regOpcL != X86::MOV32rr)
6980 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00006981 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006982 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
6983 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
6984 if (argOpers[5]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006985 MIB = BuildMI(newMBB, TII->get(regOpcH), t6);
6986 else
6987 MIB = BuildMI(newMBB, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006988 if (regOpcH != X86::MOV32rr)
6989 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006990 (*MIB).addOperand(*argOpers[5]);
6991
6992 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EAX);
6993 MIB.addReg(t1);
6994 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EDX);
6995 MIB.addReg(t2);
6996
6997 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EBX);
6998 MIB.addReg(t5);
6999 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::ECX);
7000 MIB.addReg(t6);
7001
7002 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG8B));
7003 for (int i=0; i <= lastAddrIndx; ++i)
7004 (*MIB).addOperand(*argOpers[i]);
7005
7006 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7007 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7008
7009 MIB = BuildMI(newMBB, TII->get(copyOpc), t3);
7010 MIB.addReg(X86::EAX);
7011 MIB = BuildMI(newMBB, TII->get(copyOpc), t4);
7012 MIB.addReg(X86::EDX);
7013
7014 // insert branch
7015 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
7016
7017 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7018 return nextMBB;
7019}
7020
7021// private utility function
7022MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00007023X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7024 MachineBasicBlock *MBB,
7025 unsigned cmovOpc) {
7026 // For the atomic min/max operator, we generate
7027 // thisMBB:
7028 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00007029 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00007030 // mov t2 = [min/max.val]
7031 // cmp t1, t2
7032 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00007033 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00007034 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7035 // bz newMBB
7036 // fallthrough -->nextMBB
7037 //
7038 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7039 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007040 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00007041 ++MBBIter;
7042
7043 /// First build the CFG
7044 MachineFunction *F = MBB->getParent();
7045 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00007046 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7047 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7048 F->insert(MBBIter, newMBB);
7049 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007050
7051 // Move all successors to thisMBB to nextMBB
7052 nextMBB->transferSuccessors(thisMBB);
7053
7054 // Update thisMBB to fall through to newMBB
7055 thisMBB->addSuccessor(newMBB);
7056
7057 // newMBB jumps to newMBB and fall through to nextMBB
7058 newMBB->addSuccessor(nextMBB);
7059 newMBB->addSuccessor(newMBB);
7060
7061 // Insert instructions into newMBB based on incoming instruction
7062 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
7063 MachineOperand& destOper = mInstr->getOperand(0);
7064 MachineOperand* argOpers[6];
7065 int numArgs = mInstr->getNumOperands() - 1;
7066 for (int i=0; i < numArgs; ++i)
7067 argOpers[i] = &mInstr->getOperand(i+1);
7068
7069 // x86 address has 4 operands: base, index, scale, and displacement
7070 int lastAddrIndx = 3; // [0,3]
7071 int valArgIndx = 4;
7072
Mon P Wang318b0372008-05-05 22:56:23 +00007073 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
7074 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00007075 for (int i=0; i <= lastAddrIndx; ++i)
7076 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00007077
Mon P Wang078a62d2008-05-05 19:05:59 +00007078 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007079 assert((argOpers[valArgIndx]->isReg() ||
7080 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00007081 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00007082
7083 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007084 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00007085 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
7086 else
7087 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
7088 (*MIB).addOperand(*argOpers[valArgIndx]);
7089
Mon P Wang318b0372008-05-05 22:56:23 +00007090 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
7091 MIB.addReg(t1);
7092
Mon P Wang078a62d2008-05-05 19:05:59 +00007093 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
7094 MIB.addReg(t1);
7095 MIB.addReg(t2);
7096
7097 // Generate movc
7098 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
7099 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
7100 MIB.addReg(t2);
7101 MIB.addReg(t1);
7102
7103 // Cmp and exchange if none has modified the memory location
7104 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
7105 for (int i=0; i <= lastAddrIndx; ++i)
7106 (*MIB).addOperand(*argOpers[i]);
7107 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00007108 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7109 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00007110
7111 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
7112 MIB.addReg(X86::EAX);
7113
7114 // insert branch
7115 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
7116
Dan Gohman221a4372008-07-07 23:14:23 +00007117 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00007118 return nextMBB;
7119}
7120
7121
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007122MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00007123X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
7124 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007125 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7126 switch (MI->getOpcode()) {
7127 default: assert(false && "Unexpected instr type to insert");
Mon P Wang83edba52008-12-12 01:25:51 +00007128 case X86::CMOV_V1I64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007129 case X86::CMOV_FR32:
7130 case X86::CMOV_FR64:
7131 case X86::CMOV_V4F32:
7132 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00007133 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007134 // To "insert" a SELECT_CC instruction, we actually have to insert the
7135 // diamond control-flow pattern. The incoming instruction knows the
7136 // destination vreg to set, the condition code register to branch on, the
7137 // true/false values to select between, and a branch opcode to use.
7138 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007139 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007140 ++It;
7141
7142 // thisMBB:
7143 // ...
7144 // TrueVal = ...
7145 // cmpTY ccX, r1, r2
7146 // bCC copy1MBB
7147 // fallthrough --> copy0MBB
7148 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00007149 MachineFunction *F = BB->getParent();
7150 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7151 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007152 unsigned Opc =
7153 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
7154 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00007155 F->insert(It, copy0MBB);
7156 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007157 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007158 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00007159 sinkMBB->transferSuccessors(BB);
7160
7161 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007162 BB->addSuccessor(copy0MBB);
7163 BB->addSuccessor(sinkMBB);
7164
7165 // copy0MBB:
7166 // %FalseValue = ...
7167 // # fallthrough to sinkMBB
7168 BB = copy0MBB;
7169
7170 // Update machine-CFG edges
7171 BB->addSuccessor(sinkMBB);
7172
7173 // sinkMBB:
7174 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7175 // ...
7176 BB = sinkMBB;
7177 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
7178 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7179 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7180
Dan Gohman221a4372008-07-07 23:14:23 +00007181 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007182 return BB;
7183 }
7184
7185 case X86::FP32_TO_INT16_IN_MEM:
7186 case X86::FP32_TO_INT32_IN_MEM:
7187 case X86::FP32_TO_INT64_IN_MEM:
7188 case X86::FP64_TO_INT16_IN_MEM:
7189 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007190 case X86::FP64_TO_INT64_IN_MEM:
7191 case X86::FP80_TO_INT16_IN_MEM:
7192 case X86::FP80_TO_INT32_IN_MEM:
7193 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007194 // Change the floating point control register to use "round towards zero"
7195 // mode when truncating to an integer value.
7196 MachineFunction *F = BB->getParent();
7197 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
7198 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
7199
7200 // Load the old value of the high byte of the control word...
7201 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00007202 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007203 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
7204
7205 // Set the high part to be round to zero...
7206 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
7207 .addImm(0xC7F);
7208
7209 // Reload the modified control word now...
7210 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7211
7212 // Restore the memory image of control word to original value
7213 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
7214 .addReg(OldCW);
7215
7216 // Get the X86 opcode to use.
7217 unsigned Opc;
7218 switch (MI->getOpcode()) {
7219 default: assert(0 && "illegal opcode!");
7220 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7221 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7222 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7223 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7224 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7225 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007226 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7227 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7228 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007229 }
7230
7231 X86AddressMode AM;
7232 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007233 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007234 AM.BaseType = X86AddressMode::RegBase;
7235 AM.Base.Reg = Op.getReg();
7236 } else {
7237 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00007238 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007239 }
7240 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007241 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007242 AM.Scale = Op.getImm();
7243 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007244 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007245 AM.IndexReg = Op.getImm();
7246 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007247 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007248 AM.GV = Op.getGlobal();
7249 } else {
7250 AM.Disp = Op.getImm();
7251 }
7252 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
7253 .addReg(MI->getOperand(4).getReg());
7254
7255 // Reload the original control word now.
7256 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7257
Dan Gohman221a4372008-07-07 23:14:23 +00007258 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007259 return BB;
7260 }
Mon P Wang078a62d2008-05-05 19:05:59 +00007261 case X86::ATOMAND32:
7262 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007263 X86::AND32ri, X86::MOV32rm,
7264 X86::LCMPXCHG32, X86::MOV32rr,
7265 X86::NOT32r, X86::EAX,
7266 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007267 case X86::ATOMOR32:
7268 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007269 X86::OR32ri, X86::MOV32rm,
7270 X86::LCMPXCHG32, X86::MOV32rr,
7271 X86::NOT32r, X86::EAX,
7272 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007273 case X86::ATOMXOR32:
7274 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007275 X86::XOR32ri, X86::MOV32rm,
7276 X86::LCMPXCHG32, X86::MOV32rr,
7277 X86::NOT32r, X86::EAX,
7278 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007279 case X86::ATOMNAND32:
7280 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007281 X86::AND32ri, X86::MOV32rm,
7282 X86::LCMPXCHG32, X86::MOV32rr,
7283 X86::NOT32r, X86::EAX,
7284 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00007285 case X86::ATOMMIN32:
7286 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7287 case X86::ATOMMAX32:
7288 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7289 case X86::ATOMUMIN32:
7290 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7291 case X86::ATOMUMAX32:
7292 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00007293
7294 case X86::ATOMAND16:
7295 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7296 X86::AND16ri, X86::MOV16rm,
7297 X86::LCMPXCHG16, X86::MOV16rr,
7298 X86::NOT16r, X86::AX,
7299 X86::GR16RegisterClass);
7300 case X86::ATOMOR16:
7301 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7302 X86::OR16ri, X86::MOV16rm,
7303 X86::LCMPXCHG16, X86::MOV16rr,
7304 X86::NOT16r, X86::AX,
7305 X86::GR16RegisterClass);
7306 case X86::ATOMXOR16:
7307 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7308 X86::XOR16ri, X86::MOV16rm,
7309 X86::LCMPXCHG16, X86::MOV16rr,
7310 X86::NOT16r, X86::AX,
7311 X86::GR16RegisterClass);
7312 case X86::ATOMNAND16:
7313 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7314 X86::AND16ri, X86::MOV16rm,
7315 X86::LCMPXCHG16, X86::MOV16rr,
7316 X86::NOT16r, X86::AX,
7317 X86::GR16RegisterClass, true);
7318 case X86::ATOMMIN16:
7319 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7320 case X86::ATOMMAX16:
7321 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7322 case X86::ATOMUMIN16:
7323 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7324 case X86::ATOMUMAX16:
7325 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7326
7327 case X86::ATOMAND8:
7328 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7329 X86::AND8ri, X86::MOV8rm,
7330 X86::LCMPXCHG8, X86::MOV8rr,
7331 X86::NOT8r, X86::AL,
7332 X86::GR8RegisterClass);
7333 case X86::ATOMOR8:
7334 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7335 X86::OR8ri, X86::MOV8rm,
7336 X86::LCMPXCHG8, X86::MOV8rr,
7337 X86::NOT8r, X86::AL,
7338 X86::GR8RegisterClass);
7339 case X86::ATOMXOR8:
7340 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7341 X86::XOR8ri, X86::MOV8rm,
7342 X86::LCMPXCHG8, X86::MOV8rr,
7343 X86::NOT8r, X86::AL,
7344 X86::GR8RegisterClass);
7345 case X86::ATOMNAND8:
7346 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7347 X86::AND8ri, X86::MOV8rm,
7348 X86::LCMPXCHG8, X86::MOV8rr,
7349 X86::NOT8r, X86::AL,
7350 X86::GR8RegisterClass, true);
7351 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00007352 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007353 case X86::ATOMAND64:
7354 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7355 X86::AND64ri32, X86::MOV64rm,
7356 X86::LCMPXCHG64, X86::MOV64rr,
7357 X86::NOT64r, X86::RAX,
7358 X86::GR64RegisterClass);
7359 case X86::ATOMOR64:
7360 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7361 X86::OR64ri32, X86::MOV64rm,
7362 X86::LCMPXCHG64, X86::MOV64rr,
7363 X86::NOT64r, X86::RAX,
7364 X86::GR64RegisterClass);
7365 case X86::ATOMXOR64:
7366 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7367 X86::XOR64ri32, X86::MOV64rm,
7368 X86::LCMPXCHG64, X86::MOV64rr,
7369 X86::NOT64r, X86::RAX,
7370 X86::GR64RegisterClass);
7371 case X86::ATOMNAND64:
7372 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7373 X86::AND64ri32, X86::MOV64rm,
7374 X86::LCMPXCHG64, X86::MOV64rr,
7375 X86::NOT64r, X86::RAX,
7376 X86::GR64RegisterClass, true);
7377 case X86::ATOMMIN64:
7378 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7379 case X86::ATOMMAX64:
7380 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7381 case X86::ATOMUMIN64:
7382 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7383 case X86::ATOMUMAX64:
7384 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00007385
7386 // This group does 64-bit operations on a 32-bit host.
7387 case X86::ATOMAND6432:
7388 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7389 X86::AND32rr, X86::AND32rr,
7390 X86::AND32ri, X86::AND32ri,
7391 false);
7392 case X86::ATOMOR6432:
7393 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7394 X86::OR32rr, X86::OR32rr,
7395 X86::OR32ri, X86::OR32ri,
7396 false);
7397 case X86::ATOMXOR6432:
7398 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7399 X86::XOR32rr, X86::XOR32rr,
7400 X86::XOR32ri, X86::XOR32ri,
7401 false);
7402 case X86::ATOMNAND6432:
7403 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7404 X86::AND32rr, X86::AND32rr,
7405 X86::AND32ri, X86::AND32ri,
7406 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00007407 case X86::ATOMADD6432:
7408 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7409 X86::ADD32rr, X86::ADC32rr,
7410 X86::ADD32ri, X86::ADC32ri,
7411 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00007412 case X86::ATOMSUB6432:
7413 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7414 X86::SUB32rr, X86::SBB32rr,
7415 X86::SUB32ri, X86::SBB32ri,
7416 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007417 case X86::ATOMSWAP6432:
7418 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7419 X86::MOV32rr, X86::MOV32rr,
7420 X86::MOV32ri, X86::MOV32ri,
7421 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007422 }
7423}
7424
7425//===----------------------------------------------------------------------===//
7426// X86 Optimization Hooks
7427//===----------------------------------------------------------------------===//
7428
Dan Gohman8181bd12008-07-27 21:46:04 +00007429void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00007430 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00007431 APInt &KnownZero,
7432 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007433 const SelectionDAG &DAG,
7434 unsigned Depth) const {
7435 unsigned Opc = Op.getOpcode();
7436 assert((Opc >= ISD::BUILTIN_OP_END ||
7437 Opc == ISD::INTRINSIC_WO_CHAIN ||
7438 Opc == ISD::INTRINSIC_W_CHAIN ||
7439 Opc == ISD::INTRINSIC_VOID) &&
7440 "Should use MaskedValueIsZero if you don't know whether Op"
7441 " is a target node!");
7442
Dan Gohman1d79e432008-02-13 23:07:24 +00007443 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007444 switch (Opc) {
7445 default: break;
7446 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007447 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7448 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007449 break;
7450 }
7451}
7452
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007453/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007454/// node is a GlobalAddress + offset.
7455bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7456 GlobalValue* &GA, int64_t &Offset) const{
7457 if (N->getOpcode() == X86ISD::Wrapper) {
7458 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007459 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00007460 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007461 return true;
7462 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007463 }
Evan Chengef7be082008-05-12 19:56:52 +00007464 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007465}
7466
Evan Chengef7be082008-05-12 19:56:52 +00007467static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7468 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007469 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007470 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007471 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007472 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007473 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007474 return false;
7475}
7476
Dan Gohman8181bd12008-07-27 21:46:04 +00007477static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007478 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007479 SDNode *&Base,
7480 SelectionDAG &DAG, MachineFrameInfo *MFI,
7481 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007482 Base = NULL;
7483 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007484 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007485 if (Idx.getOpcode() == ISD::UNDEF) {
7486 if (!Base)
7487 return false;
7488 continue;
7489 }
7490
Dan Gohman8181bd12008-07-27 21:46:04 +00007491 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007492 if (!Elt.getNode() ||
7493 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007494 return false;
7495 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007496 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007497 if (Base->getOpcode() == ISD::UNDEF)
7498 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007499 continue;
7500 }
7501 if (Elt.getOpcode() == ISD::UNDEF)
7502 continue;
7503
Gabor Greif1c80d112008-08-28 21:40:38 +00007504 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007505 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007506 return false;
7507 }
7508 return true;
7509}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007510
7511/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7512/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7513/// if the load addresses are consecutive, non-overlapping, and in the right
7514/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007515static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007516 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007517 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00007518 MVT VT = N->getValueType(0);
7519 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007520 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007521 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007522 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007523 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7524 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007525 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007526
Dan Gohman11821702007-07-27 17:16:43 +00007527 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007528 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007529 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00007530 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00007531 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
7532 LD->getSrcValueOffset(), LD->isVolatile(),
7533 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007534}
7535
Evan Chengb6290462008-05-12 23:04:07 +00007536/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007537static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng6617eed2008-09-24 23:26:36 +00007538 const X86Subtarget *Subtarget,
7539 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007540 unsigned NumOps = N->getNumOperands();
7541
Evan Chenge9b9c672008-05-09 21:53:03 +00007542 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007543 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007544 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007545
Duncan Sands92c43912008-06-06 12:08:01 +00007546 MVT VT = N->getValueType(0);
7547 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007548 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7549 // We are looking for load i64 and zero extend. We want to transform
7550 // it before legalizer has a chance to expand it. Also look for i64
7551 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007552 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007553 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007554 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007555 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007556 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007557
7558 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007559 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007560 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007561 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007562 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007563 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007564 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007565 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007566 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007567
7568 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007569 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007570
7571 // Load must not be an extload.
7572 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007573 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00007574
Evan Cheng6617eed2008-09-24 23:26:36 +00007575 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7576 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7577 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
7578 DAG.ReplaceAllUsesOfValueWith(SDValue(Base, 1), ResNode.getValue(1));
7579 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007580}
7581
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007582/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007583static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007584 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007585 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007586
7587 // If we have SSE[12] support, try to form min/max nodes.
7588 if (Subtarget->hasSSE2() &&
7589 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7590 if (Cond.getOpcode() == ISD::SETCC) {
7591 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007592 SDValue LHS = N->getOperand(1);
7593 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007594 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7595
7596 unsigned Opcode = 0;
7597 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7598 switch (CC) {
7599 default: break;
7600 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7601 case ISD::SETULE:
7602 case ISD::SETLE:
7603 if (!UnsafeFPMath) break;
7604 // FALL THROUGH.
7605 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7606 case ISD::SETLT:
7607 Opcode = X86ISD::FMIN;
7608 break;
7609
7610 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7611 case ISD::SETUGT:
7612 case ISD::SETGT:
7613 if (!UnsafeFPMath) break;
7614 // FALL THROUGH.
7615 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7616 case ISD::SETGE:
7617 Opcode = X86ISD::FMAX;
7618 break;
7619 }
7620 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7621 switch (CC) {
7622 default: break;
7623 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7624 case ISD::SETUGT:
7625 case ISD::SETGT:
7626 if (!UnsafeFPMath) break;
7627 // FALL THROUGH.
7628 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7629 case ISD::SETGE:
7630 Opcode = X86ISD::FMIN;
7631 break;
7632
7633 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7634 case ISD::SETULE:
7635 case ISD::SETLE:
7636 if (!UnsafeFPMath) break;
7637 // FALL THROUGH.
7638 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7639 case ISD::SETLT:
7640 Opcode = X86ISD::FMAX;
7641 break;
7642 }
7643 }
7644
7645 if (Opcode)
7646 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
7647 }
7648
7649 }
7650
Dan Gohman8181bd12008-07-27 21:46:04 +00007651 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007652}
7653
Chris Lattnerce84ae42008-02-22 02:09:43 +00007654/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007655static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007656 const X86Subtarget *Subtarget) {
7657 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7658 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007659 // A preferable solution to the general problem is to figure out the right
7660 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007661 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007662 if (St->getValue().getValueType().isVector() &&
7663 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007664 isa<LoadSDNode>(St->getValue()) &&
7665 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7666 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007667 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007668 LoadSDNode *Ld = 0;
7669 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007670 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007671 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007672 // Must be a store of a load. We currently handle two cases: the load
7673 // is a direct child, and it's under an intervening TokenFactor. It is
7674 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007675 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007676 Ld = cast<LoadSDNode>(St->getChain());
7677 else if (St->getValue().hasOneUse() &&
7678 ChainVal->getOpcode() == ISD::TokenFactor) {
7679 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007680 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007681 TokenFactorIndex = i;
7682 Ld = cast<LoadSDNode>(St->getValue());
7683 } else
7684 Ops.push_back(ChainVal->getOperand(i));
7685 }
7686 }
7687 if (Ld) {
7688 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7689 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007690 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007691 Ld->getBasePtr(), Ld->getSrcValue(),
7692 Ld->getSrcValueOffset(), Ld->isVolatile(),
7693 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007694 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007695 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00007696 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00007697 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7698 Ops.size());
7699 }
7700 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
7701 St->getSrcValue(), St->getSrcValueOffset(),
7702 St->isVolatile(), St->getAlignment());
7703 }
7704
7705 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00007706 SDValue LoAddr = Ld->getBasePtr();
7707 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007708 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007709
Dan Gohman8181bd12008-07-27 21:46:04 +00007710 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007711 Ld->getSrcValue(), Ld->getSrcValueOffset(),
7712 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007713 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007714 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
7715 Ld->isVolatile(),
7716 MinAlign(Ld->getAlignment(), 4));
7717
Dan Gohman8181bd12008-07-27 21:46:04 +00007718 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007719 if (TokenFactorIndex != -1) {
7720 Ops.push_back(LoLd);
7721 Ops.push_back(HiLd);
7722 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7723 Ops.size());
7724 }
7725
7726 LoAddr = St->getBasePtr();
7727 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007728 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007729
Dan Gohman8181bd12008-07-27 21:46:04 +00007730 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007731 St->getSrcValue(), St->getSrcValueOffset(),
7732 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007733 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00007734 St->getSrcValue(),
7735 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00007736 St->isVolatile(),
7737 MinAlign(St->getAlignment(), 4));
7738 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00007739 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00007740 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007741 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00007742}
7743
Chris Lattner470d5dc2008-01-25 06:14:17 +00007744/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7745/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007746static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00007747 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7748 // F[X]OR(0.0, x) -> x
7749 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00007750 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7751 if (C->getValueAPF().isPosZero())
7752 return N->getOperand(1);
7753 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7754 if (C->getValueAPF().isPosZero())
7755 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007756 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007757}
7758
7759/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007760static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007761 // FAND(0.0, x) -> 0.0
7762 // FAND(x, 0.0) -> 0.0
7763 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7764 if (C->getValueAPF().isPosZero())
7765 return N->getOperand(0);
7766 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7767 if (C->getValueAPF().isPosZero())
7768 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007769 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007770}
7771
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007772
Dan Gohman8181bd12008-07-27 21:46:04 +00007773SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00007774 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007775 SelectionDAG &DAG = DCI.DAG;
7776 switch (N->getOpcode()) {
7777 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007778 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7779 case ISD::BUILD_VECTOR:
7780 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007781 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007782 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007783 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007784 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7785 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007786 }
7787
Dan Gohman8181bd12008-07-27 21:46:04 +00007788 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007789}
7790
7791//===----------------------------------------------------------------------===//
7792// X86 Inline Assembly Support
7793//===----------------------------------------------------------------------===//
7794
7795/// getConstraintType - Given a constraint letter, return the type of
7796/// constraint it is for this target.
7797X86TargetLowering::ConstraintType
7798X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7799 if (Constraint.size() == 1) {
7800 switch (Constraint[0]) {
7801 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +00007802 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +00007803 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007804 case 'r':
7805 case 'R':
7806 case 'l':
7807 case 'q':
7808 case 'Q':
7809 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007810 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007811 case 'Y':
7812 return C_RegisterClass;
7813 default:
7814 break;
7815 }
7816 }
7817 return TargetLowering::getConstraintType(Constraint);
7818}
7819
Dale Johannesene99fc902008-01-29 02:21:21 +00007820/// LowerXConstraint - try to replace an X constraint, which matches anything,
7821/// with another that has more specific requirements based on the type of the
7822/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007823const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007824LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007825 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7826 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007827 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007828 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007829 return "Y";
7830 if (Subtarget->hasSSE1())
7831 return "x";
7832 }
7833
7834 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007835}
7836
Chris Lattnera531abc2007-08-25 00:47:38 +00007837/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7838/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007839void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007840 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00007841 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00007842 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007843 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007844 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007845
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007846 switch (Constraint) {
7847 default: break;
7848 case 'I':
7849 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007850 if (C->getZExtValue() <= 31) {
7851 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007852 break;
7853 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007854 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007855 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00007856 case 'J':
7857 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
7858 if (C->getZExtValue() <= 63) {
7859 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
7860 break;
7861 }
7862 }
7863 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007864 case 'N':
7865 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007866 if (C->getZExtValue() <= 255) {
7867 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007868 break;
7869 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007870 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007871 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007872 case 'i': {
7873 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007874 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007875 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007876 break;
7877 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007878
7879 // If we are in non-pic codegen mode, we allow the address of a global (with
7880 // an optional displacement) to be used with 'i'.
7881 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7882 int64_t Offset = 0;
7883
7884 // Match either (GA) or (GA+C)
7885 if (GA) {
7886 Offset = GA->getOffset();
7887 } else if (Op.getOpcode() == ISD::ADD) {
7888 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7889 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7890 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007891 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007892 } else {
7893 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7894 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7895 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007896 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007897 else
7898 C = 0, GA = 0;
7899 }
7900 }
7901
7902 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00007903 if (hasMemory)
Dan Gohman36322c72008-10-18 02:06:02 +00007904 Op = LowerGlobalAddress(GA->getGlobal(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00007905 else
7906 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7907 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007908 Result = Op;
7909 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007910 }
7911
7912 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007913 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007914 }
7915 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007916
Gabor Greif1c80d112008-08-28 21:40:38 +00007917 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007918 Ops.push_back(Result);
7919 return;
7920 }
Evan Cheng7f250d62008-09-24 00:05:32 +00007921 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
7922 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007923}
7924
7925std::vector<unsigned> X86TargetLowering::
7926getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007927 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007928 if (Constraint.size() == 1) {
7929 // FIXME: not handling fp-stack yet!
7930 switch (Constraint[0]) { // GCC X86 Constraint Letters
7931 default: break; // Unknown constraint letter
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007932 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7933 case 'Q': // Q_REGS
7934 if (VT == MVT::i32)
7935 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7936 else if (VT == MVT::i16)
7937 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7938 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007939 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007940 else if (VT == MVT::i64)
7941 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7942 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007943 }
7944 }
7945
7946 return std::vector<unsigned>();
7947}
7948
7949std::pair<unsigned, const TargetRegisterClass*>
7950X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007951 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007952 // First, see if this is a constraint that directly corresponds to an LLVM
7953 // register class.
7954 if (Constraint.size() == 1) {
7955 // GCC Constraint Letters
7956 switch (Constraint[0]) {
7957 default: break;
7958 case 'r': // GENERAL_REGS
7959 case 'R': // LEGACY_REGS
7960 case 'l': // INDEX_REGS
Chris Lattnerbbfea052008-10-17 18:15:05 +00007961 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007962 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00007963 if (VT == MVT::i16)
7964 return std::make_pair(0U, X86::GR16RegisterClass);
7965 if (VT == MVT::i32 || !Subtarget->is64Bit())
7966 return std::make_pair(0U, X86::GR32RegisterClass);
7967 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00007968 case 'f': // FP Stack registers.
7969 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7970 // value to the correct fpstack register class.
7971 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7972 return std::make_pair(0U, X86::RFP32RegisterClass);
7973 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7974 return std::make_pair(0U, X86::RFP64RegisterClass);
7975 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007976 case 'y': // MMX_REGS if MMX allowed.
7977 if (!Subtarget->hasMMX()) break;
7978 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007979 case 'Y': // SSE_REGS if SSE2 allowed
7980 if (!Subtarget->hasSSE2()) break;
7981 // FALL THROUGH.
7982 case 'x': // SSE_REGS if SSE1 allowed
7983 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007984
7985 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007986 default: break;
7987 // Scalar SSE types.
7988 case MVT::f32:
7989 case MVT::i32:
7990 return std::make_pair(0U, X86::FR32RegisterClass);
7991 case MVT::f64:
7992 case MVT::i64:
7993 return std::make_pair(0U, X86::FR64RegisterClass);
7994 // Vector types.
7995 case MVT::v16i8:
7996 case MVT::v8i16:
7997 case MVT::v4i32:
7998 case MVT::v2i64:
7999 case MVT::v4f32:
8000 case MVT::v2f64:
8001 return std::make_pair(0U, X86::VR128RegisterClass);
8002 }
8003 break;
8004 }
8005 }
8006
8007 // Use the default implementation in TargetLowering to convert the register
8008 // constraint into a member of a register class.
8009 std::pair<unsigned, const TargetRegisterClass*> Res;
8010 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8011
8012 // Not found as a standard register?
8013 if (Res.second == 0) {
8014 // GCC calls "st(0)" just plain "st".
8015 if (StringsEqualNoCase("{st}", Constraint)) {
8016 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00008017 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008018 }
Dale Johannesen73920c02008-11-13 21:52:36 +00008019 // 'A' means EAX + EDX.
8020 if (Constraint == "A") {
8021 Res.first = X86::EAX;
8022 Res.second = X86::GRADRegisterClass;
8023 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008024 return Res;
8025 }
8026
8027 // Otherwise, check to see if this is a register class of the wrong value
8028 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8029 // turn into {ax},{dx}.
8030 if (Res.second->hasType(VT))
8031 return Res; // Correct type already, nothing to do.
8032
8033 // All of the single-register GCC register classes map their values onto
8034 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8035 // really want an 8-bit or 32-bit register, map to the appropriate register
8036 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00008037 if (Res.second == X86::GR16RegisterClass) {
8038 if (VT == MVT::i8) {
8039 unsigned DestReg = 0;
8040 switch (Res.first) {
8041 default: break;
8042 case X86::AX: DestReg = X86::AL; break;
8043 case X86::DX: DestReg = X86::DL; break;
8044 case X86::CX: DestReg = X86::CL; break;
8045 case X86::BX: DestReg = X86::BL; break;
8046 }
8047 if (DestReg) {
8048 Res.first = DestReg;
8049 Res.second = Res.second = X86::GR8RegisterClass;
8050 }
8051 } else if (VT == MVT::i32) {
8052 unsigned DestReg = 0;
8053 switch (Res.first) {
8054 default: break;
8055 case X86::AX: DestReg = X86::EAX; break;
8056 case X86::DX: DestReg = X86::EDX; break;
8057 case X86::CX: DestReg = X86::ECX; break;
8058 case X86::BX: DestReg = X86::EBX; break;
8059 case X86::SI: DestReg = X86::ESI; break;
8060 case X86::DI: DestReg = X86::EDI; break;
8061 case X86::BP: DestReg = X86::EBP; break;
8062 case X86::SP: DestReg = X86::ESP; break;
8063 }
8064 if (DestReg) {
8065 Res.first = DestReg;
8066 Res.second = Res.second = X86::GR32RegisterClass;
8067 }
8068 } else if (VT == MVT::i64) {
8069 unsigned DestReg = 0;
8070 switch (Res.first) {
8071 default: break;
8072 case X86::AX: DestReg = X86::RAX; break;
8073 case X86::DX: DestReg = X86::RDX; break;
8074 case X86::CX: DestReg = X86::RCX; break;
8075 case X86::BX: DestReg = X86::RBX; break;
8076 case X86::SI: DestReg = X86::RSI; break;
8077 case X86::DI: DestReg = X86::RDI; break;
8078 case X86::BP: DestReg = X86::RBP; break;
8079 case X86::SP: DestReg = X86::RSP; break;
8080 }
8081 if (DestReg) {
8082 Res.first = DestReg;
8083 Res.second = Res.second = X86::GR64RegisterClass;
8084 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008085 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00008086 } else if (Res.second == X86::FR32RegisterClass ||
8087 Res.second == X86::FR64RegisterClass ||
8088 Res.second == X86::VR128RegisterClass) {
8089 // Handle references to XMM physical registers that got mapped into the
8090 // wrong class. This can happen with constraints like {xmm0} where the
8091 // target independent register mapper will just pick the first match it can
8092 // find, ignoring the required type.
8093 if (VT == MVT::f32)
8094 Res.second = X86::FR32RegisterClass;
8095 else if (VT == MVT::f64)
8096 Res.second = X86::FR64RegisterClass;
8097 else if (X86::VR128RegisterClass->hasType(VT))
8098 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008099 }
8100
8101 return Res;
8102}
Mon P Wang1448aad2008-10-30 08:01:45 +00008103
8104//===----------------------------------------------------------------------===//
8105// X86 Widen vector type
8106//===----------------------------------------------------------------------===//
8107
8108/// getWidenVectorType: given a vector type, returns the type to widen
8109/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8110/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +00008111/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +00008112/// scalarizing vs using the wider vector type.
8113
Dan Gohman0fe66c92009-01-15 17:34:08 +00008114MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang1448aad2008-10-30 08:01:45 +00008115 assert(VT.isVector());
8116 if (isTypeLegal(VT))
8117 return VT;
8118
8119 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8120 // type based on element type. This would speed up our search (though
8121 // it may not be worth it since the size of the list is relatively
8122 // small).
8123 MVT EltVT = VT.getVectorElementType();
8124 unsigned NElts = VT.getVectorNumElements();
8125
8126 // On X86, it make sense to widen any vector wider than 1
8127 if (NElts <= 1)
8128 return MVT::Other;
8129
8130 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
8131 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8132 MVT SVT = (MVT::SimpleValueType)nVT;
8133
8134 if (isTypeLegal(SVT) &&
8135 SVT.getVectorElementType() == EltVT &&
8136 SVT.getVectorNumElements() > NElts)
8137 return SVT;
8138 }
8139 return MVT::Other;
8140}