blob: 043e551473635ec236f5d5ef702f4303661674dc [file] [log] [blame]
Arnold Schwaighofera70fe792007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86InstrBuilder.h"
17#include "X86ISelLowering.h"
18#include "X86MachineFunctionInfo.h"
19#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
21#include "llvm/Constants.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/Function.h"
25#include "llvm/Intrinsics.h"
Evan Cheng75184a92007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/ADT/VectorExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/CodeGen/CallingConvLower.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000034#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Support/MathExtras.h"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000037#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Target/TargetOptions.h"
Evan Cheng75184a92007-12-11 01:46:18 +000039#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include "llvm/ADT/StringExtras.h"
Mon P Wang1f292322008-11-23 04:37:22 +000041#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042using namespace llvm;
43
Mon P Wang1f292322008-11-23 04:37:22 +000044static cl::opt<bool>
Mon P Wangba7e48e2008-11-24 02:10:43 +000045DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang1f292322008-11-23 04:37:22 +000046
Evan Cheng2aea0b42008-04-25 19:11:04 +000047// Forward declarations.
Dale Johannesence0805b2009-02-03 19:33:06 +000048static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG, DebugLoc dl);
Evan Cheng2aea0b42008-04-25 19:11:04 +000049
Dan Gohmanb41dfba2008-05-14 01:58:56 +000050X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 : TargetLowering(TM) {
52 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000053 X86ScalarSSEf64 = Subtarget->hasSSE2();
54 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000056
Chris Lattnerdec9cb52008-01-24 08:07:48 +000057 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
59 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000060 TD = getTargetData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061
62 // Set up the TargetLowering object.
63
64 // X86 is weird, it always uses i8 for shift amounts and setcc results.
65 setShiftAmountType(MVT::i8);
Duncan Sands8cf4a822008-11-23 15:47:28 +000066 setBooleanContents(ZeroOrOneBooleanContent);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 setSchedulingPreference(SchedulingForRegPressure);
68 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
69 setStackPointerRegisterToSaveRestore(X86StackPtr);
70
71 if (Subtarget->isTargetDarwin()) {
72 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
73 setUseUnderscoreSetJmp(false);
74 setUseUnderscoreLongJmp(false);
75 } else if (Subtarget->isTargetMingw()) {
76 // MS runtime is weird: it exports _setjmp, but longjmp!
77 setUseUnderscoreSetJmp(true);
78 setUseUnderscoreLongJmp(false);
79 } else {
80 setUseUnderscoreSetJmp(true);
81 setUseUnderscoreLongJmp(true);
82 }
83
84 // Set up the register classes.
85 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
86 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
87 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
88 if (Subtarget->is64Bit())
89 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
90
Evan Cheng08c171a2008-10-14 21:26:46 +000091 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
Chris Lattner3bc08502008-01-17 19:59:44 +000093 // We don't accept any truncstore of integer registers.
94 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
95 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
96 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
97 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
98 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
Evan Cheng71343822008-10-15 02:05:31 +000099 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
100
101 // SETOEQ and SETUNE require checking two conditions.
102 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
103 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
104 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
105 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
106 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
107 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattner3bc08502008-01-17 19:59:44 +0000108
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
110 // operation.
111 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
112 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
113 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
114
115 if (Subtarget->is64Bit()) {
116 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
117 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
118 } else {
Dale Johannesena359b8b2008-10-21 20:50:01 +0000119 if (X86ScalarSSEf64) {
120 // We have an impenetrably clever algorithm for ui64->double only.
121 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling14a30ef2009-01-17 03:56:04 +0000122
123 // We have faster algorithm for ui32->single only.
124 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Dale Johannesena359b8b2008-10-21 20:50:01 +0000125 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
127 }
128
129 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
130 // this operation.
131 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
132 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
133 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000134 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000136 // f32 and f64 cases are Legal, f80 case is not
137 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
138 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
140 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
141 }
142
Dale Johannesen958b08b2007-09-19 23:55:34 +0000143 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
144 // are Legal, f80 is custom lowered.
145 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
146 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147
148 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
149 // this operation.
150 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
151 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
152
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000153 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000155 // f32 and f64 cases are Legal, f80 case is not
156 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 } else {
158 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
159 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
160 }
161
162 // Handle FP_TO_UINT by promoting the destination to a larger signed
163 // conversion.
164 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
165 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
166 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
167
168 if (Subtarget->is64Bit()) {
169 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
170 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
171 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000172 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 // Expand FP_TO_UINT into a select.
174 // FIXME: We would like to use a Custom expander here eventually to do
175 // the optimal thing for SSE vs. the default expansion in the legalizer.
176 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
177 else
178 // With SSE3 we can use fisttpll to convert to a signed i64.
179 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
180 }
181
182 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000183 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
185 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
186 }
187
Dan Gohman8450d862008-02-18 19:34:53 +0000188 // Scalar integer divide and remainder are lowered to use operations that
189 // produce two results, to match the available instructions. This exposes
190 // the two-result form to trivial CSE, which is able to combine x/y and x%y
191 // into a single instruction.
192 //
193 // Scalar integer multiply-high is also lowered to use two-result
194 // operations, to match the available instructions. However, plain multiply
195 // (low) operations are left as Legal, as there are single-result
196 // instructions for this in x86. Using the two-result multiply instructions
197 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000198 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
199 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
200 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
201 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
202 setOperationAction(ISD::SREM , MVT::i8 , Expand);
203 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000204 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
205 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
206 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
207 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
208 setOperationAction(ISD::SREM , MVT::i16 , Expand);
209 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000210 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
211 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
212 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
213 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
214 setOperationAction(ISD::SREM , MVT::i32 , Expand);
215 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000216 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
217 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
218 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
219 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
220 setOperationAction(ISD::SREM , MVT::i64 , Expand);
221 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000222
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
224 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
225 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
226 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000228 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
229 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
230 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
232 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000233 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000235 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000236 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000237
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000239 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
240 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000242 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
243 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000245 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
246 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 if (Subtarget->is64Bit()) {
248 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000249 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
250 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 }
252
253 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
254 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
255
256 // These should be promoted to a larger select which is supported.
257 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
258 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
259 // X86 wants to expand cmov itself.
260 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
261 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
262 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
263 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000264 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
266 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
267 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
268 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
269 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000270 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 if (Subtarget->is64Bit()) {
272 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
273 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
274 }
275 // X86 ret instruction may pop stack.
276 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000277 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278
279 // Darwin ABI issue.
280 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
281 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
282 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
283 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000284 if (Subtarget->is64Bit())
285 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000286 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 if (Subtarget->is64Bit()) {
288 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
289 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
290 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000291 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 }
293 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
294 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
295 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
296 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000297 if (Subtarget->is64Bit()) {
298 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
299 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
300 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
301 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302
Evan Cheng8d51ab32008-03-10 19:38:10 +0000303 if (Subtarget->hasSSE1())
304 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000305
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000306 if (!Subtarget->hasSSE2())
307 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
308
Mon P Wang078a62d2008-05-05 19:05:59 +0000309 // Expand certain atomics
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000310 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
311 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
312 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
313 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000314
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000315 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
316 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
317 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
318 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000319
Dale Johannesenf160d802008-10-02 18:53:47 +0000320 if (!Subtarget->is64Bit()) {
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000321 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
322 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
323 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
324 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
325 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
326 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
327 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesenf160d802008-10-02 18:53:47 +0000328 }
329
Dan Gohman472d12c2008-06-30 20:59:49 +0000330 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
331 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 // FIXME - use subtarget debug flags
333 if (!Subtarget->isTargetDarwin() &&
334 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000335 !Subtarget->isTargetCygMing()) {
336 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
337 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
338 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339
340 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
341 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
342 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
343 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
344 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 setExceptionPointerRegister(X86::RAX);
346 setExceptionSelectorRegister(X86::RDX);
347 } else {
348 setExceptionPointerRegister(X86::EAX);
349 setExceptionSelectorRegister(X86::EDX);
350 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000351 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000352 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
353
Duncan Sands7407a9f2007-09-11 14:10:23 +0000354 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000355
Chris Lattner56b941f2008-01-15 21:58:22 +0000356 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000357
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
359 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000361 if (Subtarget->is64Bit()) {
362 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000364 } else {
365 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000367 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368
369 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
370 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
371 if (Subtarget->is64Bit())
372 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
373 if (Subtarget->isTargetCygMing())
374 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
375 else
376 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
377
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000378 if (X86ScalarSSEf64) {
379 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 // Set up the FP register classes.
381 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
382 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
383
384 // Use ANDPD to simulate FABS.
385 setOperationAction(ISD::FABS , MVT::f64, Custom);
386 setOperationAction(ISD::FABS , MVT::f32, Custom);
387
388 // Use XORP to simulate FNEG.
389 setOperationAction(ISD::FNEG , MVT::f64, Custom);
390 setOperationAction(ISD::FNEG , MVT::f32, Custom);
391
392 // Use ANDPD and ORPD to simulate FCOPYSIGN.
393 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
394 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
395
396 // We don't support sin/cos/fmod
397 setOperationAction(ISD::FSIN , MVT::f64, Expand);
398 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 setOperationAction(ISD::FSIN , MVT::f32, Expand);
400 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401
402 // Expand FP immediates into loads from the stack, except for the special
403 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000404 addLegalFPImmediate(APFloat(+0.0)); // xorpd
405 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000406
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000407 // Floating truncations from f80 and extensions to f80 go through memory.
408 // If optimizing, we lie about this though and handle it in
409 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
410 if (Fast) {
411 setConvertAction(MVT::f32, MVT::f80, Expand);
412 setConvertAction(MVT::f64, MVT::f80, Expand);
413 setConvertAction(MVT::f80, MVT::f32, Expand);
414 setConvertAction(MVT::f80, MVT::f64, Expand);
415 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000416 } else if (X86ScalarSSEf32) {
417 // Use SSE for f32, x87 for f64.
418 // Set up the FP register classes.
419 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
420 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
421
422 // Use ANDPS to simulate FABS.
423 setOperationAction(ISD::FABS , MVT::f32, Custom);
424
425 // Use XORP to simulate FNEG.
426 setOperationAction(ISD::FNEG , MVT::f32, Custom);
427
428 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
429
430 // Use ANDPS and ORPS to simulate FCOPYSIGN.
431 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
432 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
433
434 // We don't support sin/cos/fmod
435 setOperationAction(ISD::FSIN , MVT::f32, Expand);
436 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000437
Nate Begemane2ba64f2008-02-14 08:57:00 +0000438 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000439 addLegalFPImmediate(APFloat(+0.0f)); // xorps
440 addLegalFPImmediate(APFloat(+0.0)); // FLD0
441 addLegalFPImmediate(APFloat(+1.0)); // FLD1
442 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
443 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
444
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000445 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
446 // this though and handle it in InstructionSelectPreprocess so that
447 // dagcombine2 can hack on these.
448 if (Fast) {
449 setConvertAction(MVT::f32, MVT::f64, Expand);
450 setConvertAction(MVT::f32, MVT::f80, Expand);
451 setConvertAction(MVT::f80, MVT::f32, Expand);
452 setConvertAction(MVT::f64, MVT::f32, Expand);
453 // And x87->x87 truncations also.
454 setConvertAction(MVT::f80, MVT::f64, Expand);
455 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000456
457 if (!UnsafeFPMath) {
458 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
459 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
460 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000462 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 // Set up the FP register classes.
464 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
465 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
466
467 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
468 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
469 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
470 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000471
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000472 // Floating truncations go through memory. If optimizing, we lie about
473 // this though and handle it in InstructionSelectPreprocess so that
474 // dagcombine2 can hack on these.
475 if (Fast) {
476 setConvertAction(MVT::f80, MVT::f32, Expand);
477 setConvertAction(MVT::f64, MVT::f32, Expand);
478 setConvertAction(MVT::f80, MVT::f64, Expand);
479 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480
481 if (!UnsafeFPMath) {
482 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
483 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
484 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000485 addLegalFPImmediate(APFloat(+0.0)); // FLD0
486 addLegalFPImmediate(APFloat(+1.0)); // FLD1
487 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
488 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000489 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
490 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
491 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
492 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 }
494
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000495 // Long double always uses X87.
496 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000497 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
498 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000499 {
Dale Johannesen6e547b42008-10-09 23:00:39 +0000500 bool ignored;
Chris Lattnerdd867392008-01-27 06:19:31 +0000501 APFloat TmpFlt(+0.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000502 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
503 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000504 addLegalFPImmediate(TmpFlt); // FLD0
505 TmpFlt.changeSign();
506 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
507 APFloat TmpFlt2(+1.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000508 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
509 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000510 addLegalFPImmediate(TmpFlt2); // FLD1
511 TmpFlt2.changeSign();
512 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
513 }
514
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000515 if (!UnsafeFPMath) {
516 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
517 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
518 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000519
Dan Gohman2f7b1982007-10-11 23:21:31 +0000520 // Always use a library call for pow.
521 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
522 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
523 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
524
Dale Johannesen92b33082008-09-04 00:47:13 +0000525 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000526 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000527 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000528 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000529 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
530
Mon P Wanga5a239f2008-11-06 05:31:54 +0000531 // First set operation action for all vector types to either promote
Mon P Wang1448aad2008-10-30 08:01:45 +0000532 // (for widening) or expand (for scalarization). Then we will selectively
533 // turn on ones that can be effectively codegen'd.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
535 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000536 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif825aa892008-08-28 23:19:51 +0000549 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
551 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands92c43912008-06-06 12:08:01 +0000552 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesen177edff2008-09-10 17:31:40 +0000574 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 }
580
Mon P Wang1f292322008-11-23 04:37:22 +0000581 if (!DisableMMX && Subtarget->hasMMX()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
583 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
584 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000585 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
587
588 // FIXME: add MMX packed arithmetics
589
590 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
591 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
592 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
593 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
594
595 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
596 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
597 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000598 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599
600 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
601 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
602
603 setOperationAction(ISD::AND, MVT::v8i8, Promote);
604 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
605 setOperationAction(ISD::AND, MVT::v4i16, Promote);
606 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
607 setOperationAction(ISD::AND, MVT::v2i32, Promote);
608 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
609 setOperationAction(ISD::AND, MVT::v1i64, Legal);
610
611 setOperationAction(ISD::OR, MVT::v8i8, Promote);
612 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
613 setOperationAction(ISD::OR, MVT::v4i16, Promote);
614 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
615 setOperationAction(ISD::OR, MVT::v2i32, Promote);
616 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
617 setOperationAction(ISD::OR, MVT::v1i64, Legal);
618
619 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
620 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
621 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
622 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
623 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
624 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
625 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
626
627 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
628 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
629 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
630 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
631 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
632 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000633 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
634 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
636
637 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
638 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
639 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000640 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
642
643 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
644 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
645 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
646 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
647
Evan Cheng759fe022008-07-22 18:39:19 +0000648 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
650 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000652
653 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang83edba52008-12-12 01:25:51 +0000654
655 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
656 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
657 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
658 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
659 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
660 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661 }
662
663 if (Subtarget->hasSSE1()) {
664 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
665
666 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
667 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
668 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
669 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
670 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
671 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
673 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
674 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
675 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
676 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000677 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 }
679
680 if (Subtarget->hasSSE2()) {
681 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
682 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
683 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
684 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
685 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
686
687 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
688 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
689 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
690 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wang14edb092008-12-18 21:42:19 +0000691 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
693 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
694 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
695 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
696 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
697 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
698 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
699 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
700 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
701 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
702 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703
Nate Begeman03605a02008-07-17 16:51:19 +0000704 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
705 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
706 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
707 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000708
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
710 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
711 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
712 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
714
715 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000716 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
717 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000718 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000719 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000720 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000721 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
722 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
723 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 }
725 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
726 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
727 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
728 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000729 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000731 if (Subtarget->is64Bit()) {
732 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000733 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000734 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735
736 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
737 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000738 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
739 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
740 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
741 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
742 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
743 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
744 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
745 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
746 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
747 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748 }
749
Chris Lattner3bc08502008-01-17 19:59:44 +0000750 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000751
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 // Custom lower v2i64 and v2f64 selects.
753 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
754 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
755 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
756 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000757
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000759
760 if (Subtarget->hasSSE41()) {
761 // FIXME: Do we need to handle scalar-to-vector here?
762 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
763
764 // i8 and i16 vectors are custom , because the source register and source
765 // source memory operand types are not the same width. f32 vectors are
766 // custom since the immediate controlling the insert encodes additional
767 // information.
768 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
769 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangac2a3c52009-01-15 21:10:20 +0000770 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000771 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
772
773 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
774 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangac2a3c52009-01-15 21:10:20 +0000775 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng6c249332008-03-24 21:52:23 +0000776 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000777
778 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000779 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
780 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000781 }
782 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000783
Nate Begeman03605a02008-07-17 16:51:19 +0000784 if (Subtarget->hasSSE42()) {
785 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
786 }
787
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788 // We want to custom lower some of our intrinsics.
789 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
790
Bill Wendling7e04be62008-12-09 22:08:41 +0000791 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling4c134df2008-11-24 19:21:46 +0000792 setOperationAction(ISD::SADDO, MVT::i32, Custom);
793 setOperationAction(ISD::SADDO, MVT::i64, Custom);
794 setOperationAction(ISD::UADDO, MVT::i32, Custom);
795 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling7e04be62008-12-09 22:08:41 +0000796 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
797 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
798 setOperationAction(ISD::USUBO, MVT::i32, Custom);
799 setOperationAction(ISD::USUBO, MVT::i64, Custom);
800 setOperationAction(ISD::SMULO, MVT::i32, Custom);
801 setOperationAction(ISD::SMULO, MVT::i64, Custom);
802 setOperationAction(ISD::UMULO, MVT::i32, Custom);
803 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Bill Wendling4c134df2008-11-24 19:21:46 +0000804
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 // We have target-specific dag combine patterns for the following nodes:
806 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000807 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 setTargetDAGCombine(ISD::SELECT);
sampo025b75c2009-01-26 00:52:55 +0000809 setTargetDAGCombine(ISD::SHL);
810 setTargetDAGCombine(ISD::SRA);
811 setTargetDAGCombine(ISD::SRL);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000812 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000813
814 computeRegisterProperties();
815
816 // FIXME: These should be based on subtarget info. Plus, the values should
817 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000818 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
819 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
820 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000822 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823}
824
Scott Michel502151f2008-03-10 15:42:14 +0000825
Duncan Sands4a361272009-01-01 15:52:00 +0000826MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel502151f2008-03-10 15:42:14 +0000827 return MVT::i8;
828}
829
830
Evan Cheng5a67b812008-01-23 23:17:41 +0000831/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
832/// the desired ByVal argument alignment.
833static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
834 if (MaxAlign == 16)
835 return;
836 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
837 if (VTy->getBitWidth() == 128)
838 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000839 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
840 unsigned EltAlign = 0;
841 getMaxByValAlign(ATy->getElementType(), EltAlign);
842 if (EltAlign > MaxAlign)
843 MaxAlign = EltAlign;
844 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
845 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
846 unsigned EltAlign = 0;
847 getMaxByValAlign(STy->getElementType(i), EltAlign);
848 if (EltAlign > MaxAlign)
849 MaxAlign = EltAlign;
850 if (MaxAlign == 16)
851 break;
852 }
853 }
854 return;
855}
856
857/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
858/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000859/// that contain SSE vectors are placed at 16-byte boundaries while the rest
860/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000861unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000862 if (Subtarget->is64Bit()) {
863 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000864 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000865 if (TyAlign > 8)
866 return TyAlign;
867 return 8;
868 }
869
Evan Cheng5a67b812008-01-23 23:17:41 +0000870 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000871 if (Subtarget->hasSSE1())
872 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000873 return Align;
874}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875
Evan Cheng8c590372008-05-15 08:39:06 +0000876/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000877/// and store operations as a result of memset, memcpy, and memmove
878/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000879/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000880MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000881X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
882 bool isSrcConst, bool isSrcStr) const {
Chris Lattnerf0bf1062008-10-28 05:49:35 +0000883 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
884 // linux. This is because the stack realignment code can't handle certain
885 // cases like PR2962. This should be removed when PR2962 is fixed.
886 if (Subtarget->getStackAlignment() >= 16) {
887 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
888 return MVT::v4i32;
889 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
890 return MVT::v4f32;
891 }
Evan Cheng8c590372008-05-15 08:39:06 +0000892 if (Subtarget->is64Bit() && Size >= 8)
893 return MVT::i64;
894 return MVT::i32;
895}
896
897
Evan Cheng6fb06762007-11-09 01:32:10 +0000898/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
899/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000900SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000901 SelectionDAG &DAG) const {
902 if (usesGlobalOffsetTable())
903 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
904 if (!Subtarget->isPICStyleRIPRel())
905 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
906 return Table;
907}
908
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909//===----------------------------------------------------------------------===//
910// Return Value Calling Convention Implementation
911//===----------------------------------------------------------------------===//
912
913#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000914
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000916SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +0000917 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
919
920 SmallVector<CCValAssign, 16> RVLocs;
921 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
922 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
923 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000924 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000925
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000926 // If this is the first return lowered for this function, add the regs to the
927 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000928 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 for (unsigned i = 0; i != RVLocs.size(); ++i)
930 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000931 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000933 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000934
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000935 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000936 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000937 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000938 SDValue TailCall = Chain;
939 SDValue TargetAddress = TailCall.getOperand(1);
940 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000941 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000942 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000943 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000944 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000945 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
946 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000947 assert(StackAdjustment.getOpcode() == ISD::Constant &&
948 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000949
Dan Gohman8181bd12008-07-27 21:46:04 +0000950 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000951 Operands.push_back(Chain.getOperand(0));
952 Operands.push_back(TargetAddress);
953 Operands.push_back(StackAdjustment);
954 // Copy registers used by the call. Last operand is a flag so it is not
955 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000956 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000957 Operands.push_back(Chain.getOperand(i));
958 }
Dale Johannesence0805b2009-02-03 19:33:06 +0000959 return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000960 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000961 }
962
963 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000964 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000965
Dan Gohman8181bd12008-07-27 21:46:04 +0000966 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000967 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
968 // Operand #1 = Bytes To Pop
969 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
970
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000972 for (unsigned i = 0; i != RVLocs.size(); ++i) {
973 CCValAssign &VA = RVLocs[i];
974 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000975 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976
Chris Lattnerb56cc342008-03-11 03:23:40 +0000977 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
978 // the RET instruction and handled by the FP Stackifier.
979 if (RVLocs[i].getLocReg() == X86::ST0 ||
980 RVLocs[i].getLocReg() == X86::ST1) {
981 // If this is a copy from an xmm register to ST(0), use an FPExtend to
982 // change the value to the FP stack register class.
983 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
Dale Johannesence0805b2009-02-03 19:33:06 +0000984 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattnerb56cc342008-03-11 03:23:40 +0000985 RetOps.push_back(ValToCopy);
986 // Don't emit a copytoreg.
987 continue;
988 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000989
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +0000990 Chain = DAG.getCopyToReg(Chain, dl, 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 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001007 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001008
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001009 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001010 Flag = Chain.getValue(1);
1011 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012
Chris Lattnerb56cc342008-03-11 03:23:40 +00001013 RetOps[0] = Chain; // Update chain.
1014
1015 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +00001016 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +00001017 RetOps.push_back(Flag);
1018
Dale Johannesence0805b2009-02-03 19:33:06 +00001019 return DAG.getNode(X86ISD::RET_FLAG, dl,
1020 MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001021}
1022
1023
1024/// LowerCallResult - Lower the result values of an ISD::CALL into the
1025/// appropriate copies out of appropriate physical registers. This assumes that
1026/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1027/// being lowered. The returns a SDNode with the same number of values as the
1028/// ISD::CALL.
1029SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +00001030LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031 unsigned CallingConv, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00001032
1033 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001034 // Assign locations to each value returned by this call.
1035 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +00001036 bool isVarArg = TheCall->isVarArg();
Edwin Törökaf8e1332009-02-01 18:15:56 +00001037 bool Is64Bit = Subtarget->is64Bit();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001038 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
1039 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1040
Dan Gohman8181bd12008-07-27 21:46:04 +00001041 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001042
1043 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001044 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00001045 MVT CopyVT = RVLocs[i].getValVT();
Edwin Törökaf8e1332009-02-01 18:15:56 +00001046
1047 // If this is x86-64, and we disabled SSE, we can't return FP values
1048 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
1049 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
1050 cerr << "SSE register return with SSE disabled\n";
1051 exit(1);
1052 }
1053
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001054 // If this is a call to a function that returns an fp value on the floating
1055 // point stack, but where we prefer to use the value in xmm registers, copy
1056 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Mon P Wang73a2c152008-08-21 19:54:16 +00001057 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1058 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001059 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1060 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001063 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001064 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001065 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001066 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001067
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001068 if (CopyVT != RVLocs[i].getValVT()) {
1069 // Round the F80 the right size, which also moves to the appropriate xmm
1070 // register.
Dale Johannesence0805b2009-02-03 19:33:06 +00001071 Val = DAG.getNode(ISD::FP_ROUND, dl, RVLocs[i].getValVT(), Val,
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001072 // This truncation won't change the value.
1073 DAG.getIntPtrConstant(1));
1074 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001075
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001076 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001077 }
Duncan Sands698842f2008-07-02 17:40:58 +00001078
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001079 // Merge everything together with a MERGE_VALUES node.
1080 ResultVals.push_back(Chain);
Dale Johannesence0805b2009-02-03 19:33:06 +00001081 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
1082 &ResultVals[0], ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001083}
1084
1085
1086//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001087// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001088//===----------------------------------------------------------------------===//
1089// StdCall calling convention seems to be standard for many Windows' API
1090// routines and around. It differs from C calling convention just a little:
1091// callee should clean up the stack, not caller. Symbols should be also
1092// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001093// For info on fast calling convention see Fast Calling Convention (tail call)
1094// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001095
1096/// AddLiveIn - This helper function adds the specified physical register to the
1097/// MachineFunction as a live in value. It also creates a corresponding virtual
1098/// register for it.
1099static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1100 const TargetRegisterClass *RC) {
1101 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001102 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1103 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001104 return VReg;
1105}
1106
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001107/// CallIsStructReturn - Determines whether a CALL node uses struct return
1108/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001109static bool CallIsStructReturn(CallSDNode *TheCall) {
1110 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001111 if (!NumOps)
1112 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001113
Dan Gohman705e3f72008-09-13 01:54:27 +00001114 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001115}
1116
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001117/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1118/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001119static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001120 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001121 if (!NumArgs)
1122 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001123
1124 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001125}
1126
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001127/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1128/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001129/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001130bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001131 if (IsVarArg)
1132 return false;
1133
Dan Gohman705e3f72008-09-13 01:54:27 +00001134 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001135 default:
1136 return false;
1137 case CallingConv::X86_StdCall:
1138 return !Subtarget->is64Bit();
1139 case CallingConv::X86_FastCall:
1140 return !Subtarget->is64Bit();
1141 case CallingConv::Fast:
1142 return PerformTailCallOpt;
1143 }
1144}
1145
Dan Gohman705e3f72008-09-13 01:54:27 +00001146/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1147/// given CallingConvention value.
1148CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001149 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001150 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001151 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001152 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1153 return CC_X86_64_TailCall;
1154 else
1155 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001156 }
1157
Gordon Henriksen18ace102008-01-05 16:56:59 +00001158 if (CC == CallingConv::X86_FastCall)
1159 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001160 else if (CC == CallingConv::Fast)
1161 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001162 else
1163 return CC_X86_32_C;
1164}
1165
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001166/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1167/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001168NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001169X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001170 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001171 if (CC == CallingConv::X86_FastCall)
1172 return FastCall;
1173 else if (CC == CallingConv::X86_StdCall)
1174 return StdCall;
1175 return None;
1176}
1177
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001178
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001179/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1180/// in a register before calling.
1181bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1182 return !IsTailCall && !Is64Bit &&
1183 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1184 Subtarget->isPICStyleGOT();
1185}
1186
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001187/// CallRequiresFnAddressInReg - Check whether the call requires the function
1188/// address to be loaded in a register.
1189bool
1190X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1191 return !Is64Bit && IsTailCall &&
1192 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1193 Subtarget->isPICStyleGOT();
1194}
1195
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001196/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1197/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001198/// the specific parameter attribute. The copy will be passed as a byval
1199/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001200static SDValue
1201CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001202 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1203 DebugLoc dl) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001204 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001205 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001206 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001207}
1208
Dan Gohman8181bd12008-07-27 21:46:04 +00001209SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001210 const CCValAssign &VA,
1211 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001212 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001213 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001214 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001215 ISD::ArgFlagsTy Flags =
1216 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001217 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001218 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001219
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001220 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1221 // changed with more analysis.
1222 // In case of tail call optimization mark all arguments mutable. Since they
1223 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001224 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001225 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001226 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001227 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001228 return FIN;
Dale Johannesence0805b2009-02-03 19:33:06 +00001229 return DAG.getLoad(VA.getValVT(), Op.getNode()->getDebugLoc(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001230 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001231}
1232
Dan Gohman8181bd12008-07-27 21:46:04 +00001233SDValue
1234X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001235 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001236 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Dale Johannesence0805b2009-02-03 19:33:06 +00001237 DebugLoc dl = Op.getNode()->getDebugLoc();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001238
1239 const Function* Fn = MF.getFunction();
1240 if (Fn->hasExternalLinkage() &&
1241 Subtarget->isTargetCygMing() &&
1242 Fn->getName() == "main")
1243 FuncInfo->setForceFramePointer(true);
1244
1245 // Decorate the function name.
1246 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1247
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001248 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001249 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001250 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001251 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001252 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001253 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001254
1255 assert(!(isVarArg && CC == CallingConv::Fast) &&
1256 "Var args not supported with calling convention fastcc");
1257
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001258 // Assign locations to all of the incoming arguments.
1259 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001260 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001261 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001262
Dan Gohman8181bd12008-07-27 21:46:04 +00001263 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264 unsigned LastVal = ~0U;
1265 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1266 CCValAssign &VA = ArgLocs[i];
1267 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1268 // places.
1269 assert(VA.getValNo() != LastVal &&
1270 "Don't support value assigned to multiple locs yet");
1271 LastVal = VA.getValNo();
1272
1273 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001274 MVT RegVT = VA.getLocVT();
Devang Patelf3707e82009-01-05 17:31:22 +00001275 TargetRegisterClass *RC = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001276 if (RegVT == MVT::i32)
1277 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001278 else if (Is64Bit && RegVT == MVT::i64)
1279 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001280 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001281 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001282 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001283 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001284 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001285 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001286 else if (RegVT.isVector()) {
1287 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001288 if (!Is64Bit)
1289 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1290 else {
1291 // Darwin calling convention passes MMX values in either GPRs or
1292 // XMMs in x86-64. Other targets pass them in memory.
1293 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1294 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1295 RegVT = MVT::v2i64;
1296 } else {
1297 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1298 RegVT = MVT::i64;
1299 }
1300 }
1301 } else {
1302 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001303 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001304
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001305 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001306 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001307
1308 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1309 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1310 // right size.
1311 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesence0805b2009-02-03 19:33:06 +00001312 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001313 DAG.getValueType(VA.getValVT()));
1314 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesence0805b2009-02-03 19:33:06 +00001315 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316 DAG.getValueType(VA.getValVT()));
1317
1318 if (VA.getLocInfo() != CCValAssign::Full)
Dale Johannesence0805b2009-02-03 19:33:06 +00001319 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320
Gordon Henriksen18ace102008-01-05 16:56:59 +00001321 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001322 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001323 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Dale Johannesence0805b2009-02-03 19:33:06 +00001324 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Chengad6980b2008-04-25 20:13:28 +00001325 else if (RC == X86::VR128RegisterClass) {
Dale Johannesence0805b2009-02-03 19:33:06 +00001326 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1327 ArgValue, DAG.getConstant(0, MVT::i64));
1328 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Chengad6980b2008-04-25 20:13:28 +00001329 }
1330 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001331
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001332 ArgValues.push_back(ArgValue);
1333 } else {
1334 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001335 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001336 }
1337 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001338
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001339 // The x86-64 ABI for returning structs by value requires that we copy
1340 // the sret argument into %rax for the return. Save the argument into
1341 // a virtual register so that we can access it from the return points.
1342 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1343 MachineFunction &MF = DAG.getMachineFunction();
1344 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1345 unsigned Reg = FuncInfo->getSRetReturnReg();
1346 if (!Reg) {
1347 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1348 FuncInfo->setSRetReturnReg(Reg);
1349 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001350 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
Dale Johannesence0805b2009-02-03 19:33:06 +00001351 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001352 }
1353
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001354 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001355 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001356 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001357 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001358
1359 // If the function takes variable number of arguments, make a frame index for
1360 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001361 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001362 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1363 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1364 }
1365 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001366 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1367
1368 // FIXME: We should really autogenerate these arrays
1369 static const unsigned GPR64ArgRegsWin64[] = {
1370 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001371 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001372 static const unsigned XMMArgRegsWin64[] = {
1373 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1374 };
1375 static const unsigned GPR64ArgRegs64Bit[] = {
1376 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1377 };
1378 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001379 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1380 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1381 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001382 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1383
1384 if (IsWin64) {
1385 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1386 GPR64ArgRegs = GPR64ArgRegsWin64;
1387 XMMArgRegs = XMMArgRegsWin64;
1388 } else {
1389 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1390 GPR64ArgRegs = GPR64ArgRegs64Bit;
1391 XMMArgRegs = XMMArgRegs64Bit;
1392 }
1393 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1394 TotalNumIntRegs);
1395 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1396 TotalNumXMMRegs);
1397
Edwin Törökaf8e1332009-02-01 18:15:56 +00001398 assert((Subtarget->hasSSE1() || !NumXMMRegs) &&
1399 "SSE register cannot be used when SSE is disabled!");
1400 if (!Subtarget->hasSSE1()) {
1401 // Kernel mode asks for SSE to be disabled, so don't push them
1402 // on the stack.
1403 TotalNumXMMRegs = 0;
1404 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001405 // For X86-64, if there are vararg parameters that are passed via
1406 // registers, then we must store them to their spots on the stack so they
1407 // may be loaded by deferencing the result of va_next.
1408 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001409 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1410 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1411 TotalNumXMMRegs * 16, 16);
1412
Gordon Henriksen18ace102008-01-05 16:56:59 +00001413 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001414 SmallVector<SDValue, 8> MemOps;
1415 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00001416 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001417 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001418 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001419 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1420 X86::GR64RegisterClass);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001421 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64);
Dan Gohman8181bd12008-07-27 21:46:04 +00001422 SDValue Store =
Dale Johannesence0805b2009-02-03 19:33:06 +00001423 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001424 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001425 MemOps.push_back(Store);
Dale Johannesence0805b2009-02-03 19:33:06 +00001426 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001427 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001428 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001429
Gordon Henriksen18ace102008-01-05 16:56:59 +00001430 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesence0805b2009-02-03 19:33:06 +00001431 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001432 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001433 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001434 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1435 X86::VR128RegisterClass);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001436 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::v4f32);
Dan Gohman8181bd12008-07-27 21:46:04 +00001437 SDValue Store =
Dale Johannesence0805b2009-02-03 19:33:06 +00001438 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001439 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001440 MemOps.push_back(Store);
Dale Johannesence0805b2009-02-03 19:33:06 +00001441 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001442 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001443 }
1444 if (!MemOps.empty())
Dale Johannesence0805b2009-02-03 19:33:06 +00001445 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Gordon Henriksen18ace102008-01-05 16:56:59 +00001446 &MemOps[0], MemOps.size());
1447 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001448 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001449
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001450 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001451
Gordon Henriksen18ace102008-01-05 16:56:59 +00001452 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001453 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001454 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001455 BytesCallerReserves = 0;
1456 } else {
1457 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001458 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001459 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001460 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001461 BytesCallerReserves = StackSize;
1462 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001463
Gordon Henriksen18ace102008-01-05 16:56:59 +00001464 if (!Is64Bit) {
1465 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1466 if (CC == CallingConv::X86_FastCall)
1467 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1468 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001469
Anton Korobeynikove844e472007-08-15 17:12:32 +00001470 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001471
1472 // Return the new list of results.
Dale Johannesence0805b2009-02-03 19:33:06 +00001473 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sands42d7bb82008-12-01 11:41:29 +00001474 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001475}
1476
Dan Gohman8181bd12008-07-27 21:46:04 +00001477SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001478X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001479 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001480 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001481 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001482 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dale Johannesence0805b2009-02-03 19:33:06 +00001483 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohman1190f3a2008-02-07 16:28:05 +00001484 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001485 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesence0805b2009-02-03 19:33:06 +00001486 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001487 if (Flags.isByVal()) {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001488 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengbc077bf2008-01-10 00:09:10 +00001489 }
Dale Johannesence0805b2009-02-03 19:33:06 +00001490 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001491 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001492}
1493
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001494/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001495/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001496SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001497X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001498 SDValue &OutRetAddr,
1499 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001500 bool IsTailCall,
1501 bool Is64Bit,
Dale Johannesence0805b2009-02-03 19:33:06 +00001502 int FPDiff,
1503 DebugLoc dl) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001504 if (!IsTailCall || FPDiff==0) return Chain;
1505
1506 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001507 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001508 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001509
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001510 // Load the "old" Return address.
Dale Johannesence0805b2009-02-03 19:33:06 +00001511 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001512 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001513}
1514
1515/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1516/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001517static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001518EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001519 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesence0805b2009-02-03 19:33:06 +00001520 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001521 // Store the return address to the appropriate stack slot.
1522 if (!FPDiff) return Chain;
1523 // Calculate the new stack slot for the return address.
1524 int SlotSize = Is64Bit ? 8 : 4;
1525 int NewReturnAddrFI =
1526 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001527 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001528 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Dale Johannesence0805b2009-02-03 19:33:06 +00001529 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001530 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001531 return Chain;
1532}
1533
Dan Gohman8181bd12008-07-27 21:46:04 +00001534SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001535 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001536 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1537 SDValue Chain = TheCall->getChain();
1538 unsigned CC = TheCall->getCallingConv();
1539 bool isVarArg = TheCall->isVarArg();
1540 bool IsTailCall = TheCall->isTailCall() &&
1541 CC == CallingConv::Fast && PerformTailCallOpt;
1542 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001543 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001544 bool IsStructRet = CallIsStructReturn(TheCall);
Dale Johannesence0805b2009-02-03 19:33:06 +00001545 DebugLoc dl = TheCall->getDebugLoc();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001546
1547 assert(!(isVarArg && CC == CallingConv::Fast) &&
1548 "Var args not supported with calling convention fastcc");
1549
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001550 // Analyze operands of the call, assigning locations to each operand.
1551 SmallVector<CCValAssign, 16> ArgLocs;
1552 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001553 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001554
1555 // Get a count of how many bytes are to be pushed on the stack.
1556 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001557 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001558 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001559
Gordon Henriksen18ace102008-01-05 16:56:59 +00001560 int FPDiff = 0;
1561 if (IsTailCall) {
1562 // Lower arguments at fp - stackoffset + fpdiff.
1563 unsigned NumBytesCallerPushed =
1564 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1565 FPDiff = NumBytesCallerPushed - NumBytes;
1566
1567 // Set the delta of movement of the returnaddr stackslot.
1568 // But only set if delta is greater than previous delta.
1569 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1570 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1571 }
1572
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001573 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001574
Dan Gohman8181bd12008-07-27 21:46:04 +00001575 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001576 // Load return adress for tail calls.
1577 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
Dale Johannesence0805b2009-02-03 19:33:06 +00001578 FPDiff, dl);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001579
Dan Gohman8181bd12008-07-27 21:46:04 +00001580 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1581 SmallVector<SDValue, 8> MemOpChains;
1582 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001583
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001584 // Walk the register/memloc assignments, inserting copies/loads. In the case
1585 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001586 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1587 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001588 SDValue Arg = TheCall->getArg(i);
1589 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1590 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001591
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001592 // Promote the value if needed.
1593 switch (VA.getLocInfo()) {
1594 default: assert(0 && "Unknown loc info!");
1595 case CCValAssign::Full: break;
1596 case CCValAssign::SExt:
Dale Johannesence0805b2009-02-03 19:33:06 +00001597 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001598 break;
1599 case CCValAssign::ZExt:
Dale Johannesence0805b2009-02-03 19:33:06 +00001600 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001601 break;
1602 case CCValAssign::AExt:
Dale Johannesence0805b2009-02-03 19:33:06 +00001603 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001604 break;
1605 }
1606
1607 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001608 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001609 MVT RegVT = VA.getLocVT();
1610 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001611 switch (VA.getLocReg()) {
1612 default:
1613 break;
1614 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1615 case X86::R8: {
1616 // Special case: passing MMX values in GPR registers.
Dale Johannesence0805b2009-02-03 19:33:06 +00001617 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
Evan Cheng2aea0b42008-04-25 19:11:04 +00001618 break;
1619 }
1620 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1621 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1622 // Special case: passing MMX values in XMM registers.
Dale Johannesence0805b2009-02-03 19:33:06 +00001623 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1624 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1625 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v2i64,
1626 DAG.getNode(ISD::UNDEF, dl, MVT::v2i64), Arg,
1627 getMOVLMask(2, DAG, dl));
Evan Cheng2aea0b42008-04-25 19:11:04 +00001628 break;
1629 }
1630 }
1631 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001632 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1633 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001634 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001635 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001636 if (StackPtr.getNode() == 0)
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001637 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001638
Dan Gohman705e3f72008-09-13 01:54:27 +00001639 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1640 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001641 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001642 }
1643 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001644
1645 if (!MemOpChains.empty())
Dale Johannesence0805b2009-02-03 19:33:06 +00001646 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001647 &MemOpChains[0], MemOpChains.size());
1648
1649 // Build a sequence of copy-to-reg nodes chained together with token chain
1650 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001651 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001652 // Tail call byval lowering might overwrite argument registers so in case of
1653 // tail call optimization the copies to registers are lowered later.
1654 if (!IsTailCall)
1655 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001656 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1657 RegsToPass[i].second, InFlag);
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001658 InFlag = Chain.getValue(1);
1659 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001660
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001661 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001662 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001663 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001664 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001665 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1666 InFlag);
1667 InFlag = Chain.getValue(1);
1668 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001669 // If we are tail calling and generating PIC/GOT style code load the address
1670 // of the callee into ecx. The value in ecx is used as target of the tail
1671 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1672 // calls on PIC/GOT architectures. Normally we would just put the address of
1673 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1674 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001675 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001676 // Note: The actual moving to ecx is done further down.
1677 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001678 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001679 !G->getGlobal()->hasProtectedVisibility())
1680 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001681 else if (isa<ExternalSymbolSDNode>(Callee))
1682 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001683 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001684
Gordon Henriksen18ace102008-01-05 16:56:59 +00001685 if (Is64Bit && isVarArg) {
1686 // From AMD64 ABI document:
1687 // For calls that may call functions that use varargs or stdargs
1688 // (prototype-less calls or calls to functions containing ellipsis (...) in
1689 // the declaration) %al is used as hidden argument to specify the number
1690 // of SSE registers used. The contents of %al do not need to match exactly
1691 // the number of registers, but must be an ubound on the number of SSE
1692 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001693
1694 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001695 // Count the number of XMM registers allocated.
1696 static const unsigned XMMArgRegs[] = {
1697 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1698 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1699 };
1700 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Edwin Törökaf8e1332009-02-01 18:15:56 +00001701 assert((Subtarget->hasSSE1() || !NumXMMRegs)
1702 && "SSE registers cannot be used when SSE is disabled");
Gordon Henriksen18ace102008-01-05 16:56:59 +00001703
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001704 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Gordon Henriksen18ace102008-01-05 16:56:59 +00001705 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1706 InFlag = Chain.getValue(1);
1707 }
1708
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001709
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001710 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001711 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001712 SmallVector<SDValue, 8> MemOpChains2;
1713 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001714 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001715 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001716 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001717 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1718 CCValAssign &VA = ArgLocs[i];
1719 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001720 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001721 SDValue Arg = TheCall->getArg(i);
1722 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001723 // Create frame index.
1724 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001725 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001726 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001727 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001728
Duncan Sandsc93fae32008-03-21 09:14:45 +00001729 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001730 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001731 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001732 if (StackPtr.getNode() == 0)
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001733 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
1734 getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00001735 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001736
1737 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001738 Flags, DAG, dl));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001739 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001740 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001741 MemOpChains2.push_back(
Dale Johannesence0805b2009-02-03 19:33:06 +00001742 DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001743 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001744 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001745 }
1746 }
1747
1748 if (!MemOpChains2.empty())
Dale Johannesence0805b2009-02-03 19:33:06 +00001749 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001750 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001751
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001752 // Copy arguments to their registers.
1753 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001754 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1755 RegsToPass[i].second, InFlag);
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001756 InFlag = Chain.getValue(1);
1757 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001758 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001759
Gordon Henriksen18ace102008-01-05 16:56:59 +00001760 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001761 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesence0805b2009-02-03 19:33:06 +00001762 FPDiff, dl);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001763 }
1764
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001765 // If the callee is a GlobalAddress node (quite common, every direct call is)
1766 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1767 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1768 // We should use extra load for direct calls to dllimported functions in
1769 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001770 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1771 getTargetMachine(), true))
Dan Gohman36322c72008-10-18 02:06:02 +00001772 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1773 G->getOffset());
Bill Wendlingfef06052008-09-16 21:48:12 +00001774 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1775 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001776 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001777 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001778
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001779 Chain = DAG.getCopyToReg(Chain, dl,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001780 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001781 Callee,InFlag);
1782 Callee = DAG.getRegister(Opc, getPointerTy());
1783 // Add register as live out.
1784 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001785 }
1786
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001787 // Returns a chain & a flag for retval copy to use.
1788 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001789 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001790
1791 if (IsTailCall) {
1792 Ops.push_back(Chain);
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001793 Ops.push_back(DAG.getIntPtrConstant(NumBytes, true));
1794 Ops.push_back(DAG.getIntPtrConstant(0, true));
Gabor Greif1c80d112008-08-28 21:40:38 +00001795 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001796 Ops.push_back(InFlag);
1797 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1798 InFlag = Chain.getValue(1);
1799
1800 // Returns a chain & a flag for retval copy to use.
1801 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1802 Ops.clear();
1803 }
1804
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001805 Ops.push_back(Chain);
1806 Ops.push_back(Callee);
1807
Gordon Henriksen18ace102008-01-05 16:56:59 +00001808 if (IsTailCall)
1809 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001810
Gordon Henriksen18ace102008-01-05 16:56:59 +00001811 // Add argument registers to the end of the list so that they are known live
1812 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001813 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1814 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1815 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001816
Evan Cheng8ba45e62008-03-18 23:36:35 +00001817 // Add an implicit use GOT pointer in EBX.
1818 if (!IsTailCall && !Is64Bit &&
1819 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1820 Subtarget->isPICStyleGOT())
1821 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1822
1823 // Add an implicit use of AL for x86 vararg functions.
1824 if (Is64Bit && isVarArg)
1825 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1826
Gabor Greif1c80d112008-08-28 21:40:38 +00001827 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001828 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001829
Gordon Henriksen18ace102008-01-05 16:56:59 +00001830 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001831 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001832 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesence0805b2009-02-03 19:33:06 +00001833 Chain = DAG.getNode(X86ISD::TAILCALL, dl,
Dan Gohman705e3f72008-09-13 01:54:27 +00001834 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001835
Gabor Greif1c80d112008-08-28 21:40:38 +00001836 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001837 }
1838
Dale Johannesence0805b2009-02-03 19:33:06 +00001839 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001840 InFlag = Chain.getValue(1);
1841
1842 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001843 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001844 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001845 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001846 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001847 // If this is is a call to a struct-return function, the callee
1848 // pops the hidden struct pointer, so we have to push it back.
1849 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001850 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001851 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001852 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001853
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001854 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001855 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001856 DAG.getIntPtrConstant(NumBytes, true),
1857 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1858 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001859 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001860 InFlag = Chain.getValue(1);
1861
1862 // Handle result values, copying them out of physregs into vregs that we
1863 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001864 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001865 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001866}
1867
1868
1869//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001870// Fast Calling Convention (tail call) implementation
1871//===----------------------------------------------------------------------===//
1872
1873// Like std call, callee cleans arguments, convention except that ECX is
1874// reserved for storing the tail called function address. Only 2 registers are
1875// free for argument passing (inreg). Tail call optimization is performed
1876// provided:
1877// * tailcallopt is enabled
1878// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001879// On X86_64 architecture with GOT-style position independent code only local
1880// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001881// To keep the stack aligned according to platform abi the function
1882// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1883// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001884// If a tail called function callee has more arguments than the caller the
1885// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001886// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001887// original REtADDR, but before the saved framepointer or the spilled registers
1888// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1889// stack layout:
1890// arg1
1891// arg2
1892// RETADDR
1893// [ new RETADDR
1894// move area ]
1895// (possible EBP)
1896// ESI
1897// EDI
1898// local1 ..
1899
1900/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1901/// for a 16 byte align requirement.
1902unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1903 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001904 MachineFunction &MF = DAG.getMachineFunction();
1905 const TargetMachine &TM = MF.getTarget();
1906 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1907 unsigned StackAlignment = TFI.getStackAlignment();
1908 uint64_t AlignMask = StackAlignment - 1;
1909 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001910 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001911 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1912 // Number smaller than 12 so just add the difference.
1913 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1914 } else {
1915 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1916 Offset = ((~AlignMask) & Offset) + StackAlignment +
1917 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001918 }
Evan Chengded8f902008-09-07 09:07:23 +00001919 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001920}
1921
1922/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001923/// following the call is a return. A function is eligible if caller/callee
1924/// calling conventions match, currently only fastcc supports tail calls, and
1925/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001926bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001927 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001928 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001929 if (!PerformTailCallOpt)
1930 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001931
Dan Gohman705e3f72008-09-13 01:54:27 +00001932 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001933 MachineFunction &MF = DAG.getMachineFunction();
1934 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001935 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001936 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001937 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001938 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001939 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001940 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001941 return true;
1942
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001943 // Can only do local tail calls (in same module, hidden or protected) on
1944 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001945 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1946 return G->getGlobal()->hasHiddenVisibility()
1947 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001948 }
1949 }
Evan Chenge7a87392007-11-02 01:26:22 +00001950
1951 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001952}
1953
Dan Gohmanca4857a2008-09-03 23:12:08 +00001954FastISel *
1955X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001956 MachineModuleInfo *mmo,
Devang Patelfcf1c752009-01-13 00:35:13 +00001957 DwarfWriter *dw,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001958 DenseMap<const Value *, unsigned> &vm,
1959 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001960 MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00001961 DenseMap<const AllocaInst *, int> &am
1962#ifndef NDEBUG
1963 , SmallSet<Instruction*, 8> &cil
1964#endif
1965 ) {
Devang Patelfcf1c752009-01-13 00:35:13 +00001966 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohman9dd43582008-10-14 23:54:11 +00001967#ifndef NDEBUG
1968 , cil
1969#endif
1970 );
Dan Gohman97805ee2008-08-19 21:32:53 +00001971}
1972
1973
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001974//===----------------------------------------------------------------------===//
1975// Other Lowering Hooks
1976//===----------------------------------------------------------------------===//
1977
1978
Dan Gohman8181bd12008-07-27 21:46:04 +00001979SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001980 MachineFunction &MF = DAG.getMachineFunction();
1981 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1982 int ReturnAddrIndex = FuncInfo->getRAIndex();
1983
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001984 if (ReturnAddrIndex == 0) {
1985 // Set up a frame object for the return address.
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001986 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001987 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001988 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001989 }
1990
1991 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1992}
1993
1994
Chris Lattnerebb91142008-12-24 23:53:05 +00001995/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
1996/// specific condition code, returning the condition code and the LHS/RHS of the
1997/// comparison to make.
1998static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1999 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002000 if (!isFP) {
2001 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2002 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2003 // X > -1 -> X == 0, jump !sign.
2004 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00002005 return X86::COND_NS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002006 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2007 // X < 0 -> X == 0, jump on sign.
Chris Lattnerebb91142008-12-24 23:53:05 +00002008 return X86::COND_S;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002009 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00002010 // X < 1 -> X <= 0
2011 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00002012 return X86::COND_LE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002013 }
2014 }
2015
2016 switch (SetCCOpcode) {
Chris Lattnerb8397512008-12-23 23:42:27 +00002017 default: assert(0 && "Invalid integer condition!");
Chris Lattnerebb91142008-12-24 23:53:05 +00002018 case ISD::SETEQ: return X86::COND_E;
2019 case ISD::SETGT: return X86::COND_G;
2020 case ISD::SETGE: return X86::COND_GE;
2021 case ISD::SETLT: return X86::COND_L;
2022 case ISD::SETLE: return X86::COND_LE;
2023 case ISD::SETNE: return X86::COND_NE;
2024 case ISD::SETULT: return X86::COND_B;
2025 case ISD::SETUGT: return X86::COND_A;
2026 case ISD::SETULE: return X86::COND_BE;
2027 case ISD::SETUGE: return X86::COND_AE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002028 }
Chris Lattnerb8397512008-12-23 23:42:27 +00002029 }
2030
2031 // First determine if it is required or is profitable to flip the operands.
Duncan Sandsc2a04622008-10-24 13:03:10 +00002032
Chris Lattnerb8397512008-12-23 23:42:27 +00002033 // If LHS is a foldable load, but RHS is not, flip the condition.
2034 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2035 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2036 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2037 std::swap(LHS, RHS);
Evan Chengfc937c92008-08-28 23:48:31 +00002038 }
2039
Chris Lattnerb8397512008-12-23 23:42:27 +00002040 switch (SetCCOpcode) {
2041 default: break;
2042 case ISD::SETOLT:
2043 case ISD::SETOLE:
2044 case ISD::SETUGT:
2045 case ISD::SETUGE:
2046 std::swap(LHS, RHS);
2047 break;
2048 }
2049
2050 // On a floating point condition, the flags are set as follows:
2051 // ZF PF CF op
2052 // 0 | 0 | 0 | X > Y
2053 // 0 | 0 | 1 | X < Y
2054 // 1 | 0 | 0 | X == Y
2055 // 1 | 1 | 1 | unordered
2056 switch (SetCCOpcode) {
Chris Lattnerebb91142008-12-24 23:53:05 +00002057 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattnerb8397512008-12-23 23:42:27 +00002058 case ISD::SETUEQ:
Chris Lattnerebb91142008-12-24 23:53:05 +00002059 case ISD::SETEQ: return X86::COND_E;
Chris Lattnerb8397512008-12-23 23:42:27 +00002060 case ISD::SETOLT: // flipped
2061 case ISD::SETOGT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002062 case ISD::SETGT: return X86::COND_A;
Chris Lattnerb8397512008-12-23 23:42:27 +00002063 case ISD::SETOLE: // flipped
2064 case ISD::SETOGE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002065 case ISD::SETGE: return X86::COND_AE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002066 case ISD::SETUGT: // flipped
2067 case ISD::SETULT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002068 case ISD::SETLT: return X86::COND_B;
Chris Lattnerb8397512008-12-23 23:42:27 +00002069 case ISD::SETUGE: // flipped
2070 case ISD::SETULE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002071 case ISD::SETLE: return X86::COND_BE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002072 case ISD::SETONE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002073 case ISD::SETNE: return X86::COND_NE;
2074 case ISD::SETUO: return X86::COND_P;
2075 case ISD::SETO: return X86::COND_NP;
Chris Lattnerb8397512008-12-23 23:42:27 +00002076 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002077}
2078
2079/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2080/// code. Current x86 isa includes the following FP cmov instructions:
2081/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2082static bool hasFPCMov(unsigned X86CC) {
2083 switch (X86CC) {
2084 default:
2085 return false;
2086 case X86::COND_B:
2087 case X86::COND_BE:
2088 case X86::COND_E:
2089 case X86::COND_P:
2090 case X86::COND_A:
2091 case X86::COND_AE:
2092 case X86::COND_NE:
2093 case X86::COND_NP:
2094 return true;
2095 }
2096}
2097
2098/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2099/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002100static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002101 if (Op.getOpcode() == ISD::UNDEF)
2102 return true;
2103
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002104 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002105 return (Val >= Low && Val < Hi);
2106}
2107
2108/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2109/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002110static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002111 if (Op.getOpcode() == ISD::UNDEF)
2112 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002113 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002114}
2115
2116/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2117/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2118bool X86::isPSHUFDMask(SDNode *N) {
2119 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2120
Dan Gohman7dc19012007-08-02 21:17:01 +00002121 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002122 return false;
2123
2124 // Check if the value doesn't reference the second vector.
2125 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002126 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002127 if (Arg.getOpcode() == ISD::UNDEF) continue;
2128 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002129 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002130 return false;
2131 }
2132
2133 return true;
2134}
2135
2136/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2137/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2138bool X86::isPSHUFHWMask(SDNode *N) {
2139 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2140
2141 if (N->getNumOperands() != 8)
2142 return false;
2143
2144 // Lower quadword copied in order.
2145 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002146 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002147 if (Arg.getOpcode() == ISD::UNDEF) continue;
2148 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002149 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002150 return false;
2151 }
2152
2153 // Upper quadword shuffled.
2154 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002155 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002156 if (Arg.getOpcode() == ISD::UNDEF) continue;
2157 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002158 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002159 if (Val < 4 || Val > 7)
2160 return false;
2161 }
2162
2163 return true;
2164}
2165
2166/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2167/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2168bool X86::isPSHUFLWMask(SDNode *N) {
2169 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2170
2171 if (N->getNumOperands() != 8)
2172 return false;
2173
2174 // Upper quadword copied in order.
2175 for (unsigned i = 4; i != 8; ++i)
2176 if (!isUndefOrEqual(N->getOperand(i), i))
2177 return false;
2178
2179 // Lower quadword shuffled.
2180 for (unsigned i = 0; i != 4; ++i)
2181 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2182 return false;
2183
2184 return true;
2185}
2186
2187/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2188/// specifies a shuffle of elements that is suitable for input to SHUFP*.
djgc2517d32009-01-26 04:35:06 +00002189template<class SDOperand>
2190static bool isSHUFPMask(SDOperand *Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002191 if (NumElems != 2 && NumElems != 4) return false;
2192
2193 unsigned Half = NumElems / 2;
2194 for (unsigned i = 0; i < Half; ++i)
2195 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2196 return false;
2197 for (unsigned i = Half; i < NumElems; ++i)
2198 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2199 return false;
2200
2201 return true;
2202}
2203
2204bool X86::isSHUFPMask(SDNode *N) {
2205 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2206 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2207}
2208
2209/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2210/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2211/// half elements to come from vector 1 (which would equal the dest.) and
2212/// the upper half to come from vector 2.
djgc2517d32009-01-26 04:35:06 +00002213template<class SDOperand>
2214static bool isCommutedSHUFP(SDOperand *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002215 if (NumOps != 2 && NumOps != 4) return false;
2216
2217 unsigned Half = NumOps / 2;
2218 for (unsigned i = 0; i < Half; ++i)
2219 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2220 return false;
2221 for (unsigned i = Half; i < NumOps; ++i)
2222 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2223 return false;
2224 return true;
2225}
2226
2227static bool isCommutedSHUFP(SDNode *N) {
2228 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2229 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2230}
2231
2232/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2233/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2234bool X86::isMOVHLPSMask(SDNode *N) {
2235 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2236
2237 if (N->getNumOperands() != 4)
2238 return false;
2239
2240 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2241 return isUndefOrEqual(N->getOperand(0), 6) &&
2242 isUndefOrEqual(N->getOperand(1), 7) &&
2243 isUndefOrEqual(N->getOperand(2), 2) &&
2244 isUndefOrEqual(N->getOperand(3), 3);
2245}
2246
2247/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2248/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2249/// <2, 3, 2, 3>
2250bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2251 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2252
2253 if (N->getNumOperands() != 4)
2254 return false;
2255
2256 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2257 return isUndefOrEqual(N->getOperand(0), 2) &&
2258 isUndefOrEqual(N->getOperand(1), 3) &&
2259 isUndefOrEqual(N->getOperand(2), 2) &&
2260 isUndefOrEqual(N->getOperand(3), 3);
2261}
2262
2263/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2264/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2265bool X86::isMOVLPMask(SDNode *N) {
2266 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2267
2268 unsigned NumElems = N->getNumOperands();
2269 if (NumElems != 2 && NumElems != 4)
2270 return false;
2271
2272 for (unsigned i = 0; i < NumElems/2; ++i)
2273 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2274 return false;
2275
2276 for (unsigned i = NumElems/2; i < NumElems; ++i)
2277 if (!isUndefOrEqual(N->getOperand(i), i))
2278 return false;
2279
2280 return true;
2281}
2282
2283/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2284/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2285/// and MOVLHPS.
2286bool X86::isMOVHPMask(SDNode *N) {
2287 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2288
2289 unsigned NumElems = N->getNumOperands();
2290 if (NumElems != 2 && NumElems != 4)
2291 return false;
2292
2293 for (unsigned i = 0; i < NumElems/2; ++i)
2294 if (!isUndefOrEqual(N->getOperand(i), i))
2295 return false;
2296
2297 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002298 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002299 if (!isUndefOrEqual(Arg, i + NumElems))
2300 return false;
2301 }
2302
2303 return true;
2304}
2305
2306/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2307/// specifies a shuffle of elements that is suitable for input to UNPCKL.
djgc2517d32009-01-26 04:35:06 +00002308template<class SDOperand>
2309bool static isUNPCKLMask(SDOperand *Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002310 bool V2IsSplat = false) {
2311 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2312 return false;
2313
2314 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002315 SDValue BitI = Elts[i];
2316 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002317 if (!isUndefOrEqual(BitI, j))
2318 return false;
2319 if (V2IsSplat) {
Mon P Wang56d91642009-02-04 01:16:59 +00002320 if (!isUndefOrEqual(BitI1, NumElts))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002321 return false;
2322 } else {
2323 if (!isUndefOrEqual(BitI1, j + NumElts))
2324 return false;
2325 }
2326 }
2327
2328 return true;
2329}
2330
2331bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2332 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2333 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2334}
2335
2336/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2337/// specifies a shuffle of elements that is suitable for input to UNPCKH.
djgc2517d32009-01-26 04:35:06 +00002338template<class SDOperand>
2339bool static isUNPCKHMask(SDOperand *Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002340 bool V2IsSplat = false) {
2341 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2342 return false;
2343
2344 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002345 SDValue BitI = Elts[i];
2346 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002347 if (!isUndefOrEqual(BitI, j + NumElts/2))
2348 return false;
2349 if (V2IsSplat) {
2350 if (isUndefOrEqual(BitI1, NumElts))
2351 return false;
2352 } else {
2353 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2354 return false;
2355 }
2356 }
2357
2358 return true;
2359}
2360
2361bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2362 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2363 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2364}
2365
2366/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2367/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2368/// <0, 0, 1, 1>
2369bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2370 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2371
2372 unsigned NumElems = N->getNumOperands();
2373 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2374 return false;
2375
2376 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002377 SDValue BitI = N->getOperand(i);
2378 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002379
2380 if (!isUndefOrEqual(BitI, j))
2381 return false;
2382 if (!isUndefOrEqual(BitI1, j))
2383 return false;
2384 }
2385
2386 return true;
2387}
2388
2389/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2390/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2391/// <2, 2, 3, 3>
2392bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2393 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2394
2395 unsigned NumElems = N->getNumOperands();
2396 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2397 return false;
2398
2399 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002400 SDValue BitI = N->getOperand(i);
2401 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002402
2403 if (!isUndefOrEqual(BitI, j))
2404 return false;
2405 if (!isUndefOrEqual(BitI1, j))
2406 return false;
2407 }
2408
2409 return true;
2410}
2411
2412/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2413/// specifies a shuffle of elements that is suitable for input to MOVSS,
2414/// MOVSD, and MOVD, i.e. setting the lowest element.
djgc2517d32009-01-26 04:35:06 +00002415template<class SDOperand>
2416static bool isMOVLMask(SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002417 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002418 return false;
2419
2420 if (!isUndefOrEqual(Elts[0], NumElts))
2421 return false;
2422
2423 for (unsigned i = 1; i < NumElts; ++i) {
2424 if (!isUndefOrEqual(Elts[i], i))
2425 return false;
2426 }
2427
2428 return true;
2429}
2430
2431bool X86::isMOVLMask(SDNode *N) {
2432 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2433 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2434}
2435
2436/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2437/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2438/// element of vector 2 and the other elements to come from vector 1 in order.
djgc2517d32009-01-26 04:35:06 +00002439template<class SDOperand>
2440static bool isCommutedMOVL(SDOperand *Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002441 bool V2IsSplat = false,
2442 bool V2IsUndef = false) {
2443 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2444 return false;
2445
2446 if (!isUndefOrEqual(Ops[0], 0))
2447 return false;
2448
2449 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002450 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002451 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2452 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2453 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2454 return false;
2455 }
2456
2457 return true;
2458}
2459
2460static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2461 bool V2IsUndef = false) {
2462 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2463 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2464 V2IsSplat, V2IsUndef);
2465}
2466
2467/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2468/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2469bool X86::isMOVSHDUPMask(SDNode *N) {
2470 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2471
2472 if (N->getNumOperands() != 4)
2473 return false;
2474
2475 // Expect 1, 1, 3, 3
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 != 1) 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 != 3) 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/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2499/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2500bool X86::isMOVSLDUPMask(SDNode *N) {
2501 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2502
2503 if (N->getNumOperands() != 4)
2504 return false;
2505
2506 // Expect 0, 0, 2, 2
2507 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002508 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002509 if (Arg.getOpcode() == ISD::UNDEF) continue;
2510 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002511 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002512 if (Val != 0) return false;
2513 }
2514
2515 bool HasHi = false;
2516 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002517 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002518 if (Arg.getOpcode() == ISD::UNDEF) continue;
2519 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002520 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002521 if (Val != 2) return false;
2522 HasHi = true;
2523 }
2524
2525 // Don't use movshdup if it can be done with a shufps.
2526 return HasHi;
2527}
2528
2529/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2530/// specifies a identity operation on the LHS or RHS.
2531static bool isIdentityMask(SDNode *N, bool RHS = false) {
2532 unsigned NumElems = N->getNumOperands();
2533 for (unsigned i = 0; i < NumElems; ++i)
2534 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2535 return false;
2536 return true;
2537}
2538
2539/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2540/// a splat of a single element.
2541static bool isSplatMask(SDNode *N) {
2542 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2543
2544 // This is a splat operation if each element of the permute is the same, and
2545 // if the value doesn't reference the second vector.
2546 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002547 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002548 unsigned i = 0;
2549 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002550 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002551 if (isa<ConstantSDNode>(Elt)) {
2552 ElementBase = Elt;
2553 break;
2554 }
2555 }
2556
Gabor Greif1c80d112008-08-28 21:40:38 +00002557 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002558 return false;
2559
2560 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002561 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002562 if (Arg.getOpcode() == ISD::UNDEF) continue;
2563 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2564 if (Arg != ElementBase) return false;
2565 }
2566
2567 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002568 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002569}
2570
Mon P Wang532c9632008-12-23 04:03:27 +00002571/// getSplatMaskEltNo - Given a splat mask, return the index to the element
2572/// we want to splat.
2573static SDValue getSplatMaskEltNo(SDNode *N) {
2574 assert(isSplatMask(N) && "Not a splat mask");
2575 unsigned NumElems = N->getNumOperands();
2576 SDValue ElementBase;
2577 unsigned i = 0;
2578 for (; i != NumElems; ++i) {
2579 SDValue Elt = N->getOperand(i);
2580 if (isa<ConstantSDNode>(Elt))
2581 return Elt;
2582 }
2583 assert(0 && " No splat value found!");
2584 return SDValue();
2585}
2586
2587
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002588/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2589/// a splat of a single element and it's a 2 or 4 element mask.
2590bool X86::isSplatMask(SDNode *N) {
2591 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2592
2593 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2594 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2595 return false;
2596 return ::isSplatMask(N);
2597}
2598
2599/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2600/// specifies a splat of zero element.
2601bool X86::isSplatLoMask(SDNode *N) {
2602 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2603
2604 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2605 if (!isUndefOrEqual(N->getOperand(i), 0))
2606 return false;
2607 return true;
2608}
2609
Evan Chenga2497eb2008-09-25 20:50:48 +00002610/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2611/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2612bool X86::isMOVDDUPMask(SDNode *N) {
2613 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2614
2615 unsigned e = N->getNumOperands() / 2;
2616 for (unsigned i = 0; i < e; ++i)
2617 if (!isUndefOrEqual(N->getOperand(i), i))
2618 return false;
2619 for (unsigned i = 0; i < e; ++i)
2620 if (!isUndefOrEqual(N->getOperand(e+i), i))
2621 return false;
2622 return true;
2623}
2624
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002625/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2626/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2627/// instructions.
2628unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2629 unsigned NumOperands = N->getNumOperands();
2630 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2631 unsigned Mask = 0;
2632 for (unsigned i = 0; i < NumOperands; ++i) {
2633 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002634 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002635 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002636 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002637 if (Val >= NumOperands) Val -= NumOperands;
2638 Mask |= Val;
2639 if (i != NumOperands - 1)
2640 Mask <<= Shift;
2641 }
2642
2643 return Mask;
2644}
2645
2646/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2647/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2648/// instructions.
2649unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2650 unsigned Mask = 0;
2651 // 8 nodes, but we only care about the last 4.
2652 for (unsigned i = 7; i >= 4; --i) {
2653 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002654 SDValue Arg = N->getOperand(i);
Mon P Wang56d91642009-02-04 01:16:59 +00002655 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002656 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Mon P Wang56d91642009-02-04 01:16:59 +00002657 Mask |= (Val - 4);
2658 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002659 if (i != 4)
2660 Mask <<= 2;
2661 }
2662
2663 return Mask;
2664}
2665
2666/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2667/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2668/// instructions.
2669unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2670 unsigned Mask = 0;
2671 // 8 nodes, but we only care about the first 4.
2672 for (int i = 3; i >= 0; --i) {
2673 unsigned Val = 0;
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)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002676 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002677 Mask |= Val;
2678 if (i != 0)
2679 Mask <<= 2;
2680 }
2681
2682 return Mask;
2683}
2684
2685/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2686/// specifies a 8 element shuffle that can be broken into a pair of
2687/// PSHUFHW and PSHUFLW.
2688static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2689 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2690
2691 if (N->getNumOperands() != 8)
2692 return false;
2693
2694 // Lower quadword shuffled.
2695 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002696 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002697 if (Arg.getOpcode() == ISD::UNDEF) continue;
2698 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002699 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002700 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002701 return false;
2702 }
2703
2704 // Upper quadword shuffled.
2705 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002706 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002707 if (Arg.getOpcode() == ISD::UNDEF) continue;
2708 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002709 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002710 if (Val < 4 || Val > 7)
2711 return false;
2712 }
2713
2714 return true;
2715}
2716
Chris Lattnere6aa3862007-11-25 00:24:49 +00002717/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002718/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002719static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2720 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002721 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002722 MVT VT = Op.getValueType();
2723 MVT MaskVT = Mask.getValueType();
2724 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002725 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002726 SmallVector<SDValue, 8> MaskVec;
Dale Johannesence0805b2009-02-03 19:33:06 +00002727 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002728
2729 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002730 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002731 if (Arg.getOpcode() == ISD::UNDEF) {
Dale Johannesence0805b2009-02-03 19:33:06 +00002732 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002733 continue;
2734 }
2735 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002736 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002737 if (Val < NumElems)
2738 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2739 else
2740 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2741 }
2742
2743 std::swap(V1, V2);
Dale Johannesence0805b2009-02-03 19:33:06 +00002744 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &MaskVec[0], NumElems);
2745 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002746}
2747
Evan Chenga6769df2007-12-07 21:30:01 +00002748/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2749/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002750static
Dale Johannesence0805b2009-02-03 19:33:06 +00002751SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002752 MVT MaskVT = Mask.getValueType();
2753 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002754 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002755 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002756 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002757 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002758 if (Arg.getOpcode() == ISD::UNDEF) {
Dale Johannesence0805b2009-02-03 19:33:06 +00002759 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, EltVT));
Evan Chengfca29242007-12-07 08:07:39 +00002760 continue;
2761 }
2762 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002763 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002764 if (Val < NumElems)
2765 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2766 else
2767 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2768 }
Dale Johannesence0805b2009-02-03 19:33:06 +00002769 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &MaskVec[0], NumElems);
Evan Chengfca29242007-12-07 08:07:39 +00002770}
2771
2772
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002773/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2774/// match movhlps. The lower half elements should come from upper half of
2775/// V1 (and in order), and the upper half elements should come from the upper
2776/// half of V2 (and in order).
2777static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2778 unsigned NumElems = Mask->getNumOperands();
2779 if (NumElems != 4)
2780 return false;
2781 for (unsigned i = 0, e = 2; i != e; ++i)
2782 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2783 return false;
2784 for (unsigned i = 2; i != 4; ++i)
2785 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2786 return false;
2787 return true;
2788}
2789
2790/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002791/// is promoted to a vector. It also returns the LoadSDNode by reference if
2792/// required.
2793static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002794 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2795 return false;
2796 N = N->getOperand(0).getNode();
2797 if (!ISD::isNON_EXTLoad(N))
2798 return false;
2799 if (LD)
2800 *LD = cast<LoadSDNode>(N);
2801 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002802}
2803
2804/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2805/// match movlp{s|d}. The lower half elements should come from lower half of
2806/// V1 (and in order), and the upper half elements should come from the upper
2807/// half of V2 (and in order). And since V1 will become the source of the
2808/// MOVLP, it must be either a vector load or a scalar load to vector.
2809static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2810 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2811 return false;
2812 // Is V2 is a vector load, don't do this transformation. We will try to use
2813 // load folding shufps op.
2814 if (ISD::isNON_EXTLoad(V2))
2815 return false;
2816
2817 unsigned NumElems = Mask->getNumOperands();
2818 if (NumElems != 2 && NumElems != 4)
2819 return false;
2820 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2821 if (!isUndefOrEqual(Mask->getOperand(i), i))
2822 return false;
2823 for (unsigned i = NumElems/2; i != NumElems; ++i)
2824 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2825 return false;
2826 return true;
2827}
2828
2829/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2830/// all the same.
2831static bool isSplatVector(SDNode *N) {
2832 if (N->getOpcode() != ISD::BUILD_VECTOR)
2833 return false;
2834
Dan Gohman8181bd12008-07-27 21:46:04 +00002835 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002836 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2837 if (N->getOperand(i) != SplatValue)
2838 return false;
2839 return true;
2840}
2841
2842/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2843/// to an undef.
2844static bool isUndefShuffle(SDNode *N) {
2845 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2846 return false;
2847
Dan Gohman8181bd12008-07-27 21:46:04 +00002848 SDValue V1 = N->getOperand(0);
2849 SDValue V2 = N->getOperand(1);
2850 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002851 unsigned NumElems = Mask.getNumOperands();
2852 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002853 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002854 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002855 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002856 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2857 return false;
2858 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2859 return false;
2860 }
2861 }
2862 return true;
2863}
2864
2865/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2866/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002867static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002868 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002869 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002870 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002871 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002872}
2873
2874/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2875/// to an zero vector.
2876static bool isZeroShuffle(SDNode *N) {
2877 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2878 return false;
2879
Dan Gohman8181bd12008-07-27 21:46:04 +00002880 SDValue V1 = N->getOperand(0);
2881 SDValue V2 = N->getOperand(1);
2882 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002883 unsigned NumElems = Mask.getNumOperands();
2884 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002885 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002886 if (Arg.getOpcode() == ISD::UNDEF)
2887 continue;
2888
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002889 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002890 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002891 unsigned Opc = V1.getNode()->getOpcode();
2892 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002893 continue;
2894 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002895 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002896 return false;
2897 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002898 unsigned Opc = V2.getNode()->getOpcode();
2899 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002900 continue;
2901 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002902 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002903 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002904 }
2905 }
2906 return true;
2907}
2908
2909/// getZeroVector - Returns a vector of specified type with all zero elements.
2910///
Dale Johannesence0805b2009-02-03 19:33:06 +00002911static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2912 DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002913 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002914
2915 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2916 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002917 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002918 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002919 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Dale Johannesence0805b2009-02-03 19:33:06 +00002920 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002921 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002922 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Dale Johannesence0805b2009-02-03 19:33:06 +00002923 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002924 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002925 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Dale Johannesence0805b2009-02-03 19:33:06 +00002926 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002927 }
Dale Johannesence0805b2009-02-03 19:33:06 +00002928 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002929}
2930
Chris Lattnere6aa3862007-11-25 00:24:49 +00002931/// getOnesVector - Returns a vector of specified type with all bits set.
2932///
Dale Johannesence0805b2009-02-03 19:33:06 +00002933static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002934 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002935
2936 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2937 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002938 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2939 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002940 if (VT.getSizeInBits() == 64) // MMX
Dale Johannesence0805b2009-02-03 19:33:06 +00002941 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002942 else // SSE
Dale Johannesence0805b2009-02-03 19:33:06 +00002943 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
2944 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002945}
2946
2947
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002948/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2949/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002950static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002951 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2952
2953 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002954 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002955 unsigned NumElems = Mask.getNumOperands();
2956 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002957 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002958 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002959 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002960 if (Val > NumElems) {
2961 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2962 Changed = true;
2963 }
2964 }
2965 MaskVec.push_back(Arg);
2966 }
2967
2968 if (Changed)
Dale Johannesence0805b2009-02-03 19:33:06 +00002969 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getNode()->getDebugLoc(),
2970 Mask.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002971 &MaskVec[0], MaskVec.size());
2972 return Mask;
2973}
2974
2975/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2976/// operation of specified width.
Dale Johannesence0805b2009-02-03 19:33:06 +00002977static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002978 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2979 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002980
Dan Gohman8181bd12008-07-27 21:46:04 +00002981 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002982 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2983 for (unsigned i = 1; i != NumElems; ++i)
2984 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00002985 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
2986 &MaskVec[0], MaskVec.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002987}
2988
2989/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2990/// of specified width.
Dale Johannesence0805b2009-02-03 19:33:06 +00002991static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG,
2992 DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002993 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2994 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002995 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002996 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2997 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2998 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2999 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003000 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3001 &MaskVec[0], MaskVec.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003002}
3003
3004/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
3005/// of specified width.
Dale Johannesence0805b2009-02-03 19:33:06 +00003006static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG,
3007 DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003008 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3009 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003010 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00003011 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003012 for (unsigned i = 0; i != Half; ++i) {
3013 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
3014 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
3015 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003016 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3017 &MaskVec[0], MaskVec.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003018}
3019
Chris Lattner2d91b962008-03-09 01:05:04 +00003020/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
3021/// element #0 of a vector with the specified index, leaving the rest of the
3022/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00003023static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Dale Johannesence0805b2009-02-03 19:33:06 +00003024 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003025 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3026 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003027 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00003028 // Element #0 of the result gets the elt we are replacing.
3029 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
3030 for (unsigned i = 1; i != NumElems; ++i)
3031 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003032 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3033 &MaskVec[0], MaskVec.size());
Chris Lattner2d91b962008-03-09 01:05:04 +00003034}
3035
Evan Chengbf8b2c52008-04-05 00:30:36 +00003036/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00003037static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003038 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
3039 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003040 if (PVT == VT)
3041 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00003042 SDValue V1 = Op.getOperand(0);
3043 SDValue Mask = Op.getOperand(2);
Mon P Wang532c9632008-12-23 04:03:27 +00003044 unsigned MaskNumElems = Mask.getNumOperands();
3045 unsigned NumElems = MaskNumElems;
Dale Johannesence0805b2009-02-03 19:33:06 +00003046 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003047 // Special handling of v4f32 -> v4i32.
3048 if (VT != MVT::v4f32) {
Mon P Wang532c9632008-12-23 04:03:27 +00003049 // Find which element we want to splat.
3050 SDNode* EltNoNode = getSplatMaskEltNo(Mask.getNode()).getNode();
3051 unsigned EltNo = cast<ConstantSDNode>(EltNoNode)->getZExtValue();
3052 // unpack elements to the correct location
Evan Chengbf8b2c52008-04-05 00:30:36 +00003053 while (NumElems > 4) {
Mon P Wang532c9632008-12-23 04:03:27 +00003054 if (EltNo < NumElems/2) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003055 Mask = getUnpacklMask(MaskNumElems, DAG, dl);
Mon P Wang532c9632008-12-23 04:03:27 +00003056 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00003057 Mask = getUnpackhMask(MaskNumElems, DAG, dl);
Mon P Wang532c9632008-12-23 04:03:27 +00003058 EltNo -= NumElems/2;
3059 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003060 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V1, Mask);
Evan Chengbf8b2c52008-04-05 00:30:36 +00003061 NumElems >>= 1;
3062 }
Mon P Wang532c9632008-12-23 04:03:27 +00003063 SDValue Cst = DAG.getConstant(EltNo, MVT::i32);
Dale Johannesence0805b2009-02-03 19:33:06 +00003064 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003066
Dale Johannesence0805b2009-02-03 19:33:06 +00003067 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
3068 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00003069 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dale Johannesence0805b2009-02-03 19:33:06 +00003070 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuffle);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003071}
3072
Evan Chenga2497eb2008-09-25 20:50:48 +00003073/// isVectorLoad - Returns true if the node is a vector load, a scalar
3074/// load that's promoted to vector, or a load bitcasted.
3075static bool isVectorLoad(SDValue Op) {
3076 assert(Op.getValueType().isVector() && "Expected a vector type");
3077 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
3078 Op.getOpcode() == ISD::BIT_CONVERT) {
3079 return isa<LoadSDNode>(Op.getOperand(0));
3080 }
3081 return isa<LoadSDNode>(Op);
3082}
3083
3084
3085/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3086///
3087static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3088 SelectionDAG &DAG, bool HasSSE3) {
3089 // If we have sse3 and shuffle has more than one use or input is a load, then
3090 // use movddup. Otherwise, use movlhps.
3091 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3092 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3093 MVT VT = Op.getValueType();
3094 if (VT == PVT)
3095 return Op;
Dale Johannesence0805b2009-02-03 19:33:06 +00003096 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Chenga2497eb2008-09-25 20:50:48 +00003097 unsigned NumElems = PVT.getVectorNumElements();
3098 if (NumElems == 2) {
3099 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Dale Johannesence0805b2009-02-03 19:33:06 +00003100 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chenga2497eb2008-09-25 20:50:48 +00003101 } else {
3102 assert(NumElems == 4);
3103 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3104 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
Dale Johannesence0805b2009-02-03 19:33:06 +00003105 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
3106 Cst0, Cst1, Cst0, Cst1);
Evan Chenga2497eb2008-09-25 20:50:48 +00003107 }
3108
Dale Johannesence0805b2009-02-03 19:33:06 +00003109 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
3110 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, PVT, V1,
3111 DAG.getNode(ISD::UNDEF, dl, PVT), Mask);
3112 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuffle);
Evan Chenga2497eb2008-09-25 20:50:48 +00003113}
3114
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003115/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003116/// vector of zero or undef vector. This produces a shuffle where the low
3117/// element of V2 is swizzled into the zero/undef vector, landing at element
3118/// 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 +00003119static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003120 bool isZero, bool HasSSE2,
3121 SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003122 DebugLoc dl = V2.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00003123 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003124 SDValue V1 = isZero
Dale Johannesence0805b2009-02-03 19:33:06 +00003125 ? getZeroVector(VT, HasSSE2, DAG, dl) : DAG.getNode(ISD::UNDEF, dl, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003126 unsigned NumElems = V2.getValueType().getVectorNumElements();
3127 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3128 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003129 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003130 for (unsigned i = 0; i != NumElems; ++i)
3131 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3132 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3133 else
3134 MaskVec.push_back(DAG.getConstant(i, EVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003135 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003136 &MaskVec[0], MaskVec.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00003137 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003138}
3139
Evan Chengdea99362008-05-29 08:22:04 +00003140/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3141/// a shuffle that is zero.
3142static
Dan Gohman8181bd12008-07-27 21:46:04 +00003143unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003144 unsigned NumElems, bool Low,
3145 SelectionDAG &DAG) {
3146 unsigned NumZeros = 0;
3147 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003148 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003149 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003150 if (Idx.getOpcode() == ISD::UNDEF) {
3151 ++NumZeros;
3152 continue;
3153 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003154 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3155 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003156 ++NumZeros;
3157 else
3158 break;
3159 }
3160 return NumZeros;
3161}
3162
3163/// isVectorShift - Returns true if the shuffle can be implemented as a
3164/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003165static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3166 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003167 unsigned NumElems = Mask.getNumOperands();
3168
3169 isLeft = true;
3170 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3171 if (!NumZeros) {
3172 isLeft = false;
3173 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3174 if (!NumZeros)
3175 return false;
3176 }
3177
3178 bool SeenV1 = false;
3179 bool SeenV2 = false;
3180 for (unsigned i = NumZeros; i < NumElems; ++i) {
3181 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003182 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003183 if (Idx.getOpcode() == ISD::UNDEF)
3184 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003185 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003186 if (Index < NumElems)
3187 SeenV1 = true;
3188 else {
3189 Index -= NumElems;
3190 SeenV2 = true;
3191 }
3192 if (Index != Val)
3193 return false;
3194 }
3195 if (SeenV1 && SeenV2)
3196 return false;
3197
3198 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3199 ShAmt = NumZeros;
3200 return true;
3201}
3202
3203
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003204/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3205///
Dan Gohman8181bd12008-07-27 21:46:04 +00003206static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003207 unsigned NumNonZero, unsigned NumZero,
3208 SelectionDAG &DAG, TargetLowering &TLI) {
3209 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003210 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003211
Dale Johannesence0805b2009-02-03 19:33:06 +00003212 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00003213 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003214 bool First = true;
3215 for (unsigned i = 0; i < 16; ++i) {
3216 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3217 if (ThisIsNonZero && First) {
3218 if (NumZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003219 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003220 else
Dale Johannesence0805b2009-02-03 19:33:06 +00003221 V = DAG.getNode(ISD::UNDEF, dl, MVT::v8i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003222 First = false;
3223 }
3224
3225 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003226 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003227 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3228 if (LastIsNonZero) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003229 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
3230 MVT::i16, Op.getOperand(i-1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003231 }
3232 if (ThisIsNonZero) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003233 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3234 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003235 ThisElt, DAG.getConstant(8, MVT::i8));
3236 if (LastIsNonZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003237 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003238 } else
3239 ThisElt = LastElt;
3240
Gabor Greif1c80d112008-08-28 21:40:38 +00003241 if (ThisElt.getNode())
Dale Johannesence0805b2009-02-03 19:33:06 +00003242 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003243 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003244 }
3245 }
3246
Dale Johannesence0805b2009-02-03 19:33:06 +00003247 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003248}
3249
3250/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3251///
Dan Gohman8181bd12008-07-27 21:46:04 +00003252static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003253 unsigned NumNonZero, unsigned NumZero,
3254 SelectionDAG &DAG, TargetLowering &TLI) {
3255 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003256 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003257
Dale Johannesence0805b2009-02-03 19:33:06 +00003258 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00003259 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003260 bool First = true;
3261 for (unsigned i = 0; i < 8; ++i) {
3262 bool isNonZero = (NonZeros & (1 << i)) != 0;
3263 if (isNonZero) {
3264 if (First) {
3265 if (NumZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003266 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003267 else
Dale Johannesence0805b2009-02-03 19:33:06 +00003268 V = DAG.getNode(ISD::UNDEF, dl, MVT::v8i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003269 First = false;
3270 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003271 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
3272 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003273 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003274 }
3275 }
3276
3277 return V;
3278}
3279
Evan Chengdea99362008-05-29 08:22:04 +00003280/// getVShift - Return a vector logical shift node.
3281///
Dan Gohman8181bd12008-07-27 21:46:04 +00003282static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003283 unsigned NumBits, SelectionDAG &DAG,
Dale Johannesence0805b2009-02-03 19:33:06 +00003284 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003285 bool isMMX = VT.getSizeInBits() == 64;
3286 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003287 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesence0805b2009-02-03 19:33:06 +00003288 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3289 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3290 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003291 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003292}
3293
Dan Gohman8181bd12008-07-27 21:46:04 +00003294SDValue
3295X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003296 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattnere6aa3862007-11-25 00:24:49 +00003297 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003298 if (ISD::isBuildVectorAllZeros(Op.getNode())
3299 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003300 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3301 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3302 // eliminated on x86-32 hosts.
3303 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3304 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003305
Gabor Greif1c80d112008-08-28 21:40:38 +00003306 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00003307 return getOnesVector(Op.getValueType(), DAG, dl);
3308 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003309 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003310
Duncan Sands92c43912008-06-06 12:08:01 +00003311 MVT VT = Op.getValueType();
3312 MVT EVT = VT.getVectorElementType();
3313 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003314
3315 unsigned NumElems = Op.getNumOperands();
3316 unsigned NumZero = 0;
3317 unsigned NumNonZero = 0;
3318 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003319 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003320 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003321 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003322 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003323 if (Elt.getOpcode() == ISD::UNDEF)
3324 continue;
3325 Values.insert(Elt);
3326 if (Elt.getOpcode() != ISD::Constant &&
3327 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003328 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003329 if (isZeroNode(Elt))
3330 NumZero++;
3331 else {
3332 NonZeros |= (1 << i);
3333 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003334 }
3335 }
3336
3337 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003338 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesence0805b2009-02-03 19:33:06 +00003339 return DAG.getNode(ISD::UNDEF, dl, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003340 }
3341
Chris Lattner66a4dda2008-03-09 05:42:06 +00003342 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003343 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003344 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003345 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003346
Chris Lattner2d91b962008-03-09 01:05:04 +00003347 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3348 // the value are obviously zero, truncate the value to i32 and do the
3349 // insertion that way. Only do this if the value is non-constant or if the
3350 // value is a constant being inserted into element 0. It is cheaper to do
3351 // a constant pool load than it is to do a movd + shuffle.
3352 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3353 (!IsAllConstants || Idx == 0)) {
3354 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3355 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003356 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3357 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003358
3359 // Truncate the value (which may itself be a constant) to i32, and
3360 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesence0805b2009-02-03 19:33:06 +00003361 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3362 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003363 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3364 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003365
3366 // Now we have our 32-bit value zero extended in the low element of
3367 // a vector. If Idx != 0, swizzle it into place.
3368 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003369 SDValue Ops[] = {
Dale Johannesence0805b2009-02-03 19:33:06 +00003370 Item, DAG.getNode(ISD::UNDEF, dl, Item.getValueType()),
3371 getSwapEltZeroMask(VecElts, Idx, DAG, dl)
Chris Lattner2d91b962008-03-09 01:05:04 +00003372 };
Dale Johannesence0805b2009-02-03 19:33:06 +00003373 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VecVT, Ops, 3);
Chris Lattner2d91b962008-03-09 01:05:04 +00003374 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003375 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner2d91b962008-03-09 01:05:04 +00003376 }
3377 }
3378
Chris Lattnerac914892008-03-08 22:59:52 +00003379 // If we have a constant or non-constant insertion into the low element of
3380 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3381 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3382 // depending on what the source datatype is. Because we can only get here
3383 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3384 if (Idx == 0 &&
3385 // Don't do this for i64 values on x86-32.
3386 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003387 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003388 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003389 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3390 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003391 }
Evan Chengdea99362008-05-29 08:22:04 +00003392
3393 // Is it a vector logical left shift?
3394 if (NumElems == 2 && Idx == 1 &&
3395 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003396 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003397 return getVShift(true, VT,
3398 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
Dale Johannesence0805b2009-02-03 19:33:06 +00003399 NumBits/2, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00003400 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003401
3402 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003403 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003404
Chris Lattnerac914892008-03-08 22:59:52 +00003405 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3406 // is a non-constant being inserted into an element other than the low one,
3407 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3408 // movd/movss) to move this into the low element, then shuffle it into
3409 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003410 if (EVTBits == 32) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003411 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003412
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003413 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003414 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3415 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003416 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3417 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003418 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003419 for (unsigned i = 0; i < NumElems; i++)
3420 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003421 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003422 &MaskVec[0], MaskVec.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00003423 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, Item,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003424 DAG.getNode(ISD::UNDEF, VT), Mask);
3425 }
3426 }
3427
Chris Lattner66a4dda2008-03-09 05:42:06 +00003428 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3429 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003430 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003431
Dan Gohman21463242007-07-24 22:55:08 +00003432 // A vector full of immediates; various special cases are already
3433 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003434 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003435 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003436
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003437 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003438 if (EVTBits == 64) {
3439 if (NumNonZero == 1) {
3440 // One half is zero or undef.
3441 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesence0805b2009-02-03 19:33:06 +00003442 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003443 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003444 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3445 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003446 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003447 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003448 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003449
3450 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3451 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003452 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003453 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003454 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003455 }
3456
3457 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003458 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003459 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003460 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003461 }
3462
3463 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003464 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003465 V.resize(NumElems);
3466 if (NumElems == 4 && NumZero > 0) {
3467 for (unsigned i = 0; i < 4; ++i) {
3468 bool isZero = !(NonZeros & (1 << i));
3469 if (isZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003470 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003471 else
Dale Johannesence0805b2009-02-03 19:33:06 +00003472 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003473 }
3474
3475 for (unsigned i = 0; i < 2; ++i) {
3476 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3477 default: break;
3478 case 0:
3479 V[i] = V[i*2]; // Must be a zero vector.
3480 break;
3481 case 1:
Dale Johannesence0805b2009-02-03 19:33:06 +00003482 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i*2+1], V[i*2],
3483 getMOVLMask(NumElems, DAG, dl));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003484 break;
3485 case 2:
Dale Johannesence0805b2009-02-03 19:33:06 +00003486 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i*2], V[i*2+1],
3487 getMOVLMask(NumElems, DAG, dl));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003488 break;
3489 case 3:
Dale Johannesence0805b2009-02-03 19:33:06 +00003490 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i*2], V[i*2+1],
3491 getUnpacklMask(NumElems, DAG, dl));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003492 break;
3493 }
3494 }
3495
Duncan Sands92c43912008-06-06 12:08:01 +00003496 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3497 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003498 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003499 bool Reverse = (NonZeros & 0x3) == 2;
3500 for (unsigned i = 0; i < 2; ++i)
3501 if (Reverse)
3502 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3503 else
3504 MaskVec.push_back(DAG.getConstant(i, EVT));
3505 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3506 for (unsigned i = 0; i < 2; ++i)
3507 if (Reverse)
3508 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3509 else
3510 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003511 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003512 &MaskVec[0], MaskVec.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00003513 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[0], V[1], ShufMask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003514 }
3515
3516 if (Values.size() > 2) {
3517 // Expand into a number of unpckl*.
3518 // e.g. for v4f32
3519 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3520 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3521 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dale Johannesence0805b2009-02-03 19:33:06 +00003522 SDValue UnpckMask = getUnpacklMask(NumElems, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003523 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesence0805b2009-02-03 19:33:06 +00003524 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003525 NumElems >>= 1;
3526 while (NumElems != 0) {
3527 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesence0805b2009-02-03 19:33:06 +00003528 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i], V[i + NumElems],
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003529 UnpckMask);
3530 NumElems >>= 1;
3531 }
3532 return V[0];
3533 }
3534
Dan Gohman8181bd12008-07-27 21:46:04 +00003535 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003536}
3537
Evan Chengfca29242007-12-07 08:07:39 +00003538static
Dan Gohman8181bd12008-07-27 21:46:04 +00003539SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003540 SDValue PermMask, SelectionDAG &DAG,
Dale Johannesence0805b2009-02-03 19:33:06 +00003541 TargetLowering &TLI, DebugLoc dl) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003542 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003543 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3544 MVT MaskEVT = MaskVT.getVectorElementType();
3545 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003546 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3547 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003548
3549 // First record which half of which vector the low elements come from.
3550 SmallVector<unsigned, 4> LowQuad(4);
3551 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003552 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003553 if (Elt.getOpcode() == ISD::UNDEF)
3554 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003555 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003556 int QuadIdx = EltIdx / 4;
3557 ++LowQuad[QuadIdx];
3558 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003559
Evan Cheng75184a92007-12-11 01:46:18 +00003560 int BestLowQuad = -1;
3561 unsigned MaxQuad = 1;
3562 for (unsigned i = 0; i < 4; ++i) {
3563 if (LowQuad[i] > MaxQuad) {
3564 BestLowQuad = i;
3565 MaxQuad = LowQuad[i];
3566 }
Evan Chengfca29242007-12-07 08:07:39 +00003567 }
3568
Evan Cheng75184a92007-12-11 01:46:18 +00003569 // Record which half of which vector the high elements come from.
3570 SmallVector<unsigned, 4> HighQuad(4);
3571 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003572 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003573 if (Elt.getOpcode() == ISD::UNDEF)
3574 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003575 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003576 int QuadIdx = EltIdx / 4;
3577 ++HighQuad[QuadIdx];
3578 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003579
Evan Cheng75184a92007-12-11 01:46:18 +00003580 int BestHighQuad = -1;
3581 MaxQuad = 1;
3582 for (unsigned i = 0; i < 4; ++i) {
3583 if (HighQuad[i] > MaxQuad) {
3584 BestHighQuad = i;
3585 MaxQuad = HighQuad[i];
3586 }
3587 }
3588
3589 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3590 if (BestLowQuad != -1 || BestHighQuad != -1) {
3591 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003592 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003593
Evan Cheng75184a92007-12-11 01:46:18 +00003594 if (BestLowQuad != -1)
3595 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3596 else
3597 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003598
Evan Cheng75184a92007-12-11 01:46:18 +00003599 if (BestHighQuad != -1)
3600 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3601 else
3602 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003603
Dale Johannesence0805b2009-02-03 19:33:06 +00003604 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, &MaskVec[0],2);
3605 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v2i64,
3606 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3607 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), Mask);
3608 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng75184a92007-12-11 01:46:18 +00003609
3610 // Now sort high and low parts separately.
3611 BitVector InOrder(8);
3612 if (BestLowQuad != -1) {
3613 // Sort lower half in order using PSHUFLW.
3614 MaskVec.clear();
3615 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003616
Evan Cheng75184a92007-12-11 01:46:18 +00003617 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003618 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003619 if (Elt.getOpcode() == ISD::UNDEF) {
3620 MaskVec.push_back(Elt);
3621 InOrder.set(i);
3622 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003623 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003624 if (EltIdx != i)
3625 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003626
Evan Cheng75184a92007-12-11 01:46:18 +00003627 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003628
Evan Cheng75184a92007-12-11 01:46:18 +00003629 // If this element is in the right place after this shuffle, then
3630 // remember it.
3631 if ((int)(EltIdx / 4) == BestLowQuad)
3632 InOrder.set(i);
3633 }
3634 }
3635 if (AnyOutOrder) {
3636 for (unsigned i = 4; i != 8; ++i)
3637 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003638 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3639 &MaskVec[0], 8);
3640 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v8i16,
3641 NewV, NewV, Mask);
Evan Cheng75184a92007-12-11 01:46:18 +00003642 }
3643 }
3644
3645 if (BestHighQuad != -1) {
3646 // Sort high half in order using PSHUFHW if possible.
3647 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003648
Evan Cheng75184a92007-12-11 01:46:18 +00003649 for (unsigned i = 0; i != 4; ++i)
3650 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003651
Evan Cheng75184a92007-12-11 01:46:18 +00003652 bool AnyOutOrder = false;
3653 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003654 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003655 if (Elt.getOpcode() == ISD::UNDEF) {
3656 MaskVec.push_back(Elt);
3657 InOrder.set(i);
3658 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003659 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003660 if (EltIdx != i)
3661 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003662
Evan Cheng75184a92007-12-11 01:46:18 +00003663 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003664
Evan Cheng75184a92007-12-11 01:46:18 +00003665 // If this element is in the right place after this shuffle, then
3666 // remember it.
3667 if ((int)(EltIdx / 4) == BestHighQuad)
3668 InOrder.set(i);
3669 }
3670 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003671
Evan Cheng75184a92007-12-11 01:46:18 +00003672 if (AnyOutOrder) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003673 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl,
3674 MaskVT, &MaskVec[0], 8);
3675 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v8i16,
3676 NewV, NewV, Mask);
Evan Cheng75184a92007-12-11 01:46:18 +00003677 }
3678 }
3679
3680 // The other elements are put in the right place using pextrw and pinsrw.
3681 for (unsigned i = 0; i != 8; ++i) {
3682 if (InOrder[i])
3683 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003684 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003685 if (Elt.getOpcode() == ISD::UNDEF)
3686 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003687 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003688 SDValue ExtOp = (EltIdx < 8)
Dale Johannesence0805b2009-02-03 19:33:06 +00003689 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003690 DAG.getConstant(EltIdx, PtrVT))
Dale Johannesence0805b2009-02-03 19:33:06 +00003691 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003692 DAG.getConstant(EltIdx - 8, PtrVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003693 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Evan Cheng75184a92007-12-11 01:46:18 +00003694 DAG.getConstant(i, PtrVT));
3695 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003696
Evan Cheng75184a92007-12-11 01:46:18 +00003697 return NewV;
3698 }
3699
Bill Wendling2c7cd592008-08-21 22:35:37 +00003700 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3701 // few as possible. First, let's find out how many elements are already in the
3702 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003703 unsigned V1InOrder = 0;
3704 unsigned V1FromV1 = 0;
3705 unsigned V2InOrder = 0;
3706 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003707 SmallVector<SDValue, 8> V1Elts;
3708 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003709 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003710 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003711 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003712 V1Elts.push_back(Elt);
3713 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003714 ++V1InOrder;
3715 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003716 continue;
3717 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003718 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003719 if (EltIdx == i) {
3720 V1Elts.push_back(Elt);
3721 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3722 ++V1InOrder;
3723 } else if (EltIdx == i+8) {
3724 V1Elts.push_back(Elt);
3725 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3726 ++V2InOrder;
3727 } else if (EltIdx < 8) {
3728 V1Elts.push_back(Elt);
Mon P Wang75498182009-01-28 23:11:14 +00003729 V2Elts.push_back(DAG.getConstant(EltIdx+8, MaskEVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003730 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003731 } else {
Mon P Wang532c9632008-12-23 04:03:27 +00003732 V1Elts.push_back(Elt);
Evan Cheng75184a92007-12-11 01:46:18 +00003733 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3734 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003735 }
3736 }
3737
3738 if (V2InOrder > V1InOrder) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003739 PermMask = CommuteVectorShuffleMask(PermMask, DAG, dl);
Evan Chengfca29242007-12-07 08:07:39 +00003740 std::swap(V1, V2);
3741 std::swap(V1Elts, V2Elts);
3742 std::swap(V1FromV1, V2FromV2);
3743 }
3744
Evan Cheng75184a92007-12-11 01:46:18 +00003745 if ((V1FromV1 + V1InOrder) != 8) {
3746 // Some elements are from V2.
3747 if (V1FromV1) {
3748 // If there are elements that are from V1 but out of place,
3749 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003750 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003751 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003752 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003753 if (Elt.getOpcode() == ISD::UNDEF) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003754 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003755 continue;
3756 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003757 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003758 if (EltIdx >= 8)
Dale Johannesence0805b2009-02-03 19:33:06 +00003759 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003760 else
3761 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3762 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003763 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &MaskVec[0], 8);
3764 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003765 }
Evan Cheng75184a92007-12-11 01:46:18 +00003766
3767 NewV = V1;
3768 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003769 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003770 if (Elt.getOpcode() == ISD::UNDEF)
3771 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003772 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003773 if (EltIdx < 8)
3774 continue;
Dale Johannesence0805b2009-02-03 19:33:06 +00003775 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003776 DAG.getConstant(EltIdx - 8, PtrVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003777 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Evan Cheng75184a92007-12-11 01:46:18 +00003778 DAG.getConstant(i, PtrVT));
3779 }
3780 return NewV;
3781 } else {
3782 // All elements are from V1.
3783 NewV = V1;
3784 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003785 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003786 if (Elt.getOpcode() == ISD::UNDEF)
3787 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003788 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dale Johannesence0805b2009-02-03 19:33:06 +00003789 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003790 DAG.getConstant(EltIdx, PtrVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003791 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Evan Cheng75184a92007-12-11 01:46:18 +00003792 DAG.getConstant(i, PtrVT));
3793 }
3794 return NewV;
3795 }
3796}
3797
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003798/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3799/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3800/// done when every pair / quad of shuffle mask elements point to elements in
3801/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003802/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3803static
Dan Gohman8181bd12008-07-27 21:46:04 +00003804SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003805 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003806 SDValue PermMask, SelectionDAG &DAG,
Dale Johannesence0805b2009-02-03 19:33:06 +00003807 TargetLowering &TLI, DebugLoc dl) {
Evan Cheng75184a92007-12-11 01:46:18 +00003808 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003809 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003810 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003811 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003812 MVT NewVT = MaskVT;
3813 switch (VT.getSimpleVT()) {
3814 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003815 case MVT::v4f32: NewVT = MVT::v2f64; break;
3816 case MVT::v4i32: NewVT = MVT::v2i64; break;
3817 case MVT::v8i16: NewVT = MVT::v4i32; break;
3818 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003819 }
3820
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003821 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003822 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003823 NewVT = MVT::v2i64;
3824 else
3825 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003826 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003827 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003828 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003829 for (unsigned i = 0; i < NumElems; i += Scale) {
3830 unsigned StartIdx = ~0U;
3831 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003832 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003833 if (Elt.getOpcode() == ISD::UNDEF)
3834 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003835 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003836 if (StartIdx == ~0U)
3837 StartIdx = EltIdx - (EltIdx % Scale);
3838 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003839 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003840 }
3841 if (StartIdx == ~0U)
Dale Johannesence0805b2009-02-03 19:33:06 +00003842 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003843 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003844 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003845 }
3846
Dale Johannesence0805b2009-02-03 19:33:06 +00003847 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3848 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
3849 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, NewVT, V1, V2,
3850 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003851 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003852}
3853
Evan Chenge9b9c672008-05-09 21:53:03 +00003854/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003855///
Dan Gohman8181bd12008-07-27 21:46:04 +00003856static SDValue getVZextMovL(MVT VT, MVT OpVT,
3857 SDValue SrcOp, SelectionDAG &DAG,
Dale Johannesence0805b2009-02-03 19:33:06 +00003858 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003859 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3860 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003861 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003862 LD = dyn_cast<LoadSDNode>(SrcOp);
3863 if (!LD) {
3864 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3865 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003866 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003867 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3868 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3869 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3870 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3871 // PR2108
3872 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesence0805b2009-02-03 19:33:06 +00003873 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3874 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3875 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3876 OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003877 SrcOp.getOperand(0)
3878 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003879 }
3880 }
3881 }
3882
Dale Johannesence0805b2009-02-03 19:33:06 +00003883 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3884 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3885 DAG.getNode(ISD::BIT_CONVERT, dl,
3886 OpVT, SrcOp)));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003887}
3888
Evan Chengf50554e2008-07-22 21:13:36 +00003889/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3890/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003891static SDValue
3892LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
Dale Johannesence0805b2009-02-03 19:33:06 +00003893 SDValue PermMask, MVT VT, SelectionDAG &DAG,
3894 DebugLoc dl) {
Evan Chengf50554e2008-07-22 21:13:36 +00003895 MVT MaskVT = PermMask.getValueType();
3896 MVT MaskEVT = MaskVT.getVectorElementType();
3897 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003898 Locs.resize(4);
Dale Johannesence0805b2009-02-03 19:33:06 +00003899 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003900 unsigned NumHi = 0;
3901 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003902 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003903 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003904 if (Elt.getOpcode() == ISD::UNDEF) {
3905 Locs[i] = std::make_pair(-1, -1);
3906 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003907 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003908 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003909 if (Val < 4) {
3910 Locs[i] = std::make_pair(0, NumLo);
3911 Mask1[NumLo] = Elt;
3912 NumLo++;
3913 } else {
3914 Locs[i] = std::make_pair(1, NumHi);
3915 if (2+NumHi < 4)
3916 Mask1[2+NumHi] = Elt;
3917 NumHi++;
3918 }
3919 }
3920 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003921
Evan Chengf50554e2008-07-22 21:13:36 +00003922 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003923 // If no more than two elements come from either vector. This can be
3924 // implemented with two shuffles. First shuffle gather the elements.
3925 // The second shuffle, which takes the first shuffle as both of its
3926 // vector operands, put the elements into the right order.
Dale Johannesence0805b2009-02-03 19:33:06 +00003927 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
3928 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengf50554e2008-07-22 21:13:36 +00003929 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003930
Dale Johannesence0805b2009-02-03 19:33:06 +00003931 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003932 for (unsigned i = 0; i != 4; ++i) {
3933 if (Locs[i].first == -1)
3934 continue;
3935 else {
3936 unsigned Idx = (i < 2) ? 0 : 4;
3937 Idx += Locs[i].first * 2 + Locs[i].second;
3938 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3939 }
3940 }
3941
Dale Johannesence0805b2009-02-03 19:33:06 +00003942 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V1,
3943 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengf50554e2008-07-22 21:13:36 +00003944 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003945 } else if (NumLo == 3 || NumHi == 3) {
3946 // Otherwise, we must have three elements from one vector, call it X, and
3947 // one element from the other, call it Y. First, use a shufps to build an
3948 // intermediate vector with the one element from Y and the element from X
3949 // that will be in the same half in the final destination (the indexes don't
3950 // matter). Then, use a shufps to build the final vector, taking the half
3951 // containing the element from Y from the intermediate, and the other half
3952 // from X.
3953 if (NumHi == 3) {
3954 // Normalize it so the 3 elements come from V1.
Dale Johannesence0805b2009-02-03 19:33:06 +00003955 PermMask = CommuteVectorShuffleMask(PermMask, DAG, dl);
Evan Cheng3cae0332008-07-23 00:22:17 +00003956 std::swap(V1, V2);
3957 }
3958
3959 // Find the element from V2.
3960 unsigned HiIndex;
3961 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003962 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003963 if (Elt.getOpcode() == ISD::UNDEF)
3964 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003965 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003966 if (Val >= 4)
3967 break;
3968 }
3969
3970 Mask1[0] = PermMask.getOperand(HiIndex);
Dale Johannesence0805b2009-02-03 19:33:06 +00003971 Mask1[1] = DAG.getNode(ISD::UNDEF, dl, MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003972 Mask1[2] = PermMask.getOperand(HiIndex^1);
Dale Johannesence0805b2009-02-03 19:33:06 +00003973 Mask1[3] = DAG.getNode(ISD::UNDEF, dl, MaskEVT);
3974 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
Evan Cheng3cae0332008-07-23 00:22:17 +00003975 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3976
3977 if (HiIndex >= 2) {
3978 Mask1[0] = PermMask.getOperand(0);
3979 Mask1[1] = PermMask.getOperand(1);
3980 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3981 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
Dale Johannesence0805b2009-02-03 19:33:06 +00003982 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
3983 DAG.getNode(ISD::BUILD_VECTOR, dl,
3984 MaskVT, &Mask1[0], 4));
Evan Cheng3cae0332008-07-23 00:22:17 +00003985 } else {
3986 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3987 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3988 Mask1[2] = PermMask.getOperand(2);
3989 Mask1[3] = PermMask.getOperand(3);
3990 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003991 Mask1[2] =
3992 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3993 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003994 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003995 Mask1[3] =
3996 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3997 MaskEVT);
Dale Johannesence0805b2009-02-03 19:33:06 +00003998 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V2, V1,
3999 DAG.getNode(ISD::BUILD_VECTOR, dl,
4000 MaskVT, &Mask1[0], 4));
Evan Cheng3cae0332008-07-23 00:22:17 +00004001 }
Evan Chengf50554e2008-07-22 21:13:36 +00004002 }
4003
4004 // Break it into (shuffle shuffle_hi, shuffle_lo).
4005 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00004006 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
4007 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
4008 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00004009 unsigned MaskIdx = 0;
4010 unsigned LoIdx = 0;
4011 unsigned HiIdx = 2;
4012 for (unsigned i = 0; i != 4; ++i) {
4013 if (i == 2) {
4014 MaskPtr = &HiMask;
4015 MaskIdx = 1;
4016 LoIdx = 0;
4017 HiIdx = 2;
4018 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004019 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00004020 if (Elt.getOpcode() == ISD::UNDEF) {
4021 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004022 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00004023 Locs[i] = std::make_pair(MaskIdx, LoIdx);
4024 (*MaskPtr)[LoIdx] = Elt;
4025 LoIdx++;
4026 } else {
4027 Locs[i] = std::make_pair(MaskIdx, HiIdx);
4028 (*MaskPtr)[HiIdx] = Elt;
4029 HiIdx++;
4030 }
4031 }
4032
Dale Johannesence0805b2009-02-03 19:33:06 +00004033 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
4034 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengf50554e2008-07-22 21:13:36 +00004035 &LoMask[0], LoMask.size()));
Dale Johannesence0805b2009-02-03 19:33:06 +00004036 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
4037 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengf50554e2008-07-22 21:13:36 +00004038 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004039 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00004040 for (unsigned i = 0; i != 4; ++i) {
4041 if (Locs[i].first == -1) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004042 MaskOps.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00004043 } else {
4044 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
4045 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
4046 }
4047 }
Dale Johannesence0805b2009-02-03 19:33:06 +00004048 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, LoShuffle, HiShuffle,
4049 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengf50554e2008-07-22 21:13:36 +00004050 &MaskOps[0], MaskOps.size()));
4051}
4052
Dan Gohman8181bd12008-07-27 21:46:04 +00004053SDValue
4054X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
4055 SDValue V1 = Op.getOperand(0);
4056 SDValue V2 = Op.getOperand(1);
4057 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00004058 MVT VT = Op.getValueType();
Dale Johannesence0805b2009-02-03 19:33:06 +00004059 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004060 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00004061 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004062 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4063 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
4064 bool V1IsSplat = false;
4065 bool V2IsSplat = false;
4066
Gabor Greif1c80d112008-08-28 21:40:38 +00004067 if (isUndefShuffle(Op.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00004068 return DAG.getNode(ISD::UNDEF, dl, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004069
Gabor Greif1c80d112008-08-28 21:40:38 +00004070 if (isZeroShuffle(Op.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00004071 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004072
Gabor Greif1c80d112008-08-28 21:40:38 +00004073 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004074 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00004075 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004076 return V2;
4077
Evan Chengae6c9212008-09-25 23:35:16 +00004078 // Canonicalize movddup shuffles.
4079 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Chengbdd9d9f2008-10-06 21:13:08 +00004080 VT.getSizeInBits() == 128 &&
Evan Chengae6c9212008-09-25 23:35:16 +00004081 X86::isMOVDDUPMask(PermMask.getNode()))
4082 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
4083
Gabor Greif1c80d112008-08-28 21:40:38 +00004084 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004085 if (isMMX || NumElems < 4) return Op;
4086 // Promote it to a v4{if}32 splat.
4087 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004088 }
4089
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004090 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4091 // do it!
4092 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004093 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG,
4094 *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004095 if (NewOp.getNode())
Dale Johannesence0805b2009-02-03 19:33:06 +00004096 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4097 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004098 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4099 // FIXME: Figure out a cleaner way to do this.
4100 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004101 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004102 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Dale Johannesence0805b2009-02-03 19:33:06 +00004103 DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004104 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004105 SDValue NewV1 = NewOp.getOperand(0);
4106 SDValue NewV2 = NewOp.getOperand(1);
4107 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00004108 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004109 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Dale Johannesence0805b2009-02-03 19:33:06 +00004110 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget,
4111 dl);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004112 }
4113 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004114 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004115 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Dale Johannesence0805b2009-02-03 19:33:06 +00004116 DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004117 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004118 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Dale Johannesence0805b2009-02-03 19:33:06 +00004119 DAG, Subtarget, dl);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004120 }
4121 }
4122
Evan Chengdea99362008-05-29 08:22:04 +00004123 // Check if this can be converted into a logical shift.
4124 bool isLeft = false;
4125 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004126 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00004127 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4128 if (isShift && ShVal.hasOneUse()) {
4129 // If the shifted value has multiple uses, it may be cheaper to use
4130 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004131 MVT EVT = VT.getVectorElementType();
4132 ShAmt *= EVT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004133 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00004134 }
4135
Gabor Greif1c80d112008-08-28 21:40:38 +00004136 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004137 if (V1IsUndef)
4138 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004139 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00004140 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004141 if (!isMMX)
4142 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004143 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004144
Gabor Greif1c80d112008-08-28 21:40:38 +00004145 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4146 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4147 X86::isMOVHLPSMask(PermMask.getNode()) ||
4148 X86::isMOVHPMask(PermMask.getNode()) ||
4149 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004150 return Op;
4151
Gabor Greif1c80d112008-08-28 21:40:38 +00004152 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4153 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004154 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4155
Evan Chengdea99362008-05-29 08:22:04 +00004156 if (isShift) {
4157 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004158 MVT EVT = VT.getVectorElementType();
4159 ShAmt *= EVT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004160 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00004161 }
4162
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004163 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004164 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4165 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004166 V1IsSplat = isSplatVector(V1.getNode());
4167 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004168
4169 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004170 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4171 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4172 std::swap(V1IsSplat, V2IsSplat);
4173 std::swap(V1IsUndef, V2IsUndef);
4174 Commuted = true;
4175 }
4176
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004177 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004178 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004179 if (V2IsUndef) return V1;
4180 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4181 if (V2IsSplat) {
4182 // V2 is a splat, so the mask may be malformed. That is, it may point
4183 // to any V2 element. The instruction selectior won't like this. Get
4184 // a corrected mask and commute to form a proper MOVS{S|D}.
Dale Johannesence0805b2009-02-03 19:33:06 +00004185 SDValue NewMask = getMOVLMask(NumElems, DAG, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004186 if (NewMask.getNode() != PermMask.getNode())
Dale Johannesence0805b2009-02-03 19:33:06 +00004187 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, NewMask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004188 }
4189 return Op;
4190 }
4191
Gabor Greif1c80d112008-08-28 21:40:38 +00004192 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4193 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4194 X86::isUNPCKLMask(PermMask.getNode()) ||
4195 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004196 return Op;
4197
4198 if (V2IsSplat) {
4199 // Normalize mask so all entries that point to V2 points to its first
4200 // element then try to match unpck{h|l} again. If match, return a
4201 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004202 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004203 if (NewMask.getNode() != PermMask.getNode()) {
Mon P Wang56d91642009-02-04 01:16:59 +00004204 if (X86::isUNPCKLMask(NewMask.getNode(), true)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004205 SDValue NewMask = getUnpacklMask(NumElems, DAG, dl);
4206 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, NewMask);
Mon P Wang56d91642009-02-04 01:16:59 +00004207 } else if (X86::isUNPCKHMask(NewMask.getNode(), true)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004208 SDValue NewMask = getUnpackhMask(NumElems, DAG, dl);
4209 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, NewMask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004210 }
4211 }
4212 }
4213
4214 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004215 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004216 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4217
4218 if (Commuted) {
4219 // Commute is back and try unpck* again.
4220 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004221 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4222 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4223 X86::isUNPCKLMask(PermMask.getNode()) ||
4224 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004225 return Op;
4226 }
4227
Evan Chengbf8b2c52008-04-05 00:30:36 +00004228 // Try PSHUF* first, then SHUFP*.
4229 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4230 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004231 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004232 if (V2.getOpcode() != ISD::UNDEF)
Dale Johannesence0805b2009-02-03 19:33:06 +00004233 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00004234 DAG.getNode(ISD::UNDEF, VT), PermMask);
4235 return Op;
4236 }
4237
4238 if (!isMMX) {
4239 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004240 (X86::isPSHUFDMask(PermMask.getNode()) ||
4241 X86::isPSHUFHWMask(PermMask.getNode()) ||
4242 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004243 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004244 if (VT == MVT::v4f32) {
4245 RVT = MVT::v4i32;
Dale Johannesence0805b2009-02-03 19:33:06 +00004246 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, RVT,
4247 DAG.getNode(ISD::BIT_CONVERT, dl, RVT, V1),
4248 DAG.getNode(ISD::UNDEF, dl, RVT), PermMask);
Evan Chengbf8b2c52008-04-05 00:30:36 +00004249 } else if (V2.getOpcode() != ISD::UNDEF)
Dale Johannesence0805b2009-02-03 19:33:06 +00004250 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, RVT, V1,
4251 DAG.getNode(ISD::UNDEF, dl, RVT), PermMask);
Evan Chengbf8b2c52008-04-05 00:30:36 +00004252 if (RVT != VT)
Dale Johannesence0805b2009-02-03 19:33:06 +00004253 Op = DAG.getNode(ISD::BIT_CONVERT, dl, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004254 return Op;
4255 }
4256
Evan Chengbf8b2c52008-04-05 00:30:36 +00004257 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004258 if (X86::isSHUFPMask(PermMask.getNode()) ||
4259 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004260 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004261 }
4262
Evan Cheng75184a92007-12-11 01:46:18 +00004263 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4264 if (VT == MVT::v8i16) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004265 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004266 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004267 return NewOp;
4268 }
4269
Evan Chengf50554e2008-07-22 21:13:36 +00004270 // Handle all 4 wide cases with a number of shuffles except for MMX.
4271 if (NumElems == 4 && !isMMX)
Dale Johannesence0805b2009-02-03 19:33:06 +00004272 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004273
Dan Gohman8181bd12008-07-27 21:46:04 +00004274 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004275}
4276
Dan Gohman8181bd12008-07-27 21:46:04 +00004277SDValue
4278X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004279 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004280 MVT VT = Op.getValueType();
Dale Johannesence0805b2009-02-03 19:33:06 +00004281 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00004282 if (VT.getSizeInBits() == 8) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004283 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004284 Op.getOperand(0), Op.getOperand(1));
Dale Johannesence0805b2009-02-03 19:33:06 +00004285 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004286 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004287 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004288 } else if (VT.getSizeInBits() == 16) {
Evan Chengf9393b32009-01-02 05:29:08 +00004289 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4290 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4291 if (Idx == 0)
Dale Johannesence0805b2009-02-03 19:33:06 +00004292 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4293 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4294 DAG.getNode(ISD::BIT_CONVERT, dl,
4295 MVT::v4i32,
Evan Chengf9393b32009-01-02 05:29:08 +00004296 Op.getOperand(0)),
4297 Op.getOperand(1)));
Dale Johannesence0805b2009-02-03 19:33:06 +00004298 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004299 Op.getOperand(0), Op.getOperand(1));
Dale Johannesence0805b2009-02-03 19:33:06 +00004300 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004301 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004302 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004303 } else if (VT == MVT::f32) {
4304 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4305 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004306 // result has a single use which is a store or a bitcast to i32. And in
4307 // the case of a store, it's not worth it if the index is a constant 0,
4308 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004309 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004310 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004311 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004312 if ((User->getOpcode() != ISD::STORE ||
4313 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4314 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004315 (User->getOpcode() != ISD::BIT_CONVERT ||
4316 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004317 return SDValue();
Dale Johannesence0805b2009-02-03 19:33:06 +00004318 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4319 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
4320 Op.getOperand(0)),
4321 Op.getOperand(1));
4322 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004323 } else if (VT == MVT::i32) {
4324 // ExtractPS works with constant index.
4325 if (isa<ConstantSDNode>(Op.getOperand(1)))
4326 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004327 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004328 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004329}
4330
4331
Dan Gohman8181bd12008-07-27 21:46:04 +00004332SDValue
4333X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004334 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004335 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004336
Evan Cheng6c249332008-03-24 21:52:23 +00004337 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004338 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004339 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004340 return Res;
4341 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004342
Duncan Sands92c43912008-06-06 12:08:01 +00004343 MVT VT = Op.getValueType();
Dale Johannesence0805b2009-02-03 19:33:06 +00004344 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004345 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004346 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004347 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004348 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004349 if (Idx == 0)
Dale Johannesence0805b2009-02-03 19:33:06 +00004350 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4351 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4352 DAG.getNode(ISD::BIT_CONVERT, dl,
4353 MVT::v4i32, Vec),
Evan Cheng75184a92007-12-11 01:46:18 +00004354 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004355 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004356 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesence0805b2009-02-03 19:33:06 +00004357 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004358 Op.getOperand(0), Op.getOperand(1));
Dale Johannesence0805b2009-02-03 19:33:06 +00004359 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004360 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004361 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004362 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004363 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004364 if (Idx == 0)
4365 return Op;
4366 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004367 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004368 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004369 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004370 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004371 IdxVec.
Dale Johannesence0805b2009-02-03 19:33:06 +00004372 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004373 IdxVec.
Dale Johannesence0805b2009-02-03 19:33:06 +00004374 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004375 IdxVec.
Dale Johannesence0805b2009-02-03 19:33:06 +00004376 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
4377 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004378 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004379 SDValue Vec = Op.getOperand(0);
Dale Johannesence0805b2009-02-03 19:33:06 +00004380 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, Vec.getValueType(),
4381 Vec, DAG.getNode(ISD::UNDEF, dl, Vec.getValueType()),
4382 Mask);
4383 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004384 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004385 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004386 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4387 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4388 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004389 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004390 if (Idx == 0)
4391 return Op;
4392
4393 // UNPCKHPD the element to the lowest double word, then movsd.
4394 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4395 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004396 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004397 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004398 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004399 IdxVec.
Dale Johannesence0805b2009-02-03 19:33:06 +00004400 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
4401 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004402 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004403 SDValue Vec = Op.getOperand(0);
Dale Johannesence0805b2009-02-03 19:33:06 +00004404 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, Vec.getValueType(),
4405 Vec, DAG.getNode(ISD::UNDEF, dl, Vec.getValueType()),
4406 Mask);
4407 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004408 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004409 }
4410
Dan Gohman8181bd12008-07-27 21:46:04 +00004411 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004412}
4413
Dan Gohman8181bd12008-07-27 21:46:04 +00004414SDValue
4415X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004416 MVT VT = Op.getValueType();
4417 MVT EVT = VT.getVectorElementType();
Dale Johannesence0805b2009-02-03 19:33:06 +00004418 DebugLoc dl = Op.getNode()->getDebugLoc();
Nate Begemand77e59e2008-02-11 04:19:36 +00004419
Dan Gohman8181bd12008-07-27 21:46:04 +00004420 SDValue N0 = Op.getOperand(0);
4421 SDValue N1 = Op.getOperand(1);
4422 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004423
Dan Gohman5a7af042008-08-14 22:53:18 +00004424 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4425 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004426 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004427 : X86ISD::PINSRW;
4428 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4429 // argument.
4430 if (N1.getValueType() != MVT::i32)
Dale Johannesence0805b2009-02-03 19:33:06 +00004431 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begemand77e59e2008-02-11 04:19:36 +00004432 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004433 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesence0805b2009-02-03 19:33:06 +00004434 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004435 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004436 // Bits [7:6] of the constant are the source select. This will always be
4437 // zero here. The DAG Combiner may combine an extract_elt index into these
4438 // bits. For example (insert (extract, 3), 2) could be matched by putting
4439 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4440 // Bits [5:4] of the constant are the destination select. This is the
4441 // value of the incoming immediate.
4442 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4443 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004444 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00004445 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004446 } else if (EVT == MVT::i32) {
4447 // InsertPS works with constant index.
4448 if (isa<ConstantSDNode>(N2))
4449 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004450 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004451 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004452}
4453
Dan Gohman8181bd12008-07-27 21:46:04 +00004454SDValue
4455X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004456 MVT VT = Op.getValueType();
4457 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004458
4459 if (Subtarget->hasSSE41())
4460 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4461
Evan Chenge12a7eb2007-12-12 07:55:34 +00004462 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004463 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004464
Dale Johannesence0805b2009-02-03 19:33:06 +00004465 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00004466 SDValue N0 = Op.getOperand(0);
4467 SDValue N1 = Op.getOperand(1);
4468 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004469
Duncan Sands92c43912008-06-06 12:08:01 +00004470 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004471 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4472 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004473 if (N1.getValueType() != MVT::i32)
Dale Johannesence0805b2009-02-03 19:33:06 +00004474 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004475 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004476 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesence0805b2009-02-03 19:33:06 +00004477 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004478 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004479 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004480}
4481
Dan Gohman8181bd12008-07-27 21:46:04 +00004482SDValue
4483X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004484 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng759fe022008-07-22 18:39:19 +00004485 if (Op.getValueType() == MVT::v2f32)
Dale Johannesence0805b2009-02-03 19:33:06 +00004486 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4487 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4488 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng759fe022008-07-22 18:39:19 +00004489 Op.getOperand(0))));
4490
Dale Johannesence0805b2009-02-03 19:33:06 +00004491 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004492 MVT VT = MVT::v2i32;
4493 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004494 default: break;
4495 case MVT::v16i8:
4496 case MVT::v8i16:
4497 VT = MVT::v4i32;
4498 break;
4499 }
Dale Johannesence0805b2009-02-03 19:33:06 +00004500 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4501 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004502}
4503
Bill Wendlingfef06052008-09-16 21:48:12 +00004504// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4505// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4506// one of the above mentioned nodes. It has to be wrapped because otherwise
4507// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4508// be used to form addressing mode. These wrapped nodes will be selected
4509// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004510SDValue
4511X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004512 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004513 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004514 getPointerTy(),
4515 CP->getAlignment());
4516 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4517 // With PIC, the address is actually $g + Offset.
4518 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4519 !Subtarget->isPICStyleRIPRel()) {
4520 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4521 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4522 Result);
4523 }
4524
4525 return Result;
4526}
4527
Dan Gohman8181bd12008-07-27 21:46:04 +00004528SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004529X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
Dan Gohman36322c72008-10-18 02:06:02 +00004530 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00004531 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00004532 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4533 bool ExtraLoadRequired =
4534 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4535
4536 // Create the TargetGlobalAddress node, folding in the constant
4537 // offset if it is legal.
4538 SDValue Result;
Dan Gohman3d5257c2008-10-21 03:38:42 +00004539 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +00004540 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4541 Offset = 0;
4542 } else
4543 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004544 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00004545
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004546 // With PIC, the address is actually $g + Offset.
Dan Gohman36322c72008-10-18 02:06:02 +00004547 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004548 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4549 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4550 Result);
4551 }
4552
4553 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4554 // load the value at address GV, not the value of GV itself. This means that
4555 // the GlobalAddress must be in the base or index register of the address, not
4556 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4557 // The same applies for external symbols during PIC codegen
Dan Gohman36322c72008-10-18 02:06:02 +00004558 if (ExtraLoadRequired)
Dan Gohman12a9c082008-02-06 22:27:42 +00004559 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004560 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004561
Dan Gohman36322c72008-10-18 02:06:02 +00004562 // If there was a non-zero offset that we didn't fold, create an explicit
4563 // addition for it.
4564 if (Offset != 0)
4565 Result = DAG.getNode(ISD::ADD, getPointerTy(), Result,
4566 DAG.getConstant(Offset, getPointerTy()));
4567
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004568 return Result;
4569}
4570
Evan Cheng7f250d62008-09-24 00:05:32 +00004571SDValue
4572X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4573 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00004574 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
4575 return LowerGlobalAddress(GV, Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00004576}
4577
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004578// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004579static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004580LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004581 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004582 SDValue InFlag;
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00004583 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4584 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585 DAG.getNode(X86ISD::GlobalBaseReg,
4586 PtrVT), InFlag);
4587 InFlag = Chain.getValue(1);
4588
4589 // emit leal symbol@TLSGD(,%ebx,1), %eax
4590 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004591 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004592 GA->getValueType(0),
4593 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004594 SDValue Ops[] = { Chain, TGA, InFlag };
4595 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004596 InFlag = Result.getValue(2);
4597 Chain = Result.getValue(1);
4598
4599 // call ___tls_get_addr. This function receives its argument in
4600 // the register EAX.
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00004601 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Result, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004602 InFlag = Chain.getValue(1);
4603
4604 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004605 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004606 DAG.getTargetExternalSymbol("___tls_get_addr",
4607 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004608 DAG.getRegister(X86::EAX, PtrVT),
4609 DAG.getRegister(X86::EBX, PtrVT),
4610 InFlag };
4611 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4612 InFlag = Chain.getValue(1);
4613
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00004614 return DAG.getCopyFromReg(Chain, dl, X86::EAX, PtrVT, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004615}
4616
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004617// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004618static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004619LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004620 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004621 SDValue InFlag, Chain;
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00004622 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004623
4624 // emit leaq symbol@TLSGD(%rip), %rdi
4625 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004626 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004627 GA->getValueType(0),
4628 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004629 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4630 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004631 Chain = Result.getValue(1);
4632 InFlag = Result.getValue(2);
4633
aslb204cd52008-08-16 12:58:29 +00004634 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004635 // the register RDI.
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00004636 Chain = DAG.getCopyToReg(Chain, dl, X86::RDI, Result, InFlag);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004637 InFlag = Chain.getValue(1);
4638
4639 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004640 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004641 DAG.getTargetExternalSymbol("__tls_get_addr",
4642 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004643 DAG.getRegister(X86::RDI, PtrVT),
4644 InFlag };
4645 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4646 InFlag = Chain.getValue(1);
4647
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00004648 return DAG.getCopyFromReg(Chain, dl, X86::RAX, PtrVT, InFlag);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004649}
4650
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004651// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4652// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004653static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004654 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004655 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004656 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004657 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4658 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004659 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004660 GA->getValueType(0),
4661 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004662 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004663
4664 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004665 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004666 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004667
4668 // The address of the thread local variable is the add of the thread
4669 // pointer with the offset of the variable.
4670 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4671}
4672
Dan Gohman8181bd12008-07-27 21:46:04 +00004673SDValue
4674X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004675 // TODO: implement the "local dynamic" model
4676 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004677 assert(Subtarget->isTargetELF() &&
4678 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004679 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4680 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4681 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004682 if (Subtarget->is64Bit()) {
4683 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4684 } else {
4685 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4686 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4687 else
4688 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4689 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004690}
4691
Dan Gohman8181bd12008-07-27 21:46:04 +00004692SDValue
4693X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004694 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4695 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004696 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4697 // With PIC, the address is actually $g + Offset.
4698 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4699 !Subtarget->isPICStyleRIPRel()) {
4700 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4701 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4702 Result);
4703 }
4704
4705 return Result;
4706}
4707
Dan Gohman8181bd12008-07-27 21:46:04 +00004708SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004709 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004710 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004711 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4712 // With PIC, the address is actually $g + Offset.
4713 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4714 !Subtarget->isPICStyleRIPRel()) {
4715 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4716 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4717 Result);
4718 }
4719
4720 return Result;
4721}
4722
Chris Lattner62814a32007-10-17 06:02:13 +00004723/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4724/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004725SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004726 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004727 MVT VT = Op.getValueType();
4728 unsigned VTBits = VT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004729 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattner62814a32007-10-17 06:02:13 +00004730 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004731 SDValue ShOpLo = Op.getOperand(0);
4732 SDValue ShOpHi = Op.getOperand(1);
4733 SDValue ShAmt = Op.getOperand(2);
4734 SDValue Tmp1 = isSRA ?
Dale Johannesence0805b2009-02-03 19:33:06 +00004735 DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
4736 DAG.getConstant(VTBits - 1, MVT::i8)) :
Dan Gohman092014e2008-03-03 22:22:09 +00004737 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004738
Dan Gohman8181bd12008-07-27 21:46:04 +00004739 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004740 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004741 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4742 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004743 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00004744 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4745 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004746 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004747
Dale Johannesence0805b2009-02-03 19:33:06 +00004748 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004749 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesence0805b2009-02-03 19:33:06 +00004750 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004751 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004752
Dan Gohman8181bd12008-07-27 21:46:04 +00004753 SDValue Hi, Lo;
4754 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4755 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4756 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004757
Chris Lattner62814a32007-10-17 06:02:13 +00004758 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004759 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4760 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004761 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00004762 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4763 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004764 }
4765
Dan Gohman8181bd12008-07-27 21:46:04 +00004766 SDValue Ops[2] = { Lo, Hi };
Dale Johannesence0805b2009-02-03 19:33:06 +00004767 return DAG.getMergeValues(Ops, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004768}
4769
Dan Gohman8181bd12008-07-27 21:46:04 +00004770SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004771 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004772 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004773 "Unknown SINT_TO_FP to lower!");
4774
4775 // These are really Legal; caller falls through into that case.
4776 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004777 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004778 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4779 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004780 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004781
Dale Johannesence0805b2009-02-03 19:33:06 +00004782 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00004783 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004784 MachineFunction &MF = DAG.getMachineFunction();
4785 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004786 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00004787 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004788 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004789 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004790
4791 // Build the FILD
4792 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004793 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004794 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004795 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4796 else
4797 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004798 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004799 Ops.push_back(Chain);
4800 Ops.push_back(StackSlot);
4801 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004802 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004803 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004804
Dale Johannesen2fc20782007-09-14 22:26:36 +00004805 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004806 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004807 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004808
4809 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4810 // shouldn't be necessary except that RFP cannot be live across
4811 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4812 MachineFunction &MF = DAG.getMachineFunction();
4813 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004814 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004815 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004816 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004817 Ops.push_back(Chain);
4818 Ops.push_back(Result);
4819 Ops.push_back(StackSlot);
4820 Ops.push_back(DAG.getValueType(Op.getValueType()));
4821 Ops.push_back(InFlag);
Dale Johannesence0805b2009-02-03 19:33:06 +00004822 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4823 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004824 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004825 }
4826
4827 return Result;
4828}
4829
Bill Wendling14a30ef2009-01-17 03:56:04 +00004830// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4831SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4832 // This algorithm is not obvious. Here it is in C code, more or less:
4833 /*
4834 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4835 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4836 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesenfb019af2008-10-21 23:07:49 +00004837
Bill Wendling14a30ef2009-01-17 03:56:04 +00004838 // Copy ints to xmm registers.
4839 __m128i xh = _mm_cvtsi32_si128( hi );
4840 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004841
Bill Wendling14a30ef2009-01-17 03:56:04 +00004842 // Combine into low half of a single xmm register.
4843 __m128i x = _mm_unpacklo_epi32( xh, xl );
4844 __m128d d;
4845 double sd;
Dale Johannesenfb019af2008-10-21 23:07:49 +00004846
Bill Wendling14a30ef2009-01-17 03:56:04 +00004847 // Merge in appropriate exponents to give the integer bits the right
4848 // magnitude.
4849 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004850
Bill Wendling14a30ef2009-01-17 03:56:04 +00004851 // Subtract away the biases to deal with the IEEE-754 double precision
4852 // implicit 1.
4853 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004854
Bill Wendling14a30ef2009-01-17 03:56:04 +00004855 // All conversions up to here are exact. The correctly rounded result is
4856 // calculated using the current rounding mode using the following
4857 // horizontal add.
4858 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4859 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4860 // store doesn't really need to be here (except
4861 // maybe to zero the other double)
4862 return sd;
4863 }
4864 */
Dale Johannesenfb019af2008-10-21 23:07:49 +00004865
Dale Johannesence0805b2009-02-03 19:33:06 +00004866 DebugLoc dl = Op.getNode()->getDebugLoc();
4867
Dale Johannesena359b8b2008-10-21 20:50:01 +00004868 // Build some magic constants.
Bill Wendling14a30ef2009-01-17 03:56:04 +00004869 std::vector<Constant*> CV0;
Dale Johannesena359b8b2008-10-21 20:50:01 +00004870 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4871 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4872 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4873 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4874 Constant *C0 = ConstantVector::get(CV0);
4875 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 4);
4876
Bill Wendling14a30ef2009-01-17 03:56:04 +00004877 std::vector<Constant*> CV1;
Dale Johannesena359b8b2008-10-21 20:50:01 +00004878 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4879 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4880 Constant *C1 = ConstantVector::get(CV1);
4881 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 4);
4882
4883 SmallVector<SDValue, 4> MaskVec;
4884 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
4885 MaskVec.push_back(DAG.getConstant(4, MVT::i32));
4886 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
4887 MaskVec.push_back(DAG.getConstant(5, MVT::i32));
Dale Johannesence0805b2009-02-03 19:33:06 +00004888 SDValue UnpcklMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4889 &MaskVec[0], MaskVec.size());
Dale Johannesena359b8b2008-10-21 20:50:01 +00004890 SmallVector<SDValue, 4> MaskVec2;
Duncan Sandsca872ca2008-10-22 11:24:12 +00004891 MaskVec2.push_back(DAG.getConstant(1, MVT::i32));
4892 MaskVec2.push_back(DAG.getConstant(0, MVT::i32));
Dale Johannesence0805b2009-02-03 19:33:06 +00004893 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32,
4894 &MaskVec2[0], MaskVec2.size());
Dale Johannesena359b8b2008-10-21 20:50:01 +00004895
Dale Johannesence0805b2009-02-03 19:33:06 +00004896 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4897 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004898 Op.getOperand(0),
4899 DAG.getIntPtrConstant(1)));
Dale Johannesence0805b2009-02-03 19:33:06 +00004900 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4901 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004902 Op.getOperand(0),
4903 DAG.getIntPtrConstant(0)));
Dale Johannesence0805b2009-02-03 19:33:06 +00004904 SDValue Unpck1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v4i32,
Dale Johannesena359b8b2008-10-21 20:50:01 +00004905 XR1, XR2, UnpcklMask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004906 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004907 PseudoSourceValue::getConstantPool(), 0,
4908 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00004909 SDValue Unpck2 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v4i32,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004910 Unpck1, CLod0, UnpcklMask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004911 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4912 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004913 PseudoSourceValue::getConstantPool(), 0,
4914 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00004915 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling14a30ef2009-01-17 03:56:04 +00004916
Dale Johannesena359b8b2008-10-21 20:50:01 +00004917 // Add the halves; easiest way is to swap them into another reg first.
Dale Johannesence0805b2009-02-03 19:33:06 +00004918 SDValue Shuf = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v2f64,
Dale Johannesena359b8b2008-10-21 20:50:01 +00004919 Sub, Sub, ShufMask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004920 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4921 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesena359b8b2008-10-21 20:50:01 +00004922 DAG.getIntPtrConstant(0));
4923}
4924
Bill Wendling14a30ef2009-01-17 03:56:04 +00004925// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4926SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004927 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling14a30ef2009-01-17 03:56:04 +00004928 // FP constant to bias correct the final result.
4929 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4930 MVT::f64);
4931
4932 // Load the 32-bit value into an XMM register.
Dale Johannesence0805b2009-02-03 19:33:06 +00004933 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4934 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004935 Op.getOperand(0),
4936 DAG.getIntPtrConstant(0)));
4937
Dale Johannesence0805b2009-02-03 19:33:06 +00004938 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4939 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling14a30ef2009-01-17 03:56:04 +00004940 DAG.getIntPtrConstant(0));
4941
4942 // Or the load with the bias.
Dale Johannesence0805b2009-02-03 19:33:06 +00004943 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4944 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4945 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Chengc0ab5e52009-01-19 08:19:57 +00004946 MVT::v2f64, Load)),
Dale Johannesence0805b2009-02-03 19:33:06 +00004947 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4948 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Chengc0ab5e52009-01-19 08:19:57 +00004949 MVT::v2f64, Bias)));
Dale Johannesence0805b2009-02-03 19:33:06 +00004950 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4951 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling14a30ef2009-01-17 03:56:04 +00004952 DAG.getIntPtrConstant(0));
4953
4954 // Subtract the bias.
Dale Johannesence0805b2009-02-03 19:33:06 +00004955 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling14a30ef2009-01-17 03:56:04 +00004956
4957 // Handle final rounding.
Bill Wendlingdb547de2009-01-17 07:40:19 +00004958 MVT DestVT = Op.getValueType();
4959
4960 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004961 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendlingdb547de2009-01-17 07:40:19 +00004962 DAG.getIntPtrConstant(0));
4963 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004964 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendlingdb547de2009-01-17 07:40:19 +00004965 }
4966
4967 // Handle final rounding.
4968 return Sub;
Bill Wendling14a30ef2009-01-17 03:56:04 +00004969}
4970
4971SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Cheng44fd2392009-01-19 08:08:22 +00004972 SDValue N0 = Op.getOperand(0);
Dale Johannesence0805b2009-02-03 19:33:06 +00004973 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling14a30ef2009-01-17 03:56:04 +00004974
Evan Cheng44fd2392009-01-19 08:08:22 +00004975 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4976 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4977 // the optimization here.
4978 if (DAG.SignBitIsZero(N0))
Dale Johannesence0805b2009-02-03 19:33:06 +00004979 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Cheng44fd2392009-01-19 08:08:22 +00004980
4981 MVT SrcVT = N0.getValueType();
Bill Wendling14a30ef2009-01-17 03:56:04 +00004982 if (SrcVT == MVT::i64) {
4983 // We only handle SSE2 f64 target here; caller can handle the rest.
4984 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4985 return SDValue();
Bill Wendlingdb547de2009-01-17 07:40:19 +00004986
Bill Wendling14a30ef2009-01-17 03:56:04 +00004987 return LowerUINT_TO_FP_i64(Op, DAG);
4988 } else if (SrcVT == MVT::i32) {
Bill Wendling14a30ef2009-01-17 03:56:04 +00004989 return LowerUINT_TO_FP_i32(Op, DAG);
4990 }
4991
4992 assert(0 && "Unknown UINT_TO_FP to lower!");
4993 return SDValue();
4994}
4995
Dan Gohman8181bd12008-07-27 21:46:04 +00004996std::pair<SDValue,SDValue> X86TargetLowering::
4997FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004998 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004999 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
5000 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005001 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002
Dale Johannesen2fc20782007-09-14 22:26:36 +00005003 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00005004 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005005 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00005006 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00005007 if (Subtarget->is64Bit() &&
5008 Op.getValueType() == MVT::i64 &&
5009 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00005010 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00005011
Evan Cheng05441e62007-10-15 20:11:21 +00005012 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5013 // stack slot.
5014 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00005015 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00005016 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00005017 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005018 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00005019 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005020 default: assert(0 && "Invalid FP_TO_SINT to lower!");
5021 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5022 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5023 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005024 }
5025
Dan Gohman8181bd12008-07-27 21:46:04 +00005026 SDValue Chain = DAG.getEntryNode();
5027 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00005028 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005029 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesence0805b2009-02-03 19:33:06 +00005030 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005031 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005032 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00005033 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005034 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5035 };
Dale Johannesence0805b2009-02-03 19:33:06 +00005036 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005037 Chain = Value.getValue(1);
5038 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
5039 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5040 }
5041
5042 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00005043 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesence0805b2009-02-03 19:33:06 +00005044 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005045
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005046 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005047}
5048
Dan Gohman8181bd12008-07-27 21:46:04 +00005049SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
5050 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
5051 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00005052 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005053
5054 // Load the result.
Dale Johannesence0805b2009-02-03 19:33:06 +00005055 return DAG.getLoad(Op.getValueType(), Op.getNode()->getDebugLoc(),
5056 FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005057}
5058
Dan Gohman8181bd12008-07-27 21:46:04 +00005059SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005060 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00005061 MVT VT = Op.getValueType();
5062 MVT EltVT = VT;
5063 if (VT.isVector())
5064 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005065 std::vector<Constant*> CV;
5066 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005067 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005068 CV.push_back(C);
5069 CV.push_back(C);
5070 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005071 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005072 CV.push_back(C);
5073 CV.push_back(C);
5074 CV.push_back(C);
5075 CV.push_back(C);
5076 }
Dan Gohman11821702007-07-27 17:16:43 +00005077 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005078 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00005079 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005080 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005081 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005082 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005083}
5084
Dan Gohman8181bd12008-07-27 21:46:04 +00005085SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005086 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00005087 MVT VT = Op.getValueType();
5088 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00005089 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00005090 if (VT.isVector()) {
5091 EltVT = VT.getVectorElementType();
5092 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00005093 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005094 std::vector<Constant*> CV;
5095 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005096 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005097 CV.push_back(C);
5098 CV.push_back(C);
5099 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005100 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005101 CV.push_back(C);
5102 CV.push_back(C);
5103 CV.push_back(C);
5104 CV.push_back(C);
5105 }
Dan Gohman11821702007-07-27 17:16:43 +00005106 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005107 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00005108 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005109 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005110 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00005111 if (VT.isVector()) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005112 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
5113 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5114 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
5115 Op.getOperand(0)),
5116 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Cheng92b8f782007-07-19 23:36:01 +00005117 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00005118 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng92b8f782007-07-19 23:36:01 +00005119 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005120}
5121
Dan Gohman8181bd12008-07-27 21:46:04 +00005122SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
5123 SDValue Op0 = Op.getOperand(0);
5124 SDValue Op1 = Op.getOperand(1);
Dale Johannesence0805b2009-02-03 19:33:06 +00005125 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00005126 MVT VT = Op.getValueType();
5127 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005128
5129 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005130 if (SrcVT.bitsLT(VT)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005131 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005132 SrcVT = VT;
5133 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005134 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005135 if (SrcVT.bitsGT(VT)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005136 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005137 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005138 }
5139
5140 // At this point the operands and the result should have the same
5141 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005142
5143 // First get the sign bit of second operand.
5144 std::vector<Constant*> CV;
5145 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005146 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
5147 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005148 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005149 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
5150 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5151 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5152 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005153 }
Dan Gohman11821702007-07-27 17:16:43 +00005154 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005155 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00005156 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005157 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005158 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005159 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005160
5161 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005162 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005163 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesence0805b2009-02-03 19:33:06 +00005164 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5165 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005166 DAG.getConstant(32, MVT::i32));
Dale Johannesence0805b2009-02-03 19:33:06 +00005167 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5168 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00005169 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005170 }
5171
5172 // Clear first operand sign bit.
5173 CV.clear();
5174 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005175 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
5176 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005177 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005178 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
5179 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5180 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5181 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005182 }
Dan Gohman11821702007-07-27 17:16:43 +00005183 C = ConstantVector::get(CV);
5184 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00005185 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005186 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005187 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005188 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005189
5190 // Or the value with the sign bit.
Dale Johannesence0805b2009-02-03 19:33:06 +00005191 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005192}
5193
Dan Gohman8181bd12008-07-27 21:46:04 +00005194SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00005195 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00005196 SDValue Op0 = Op.getOperand(0);
5197 SDValue Op1 = Op.getOperand(1);
Dale Johannesence0805b2009-02-03 19:33:06 +00005198 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattner77a62312008-12-25 05:34:37 +00005199 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5200
Dan Gohman22cefb02009-01-29 01:59:02 +00005201 // Lower (X & (1 << N)) == 0 to BT(X, N).
5202 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5203 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman13dd9522009-01-13 23:25:30 +00005204 if (Op0.getOpcode() == ISD::AND &&
5205 Op0.hasOneUse() &&
5206 Op1.getOpcode() == ISD::Constant &&
Dan Gohman22cefb02009-01-29 01:59:02 +00005207 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattner77a62312008-12-25 05:34:37 +00005208 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohman22cefb02009-01-29 01:59:02 +00005209 SDValue LHS, RHS;
5210 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5211 if (ConstantSDNode *Op010C =
5212 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5213 if (Op010C->getZExtValue() == 1) {
5214 LHS = Op0.getOperand(0);
5215 RHS = Op0.getOperand(1).getOperand(1);
5216 }
5217 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5218 if (ConstantSDNode *Op000C =
5219 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5220 if (Op000C->getZExtValue() == 1) {
5221 LHS = Op0.getOperand(1);
5222 RHS = Op0.getOperand(0).getOperand(1);
5223 }
5224 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5225 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5226 SDValue AndLHS = Op0.getOperand(0);
5227 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5228 LHS = AndLHS.getOperand(0);
5229 RHS = AndLHS.getOperand(1);
5230 }
5231 }
Evan Cheng950aac02007-09-25 01:57:46 +00005232
Dan Gohman22cefb02009-01-29 01:59:02 +00005233 if (LHS.getNode()) {
Chris Lattner77a62312008-12-25 05:34:37 +00005234 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5235 // instruction. Since the shift amount is in-range-or-undefined, we know
5236 // that doing a bittest on the i16 value is ok. We extend to i32 because
5237 // the encoding for the i16 version is larger than the i32 version.
5238 if (LHS.getValueType() == MVT::i8)
Dale Johannesence0805b2009-02-03 19:33:06 +00005239 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattner77a62312008-12-25 05:34:37 +00005240
5241 // If the operand types disagree, extend the shift amount to match. Since
5242 // BT ignores high bits (like shifts) we can use anyextend.
5243 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesence0805b2009-02-03 19:33:06 +00005244 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohman22cefb02009-01-29 01:59:02 +00005245
Dale Johannesence0805b2009-02-03 19:33:06 +00005246 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005247 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesence0805b2009-02-03 19:33:06 +00005248 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner77a62312008-12-25 05:34:37 +00005249 DAG.getConstant(Cond, MVT::i8), BT);
5250 }
5251 }
5252
5253 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5254 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Chris Lattner60435922008-12-24 00:11:37 +00005255
Dale Johannesence0805b2009-02-03 19:33:06 +00005256 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5257 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner60435922008-12-24 00:11:37 +00005258 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005259}
5260
Dan Gohman8181bd12008-07-27 21:46:04 +00005261SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5262 SDValue Cond;
5263 SDValue Op0 = Op.getOperand(0);
5264 SDValue Op1 = Op.getOperand(1);
5265 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00005266 MVT VT = Op.getValueType();
5267 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5268 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesence0805b2009-02-03 19:33:06 +00005269 DebugLoc dl = Op.getNode()->getDebugLoc();
Nate Begeman03605a02008-07-17 16:51:19 +00005270
5271 if (isFP) {
5272 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00005273 MVT VT0 = Op0.getValueType();
5274 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5275 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00005276 bool Swap = false;
5277
5278 switch (SetCCOpcode) {
5279 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005280 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00005281 case ISD::SETEQ: SSECC = 0; break;
5282 case ISD::SETOGT:
5283 case ISD::SETGT: Swap = true; // Fallthrough
5284 case ISD::SETLT:
5285 case ISD::SETOLT: SSECC = 1; break;
5286 case ISD::SETOGE:
5287 case ISD::SETGE: Swap = true; // Fallthrough
5288 case ISD::SETLE:
5289 case ISD::SETOLE: SSECC = 2; break;
5290 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005291 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00005292 case ISD::SETNE: SSECC = 4; break;
5293 case ISD::SETULE: Swap = true;
5294 case ISD::SETUGE: SSECC = 5; break;
5295 case ISD::SETULT: Swap = true;
5296 case ISD::SETUGT: SSECC = 6; break;
5297 case ISD::SETO: SSECC = 7; break;
5298 }
5299 if (Swap)
5300 std::swap(Op0, Op1);
5301
Nate Begeman6357f9d2008-07-25 19:05:58 +00005302 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00005303 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00005304 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005305 SDValue UNORD, EQ;
Dale Johannesence0805b2009-02-03 19:33:06 +00005306 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5307 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5308 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begeman6357f9d2008-07-25 19:05:58 +00005309 }
5310 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005311 SDValue ORD, NEQ;
Dale Johannesence0805b2009-02-03 19:33:06 +00005312 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5313 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5314 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begeman6357f9d2008-07-25 19:05:58 +00005315 }
5316 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00005317 }
5318 // Handle all other FP comparisons here.
Dale Johannesence0805b2009-02-03 19:33:06 +00005319 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman03605a02008-07-17 16:51:19 +00005320 }
5321
5322 // We are handling one of the integer comparisons here. Since SSE only has
5323 // GT and EQ comparisons for integer, swapping operands and multiple
5324 // operations may be required for some comparisons.
5325 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5326 bool Swap = false, Invert = false, FlipSigns = false;
5327
5328 switch (VT.getSimpleVT()) {
5329 default: break;
5330 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5331 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5332 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5333 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5334 }
5335
5336 switch (SetCCOpcode) {
5337 default: break;
5338 case ISD::SETNE: Invert = true;
5339 case ISD::SETEQ: Opc = EQOpc; break;
5340 case ISD::SETLT: Swap = true;
5341 case ISD::SETGT: Opc = GTOpc; break;
5342 case ISD::SETGE: Swap = true;
5343 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5344 case ISD::SETULT: Swap = true;
5345 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5346 case ISD::SETUGE: Swap = true;
5347 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5348 }
5349 if (Swap)
5350 std::swap(Op0, Op1);
5351
5352 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5353 // bits of the inputs before performing those operations.
5354 if (FlipSigns) {
5355 MVT EltVT = VT.getVectorElementType();
Duncan Sands505ba942009-02-01 18:06:53 +00005356 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5357 EltVT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005358 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Dale Johannesence0805b2009-02-03 19:33:06 +00005359 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005360 SignBits.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00005361 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5362 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman03605a02008-07-17 16:51:19 +00005363 }
5364
Dale Johannesence0805b2009-02-03 19:33:06 +00005365 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005366
5367 // If the logical-not of the result is required, perform that now.
Bob Wilson81a42cf2009-01-22 17:39:32 +00005368 if (Invert)
Dale Johannesence0805b2009-02-03 19:33:06 +00005369 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson81a42cf2009-01-22 17:39:32 +00005370
Nate Begeman03605a02008-07-17 16:51:19 +00005371 return Result;
5372}
Evan Cheng950aac02007-09-25 01:57:46 +00005373
Evan Chengd580f022008-12-03 08:38:43 +00005374// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
5375static bool isX86LogicalCmp(unsigned Opc) {
5376 return Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI;
5377}
5378
Dan Gohman8181bd12008-07-27 21:46:04 +00005379SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005380 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005381 SDValue Cond = Op.getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005382 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00005383 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005384
5385 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005386 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005387
Evan Cheng50d37ab2007-10-08 22:16:29 +00005388 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5389 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005390 if (Cond.getOpcode() == X86ISD::SETCC) {
5391 CC = Cond.getOperand(0);
5392
Dan Gohman8181bd12008-07-27 21:46:04 +00005393 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005394 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005395 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005396
Evan Cheng50d37ab2007-10-08 22:16:29 +00005397 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005398 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005399 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005400 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005401
Dan Gohman22cefb02009-01-29 01:59:02 +00005402 if ((isX86LogicalCmp(Opc) && !IllegalFPCMov) || Opc == X86ISD::BT) { // FIXME
Evan Cheng50d37ab2007-10-08 22:16:29 +00005403 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005404 addTest = false;
5405 }
5406 }
5407
5408 if (addTest) {
5409 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005410 Cond= DAG.getNode(X86ISD::CMP, dl, MVT::i32, Cond,
5411 DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005412 }
5413
Duncan Sands92c43912008-06-06 12:08:01 +00005414 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005415 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005416 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005417 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5418 // condition is true.
5419 Ops.push_back(Op.getOperand(2));
5420 Ops.push_back(Op.getOperand(1));
5421 Ops.push_back(CC);
5422 Ops.push_back(Cond);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005423 return DAG.getNode(X86ISD::CMOV, dl, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005424}
5425
Evan Chengd580f022008-12-03 08:38:43 +00005426// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5427// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5428// from the AND / OR.
5429static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5430 Opc = Op.getOpcode();
5431 if (Opc != ISD::OR && Opc != ISD::AND)
5432 return false;
5433 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5434 Op.getOperand(0).hasOneUse() &&
5435 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5436 Op.getOperand(1).hasOneUse());
5437}
5438
Evan Cheng67f98b12009-02-02 08:19:07 +00005439// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5440// 1 and that the SETCC node has a single use.
Evan Cheng8c3af2c2009-02-02 08:07:36 +00005441static bool isXor1OfSetCC(SDValue Op) {
5442 if (Op.getOpcode() != ISD::XOR)
5443 return false;
5444 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5445 if (N1C && N1C->getAPIntValue() == 1) {
5446 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5447 Op.getOperand(0).hasOneUse();
5448 }
5449 return false;
5450}
5451
Dan Gohman8181bd12008-07-27 21:46:04 +00005452SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005453 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005454 SDValue Chain = Op.getOperand(0);
5455 SDValue Cond = Op.getOperand(1);
5456 SDValue Dest = Op.getOperand(2);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005457 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00005458 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005459
5460 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005461 Cond = LowerSETCC(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005462#if 0
5463 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingf5399032008-12-12 21:15:41 +00005464 else if (Cond.getOpcode() == X86ISD::ADD ||
5465 Cond.getOpcode() == X86ISD::SUB ||
5466 Cond.getOpcode() == X86ISD::SMUL ||
5467 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling7e04be62008-12-09 22:08:41 +00005468 Cond = LowerXALUO(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005469#endif
5470
Evan Cheng50d37ab2007-10-08 22:16:29 +00005471 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5472 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005473 if (Cond.getOpcode() == X86ISD::SETCC) {
5474 CC = Cond.getOperand(0);
5475
Dan Gohman8181bd12008-07-27 21:46:04 +00005476 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005477 unsigned Opc = Cmp.getOpcode();
Chris Lattner77a62312008-12-25 05:34:37 +00005478 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
5479 if (isX86LogicalCmp(Opc) || Opc == X86ISD::BT) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005480 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005481 addTest = false;
Bill Wendlingd3511522008-12-02 01:06:39 +00005482 } else {
Evan Chengd580f022008-12-03 08:38:43 +00005483 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling809e7bd2008-12-03 08:32:02 +00005484 default: break;
5485 case X86::COND_O:
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005486 case X86::COND_B:
Chris Lattner77a62312008-12-25 05:34:37 +00005487 // These can only come from an arithmetic instruction with overflow,
5488 // e.g. SADDO, UADDO.
Bill Wendling809e7bd2008-12-03 08:32:02 +00005489 Cond = Cond.getNode()->getOperand(1);
5490 addTest = false;
5491 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00005492 }
Evan Cheng950aac02007-09-25 01:57:46 +00005493 }
Evan Chengd580f022008-12-03 08:38:43 +00005494 } else {
5495 unsigned CondOpc;
5496 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5497 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5498 unsigned Opc = Cmp.getOpcode();
5499 if (CondOpc == ISD::OR) {
5500 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5501 // two branches instead of an explicit OR instruction with a
5502 // separate test.
5503 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5504 isX86LogicalCmp(Opc)) {
5505 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005506 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Chengd580f022008-12-03 08:38:43 +00005507 Chain, Dest, CC, Cmp);
5508 CC = Cond.getOperand(1).getOperand(0);
5509 Cond = Cmp;
5510 addTest = false;
5511 }
5512 } else { // ISD::AND
5513 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5514 // two branches instead of an explicit AND instruction with a
5515 // separate test. However, we only do this if this block doesn't
5516 // have a fall-through edge, because this requires an explicit
5517 // jmp when the condition is false.
5518 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5519 isX86LogicalCmp(Opc) &&
5520 Op.getNode()->hasOneUse()) {
5521 X86::CondCode CCode =
5522 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5523 CCode = X86::GetOppositeBranchCondition(CCode);
5524 CC = DAG.getConstant(CCode, MVT::i8);
5525 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5526 // Look for an unconditional branch following this conditional branch.
5527 // We need this because we need to reverse the successors in order
5528 // to implement FCMP_OEQ.
5529 if (User.getOpcode() == ISD::BR) {
5530 SDValue FalseBB = User.getOperand(1);
5531 SDValue NewBR =
5532 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5533 assert(NewBR == User);
5534 Dest = FalseBB;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005535
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005536 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Chengd580f022008-12-03 08:38:43 +00005537 Chain, Dest, CC, Cmp);
5538 X86::CondCode CCode =
5539 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5540 CCode = X86::GetOppositeBranchCondition(CCode);
5541 CC = DAG.getConstant(CCode, MVT::i8);
5542 Cond = Cmp;
5543 addTest = false;
5544 }
5545 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005546 }
Evan Cheng8c3af2c2009-02-02 08:07:36 +00005547 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5548 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5549 // It should be transformed during dag combiner except when the condition
5550 // is set by a arithmetics with overflow node.
5551 X86::CondCode CCode =
5552 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5553 CCode = X86::GetOppositeBranchCondition(CCode);
5554 CC = DAG.getConstant(CCode, MVT::i8);
5555 Cond = Cond.getOperand(0).getOperand(1);
5556 addTest = false;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005557 }
Evan Cheng950aac02007-09-25 01:57:46 +00005558 }
5559
5560 if (addTest) {
5561 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005562 Cond= DAG.getNode(X86ISD::CMP, dl, MVT::i32, Cond,
5563 DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005564 }
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005565 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005566 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005567}
5568
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005569
5570// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5571// Calls to _alloca is needed to probe the stack when allocating more than 4k
5572// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5573// that the guard pages used by the OS virtual memory manager are allocated in
5574// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005575SDValue
5576X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005577 SelectionDAG &DAG) {
5578 assert(Subtarget->isTargetCygMing() &&
5579 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005580 DebugLoc dl = Op.getNode()->getDebugLoc();
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005581
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005582 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005583 SDValue Chain = Op.getOperand(0);
5584 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005585 // FIXME: Ensure alignment here
5586
Dan Gohman8181bd12008-07-27 21:46:04 +00005587 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005588
Duncan Sands92c43912008-06-06 12:08:01 +00005589 MVT IntPtr = getPointerTy();
5590 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005591
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005592 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005593
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00005594 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005595 Flag = Chain.getValue(1);
5596
5597 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005598 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005599 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005600 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005601 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005602 Flag };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005603 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005604 Flag = Chain.getValue(1);
5605
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005606 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005607 DAG.getIntPtrConstant(0, true),
5608 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005609 Flag);
5610
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00005611 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005612
Dan Gohman8181bd12008-07-27 21:46:04 +00005613 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005614 return DAG.getMergeValues(Ops1, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005615}
5616
Dan Gohman8181bd12008-07-27 21:46:04 +00005617SDValue
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005618X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005619 SDValue Chain,
5620 SDValue Dst, SDValue Src,
5621 SDValue Size, unsigned Align,
5622 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005623 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005624 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005625
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005626 // If not DWORD aligned or size is more than the threshold, call the library.
5627 // The libc version is likely to be faster for these cases. It can use the
5628 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005629 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005630 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005631 ConstantSize->getZExtValue() >
5632 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005633 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005634
5635 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005636 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005637
Bill Wendling4b2e3782008-10-01 00:59:58 +00005638 if (const char *bzeroEntry = V &&
5639 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5640 MVT IntPtr = getPointerTy();
5641 const Type *IntPtrTy = TD->getIntPtrType();
5642 TargetLowering::ArgListTy Args;
5643 TargetLowering::ArgListEntry Entry;
5644 Entry.Node = Dst;
5645 Entry.Ty = IntPtrTy;
5646 Args.push_back(Entry);
5647 Entry.Node = Size;
5648 Args.push_back(Entry);
5649 std::pair<SDValue,SDValue> CallResult =
5650 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5651 CallingConv::C, false,
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005652 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling4b2e3782008-10-01 00:59:58 +00005653 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005654 }
5655
Dan Gohmane8b391e2008-04-12 04:36:06 +00005656 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005657 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005658 }
5659
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005660 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005661 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005662 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005663 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005664 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005665 unsigned BytesLeft = 0;
5666 bool TwoRepStos = false;
5667 if (ValC) {
5668 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005669 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005670
5671 // If the value is a constant, then we can potentially use larger sets.
5672 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005673 case 2: // WORD aligned
5674 AVT = MVT::i16;
5675 ValReg = X86::AX;
5676 Val = (Val << 8) | Val;
5677 break;
5678 case 0: // DWORD aligned
5679 AVT = MVT::i32;
5680 ValReg = X86::EAX;
5681 Val = (Val << 8) | Val;
5682 Val = (Val << 16) | Val;
5683 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5684 AVT = MVT::i64;
5685 ValReg = X86::RAX;
5686 Val = (Val << 32) | Val;
5687 }
5688 break;
5689 default: // Byte aligned
5690 AVT = MVT::i8;
5691 ValReg = X86::AL;
5692 Count = DAG.getIntPtrConstant(SizeVal);
5693 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005694 }
5695
Duncan Sandsec142ee2008-06-08 20:54:56 +00005696 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005697 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005698 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5699 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005700 }
5701
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005702 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005703 InFlag);
5704 InFlag = Chain.getValue(1);
5705 } else {
5706 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005707 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005708 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005709 InFlag = Chain.getValue(1);
5710 }
5711
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005712 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
5713 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005714 Count, InFlag);
5715 InFlag = Chain.getValue(1);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005716 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
5717 X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005718 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005719 InFlag = Chain.getValue(1);
5720
5721 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005722 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005723 Ops.push_back(Chain);
5724 Ops.push_back(DAG.getValueType(AVT));
5725 Ops.push_back(InFlag);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005726 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005727
5728 if (TwoRepStos) {
5729 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005730 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005731 MVT CVT = Count.getValueType();
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005732 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005733 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005734 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
5735 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005736 Left, InFlag);
5737 InFlag = Chain.getValue(1);
5738 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5739 Ops.clear();
5740 Ops.push_back(Chain);
5741 Ops.push_back(DAG.getValueType(MVT::i8));
5742 Ops.push_back(InFlag);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005743 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005744 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005745 // Handle the last 1 - 7 bytes.
5746 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005747 MVT AddrVT = Dst.getValueType();
5748 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005749
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005750 Chain = DAG.getMemset(Chain, dl,
5751 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005752 DAG.getConstant(Offset, AddrVT)),
5753 Src,
5754 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005755 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005756 }
5757
Dan Gohmane8b391e2008-04-12 04:36:06 +00005758 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005759 return Chain;
5760}
5761
Dan Gohman8181bd12008-07-27 21:46:04 +00005762SDValue
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005763X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005764 SDValue Chain, SDValue Dst, SDValue Src,
5765 SDValue Size, unsigned Align,
5766 bool AlwaysInline,
5767 const Value *DstSV, uint64_t DstSVOff,
5768 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005769 // This requires the copy size to be a constant, preferrably
5770 // within a subtarget-specific limit.
5771 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5772 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005773 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005774 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005775 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005776 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005777
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005778 /// If not DWORD aligned, call the library.
5779 if ((Align & 3) != 0)
5780 return SDValue();
5781
5782 // DWORD aligned
5783 MVT AVT = MVT::i32;
5784 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005785 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005786
Duncan Sands92c43912008-06-06 12:08:01 +00005787 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005788 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005789 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005790 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005791
Dan Gohman8181bd12008-07-27 21:46:04 +00005792 SDValue InFlag(0, 0);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005793 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
5794 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005795 Count, InFlag);
5796 InFlag = Chain.getValue(1);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005797 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
5798 X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005799 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005800 InFlag = Chain.getValue(1);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005801 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
5802 X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005803 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005804 InFlag = Chain.getValue(1);
5805
5806 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005807 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005808 Ops.push_back(Chain);
5809 Ops.push_back(DAG.getValueType(AVT));
5810 Ops.push_back(InFlag);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005811 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005812
Dan Gohman8181bd12008-07-27 21:46:04 +00005813 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005814 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005815 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005816 // Handle the last 1 - 7 bytes.
5817 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005818 MVT DstVT = Dst.getValueType();
5819 MVT SrcVT = Src.getValueType();
5820 MVT SizeVT = Size.getValueType();
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005821 Results.push_back(DAG.getMemcpy(Chain, dl,
5822 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005823 DAG.getConstant(Offset, DstVT)),
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005824 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005825 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005826 DAG.getConstant(BytesLeft, SizeVT),
5827 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005828 DstSV, DstSVOff + Offset,
5829 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005830 }
5831
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005832 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
5833 &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005834}
5835
Dan Gohman8181bd12008-07-27 21:46:04 +00005836SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005837 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005838 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005839
5840 if (!Subtarget->is64Bit()) {
5841 // vastart just stores the address of the VarArgsFrameIndex slot into the
5842 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005843 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005844 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005845 }
5846
5847 // __va_list_tag:
5848 // gp_offset (0 - 6 * 8)
5849 // fp_offset (48 - 48 + 8 * 16)
5850 // overflow_arg_area (point to parameters coming in memory).
5851 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005852 SmallVector<SDValue, 8> MemOps;
5853 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005854 // Store gp_offset
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005855 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005856 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005857 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005858 MemOps.push_back(Store);
5859
5860 // Store fp_offset
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005861 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5862 FIN, DAG.getIntPtrConstant(4));
5863 Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005864 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005865 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005866 MemOps.push_back(Store);
5867
5868 // Store ptr to overflow_arg_area
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005869 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5870 FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005871 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005872 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005873 MemOps.push_back(Store);
5874
5875 // Store ptr to reg_save_area.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005876 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5877 FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005878 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005879 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005880 MemOps.push_back(Store);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005881 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
5882 &MemOps[0], MemOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005883}
5884
Dan Gohman8181bd12008-07-27 21:46:04 +00005885SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005886 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5887 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005888 SDValue Chain = Op.getOperand(0);
5889 SDValue SrcPtr = Op.getOperand(1);
5890 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005891
5892 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5893 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005894 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005895}
5896
Dan Gohman8181bd12008-07-27 21:46:04 +00005897SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005898 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005899 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005900 SDValue Chain = Op.getOperand(0);
5901 SDValue DstPtr = Op.getOperand(1);
5902 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005903 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5904 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00005905 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005906
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00005907 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman840ff5c2008-04-18 20:55:41 +00005908 DAG.getIntPtrConstant(24), 8, false,
5909 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005910}
5911
Dan Gohman8181bd12008-07-27 21:46:04 +00005912SDValue
5913X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005914 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005915 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005916 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005917 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005918 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005919 case Intrinsic::x86_sse_comieq_ss:
5920 case Intrinsic::x86_sse_comilt_ss:
5921 case Intrinsic::x86_sse_comile_ss:
5922 case Intrinsic::x86_sse_comigt_ss:
5923 case Intrinsic::x86_sse_comige_ss:
5924 case Intrinsic::x86_sse_comineq_ss:
5925 case Intrinsic::x86_sse_ucomieq_ss:
5926 case Intrinsic::x86_sse_ucomilt_ss:
5927 case Intrinsic::x86_sse_ucomile_ss:
5928 case Intrinsic::x86_sse_ucomigt_ss:
5929 case Intrinsic::x86_sse_ucomige_ss:
5930 case Intrinsic::x86_sse_ucomineq_ss:
5931 case Intrinsic::x86_sse2_comieq_sd:
5932 case Intrinsic::x86_sse2_comilt_sd:
5933 case Intrinsic::x86_sse2_comile_sd:
5934 case Intrinsic::x86_sse2_comigt_sd:
5935 case Intrinsic::x86_sse2_comige_sd:
5936 case Intrinsic::x86_sse2_comineq_sd:
5937 case Intrinsic::x86_sse2_ucomieq_sd:
5938 case Intrinsic::x86_sse2_ucomilt_sd:
5939 case Intrinsic::x86_sse2_ucomile_sd:
5940 case Intrinsic::x86_sse2_ucomigt_sd:
5941 case Intrinsic::x86_sse2_ucomige_sd:
5942 case Intrinsic::x86_sse2_ucomineq_sd: {
5943 unsigned Opc = 0;
5944 ISD::CondCode CC = ISD::SETCC_INVALID;
5945 switch (IntNo) {
5946 default: break;
5947 case Intrinsic::x86_sse_comieq_ss:
5948 case Intrinsic::x86_sse2_comieq_sd:
5949 Opc = X86ISD::COMI;
5950 CC = ISD::SETEQ;
5951 break;
5952 case Intrinsic::x86_sse_comilt_ss:
5953 case Intrinsic::x86_sse2_comilt_sd:
5954 Opc = X86ISD::COMI;
5955 CC = ISD::SETLT;
5956 break;
5957 case Intrinsic::x86_sse_comile_ss:
5958 case Intrinsic::x86_sse2_comile_sd:
5959 Opc = X86ISD::COMI;
5960 CC = ISD::SETLE;
5961 break;
5962 case Intrinsic::x86_sse_comigt_ss:
5963 case Intrinsic::x86_sse2_comigt_sd:
5964 Opc = X86ISD::COMI;
5965 CC = ISD::SETGT;
5966 break;
5967 case Intrinsic::x86_sse_comige_ss:
5968 case Intrinsic::x86_sse2_comige_sd:
5969 Opc = X86ISD::COMI;
5970 CC = ISD::SETGE;
5971 break;
5972 case Intrinsic::x86_sse_comineq_ss:
5973 case Intrinsic::x86_sse2_comineq_sd:
5974 Opc = X86ISD::COMI;
5975 CC = ISD::SETNE;
5976 break;
5977 case Intrinsic::x86_sse_ucomieq_ss:
5978 case Intrinsic::x86_sse2_ucomieq_sd:
5979 Opc = X86ISD::UCOMI;
5980 CC = ISD::SETEQ;
5981 break;
5982 case Intrinsic::x86_sse_ucomilt_ss:
5983 case Intrinsic::x86_sse2_ucomilt_sd:
5984 Opc = X86ISD::UCOMI;
5985 CC = ISD::SETLT;
5986 break;
5987 case Intrinsic::x86_sse_ucomile_ss:
5988 case Intrinsic::x86_sse2_ucomile_sd:
5989 Opc = X86ISD::UCOMI;
5990 CC = ISD::SETLE;
5991 break;
5992 case Intrinsic::x86_sse_ucomigt_ss:
5993 case Intrinsic::x86_sse2_ucomigt_sd:
5994 Opc = X86ISD::UCOMI;
5995 CC = ISD::SETGT;
5996 break;
5997 case Intrinsic::x86_sse_ucomige_ss:
5998 case Intrinsic::x86_sse2_ucomige_sd:
5999 Opc = X86ISD::UCOMI;
6000 CC = ISD::SETGE;
6001 break;
6002 case Intrinsic::x86_sse_ucomineq_ss:
6003 case Intrinsic::x86_sse2_ucomineq_sd:
6004 Opc = X86ISD::UCOMI;
6005 CC = ISD::SETNE;
6006 break;
6007 }
6008
Dan Gohman8181bd12008-07-27 21:46:04 +00006009 SDValue LHS = Op.getOperand(1);
6010 SDValue RHS = Op.getOperand(2);
Chris Lattnerebb91142008-12-24 23:53:05 +00006011 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006012 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6013 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00006014 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006015 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006016 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006017
6018 // Fix vector shift instructions where the last operand is a non-immediate
6019 // i32 value.
6020 case Intrinsic::x86_sse2_pslli_w:
6021 case Intrinsic::x86_sse2_pslli_d:
6022 case Intrinsic::x86_sse2_pslli_q:
6023 case Intrinsic::x86_sse2_psrli_w:
6024 case Intrinsic::x86_sse2_psrli_d:
6025 case Intrinsic::x86_sse2_psrli_q:
6026 case Intrinsic::x86_sse2_psrai_w:
6027 case Intrinsic::x86_sse2_psrai_d:
6028 case Intrinsic::x86_mmx_pslli_w:
6029 case Intrinsic::x86_mmx_pslli_d:
6030 case Intrinsic::x86_mmx_pslli_q:
6031 case Intrinsic::x86_mmx_psrli_w:
6032 case Intrinsic::x86_mmx_psrli_d:
6033 case Intrinsic::x86_mmx_psrli_q:
6034 case Intrinsic::x86_mmx_psrai_w:
6035 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006036 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006037 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006038 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006039
6040 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006041 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006042 switch (IntNo) {
6043 case Intrinsic::x86_sse2_pslli_w:
6044 NewIntNo = Intrinsic::x86_sse2_psll_w;
6045 break;
6046 case Intrinsic::x86_sse2_pslli_d:
6047 NewIntNo = Intrinsic::x86_sse2_psll_d;
6048 break;
6049 case Intrinsic::x86_sse2_pslli_q:
6050 NewIntNo = Intrinsic::x86_sse2_psll_q;
6051 break;
6052 case Intrinsic::x86_sse2_psrli_w:
6053 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6054 break;
6055 case Intrinsic::x86_sse2_psrli_d:
6056 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6057 break;
6058 case Intrinsic::x86_sse2_psrli_q:
6059 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6060 break;
6061 case Intrinsic::x86_sse2_psrai_w:
6062 NewIntNo = Intrinsic::x86_sse2_psra_w;
6063 break;
6064 case Intrinsic::x86_sse2_psrai_d:
6065 NewIntNo = Intrinsic::x86_sse2_psra_d;
6066 break;
6067 default: {
6068 ShAmtVT = MVT::v2i32;
6069 switch (IntNo) {
6070 case Intrinsic::x86_mmx_pslli_w:
6071 NewIntNo = Intrinsic::x86_mmx_psll_w;
6072 break;
6073 case Intrinsic::x86_mmx_pslli_d:
6074 NewIntNo = Intrinsic::x86_mmx_psll_d;
6075 break;
6076 case Intrinsic::x86_mmx_pslli_q:
6077 NewIntNo = Intrinsic::x86_mmx_psll_q;
6078 break;
6079 case Intrinsic::x86_mmx_psrli_w:
6080 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6081 break;
6082 case Intrinsic::x86_mmx_psrli_d:
6083 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6084 break;
6085 case Intrinsic::x86_mmx_psrli_q:
6086 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6087 break;
6088 case Intrinsic::x86_mmx_psrai_w:
6089 NewIntNo = Intrinsic::x86_mmx_psra_w;
6090 break;
6091 case Intrinsic::x86_mmx_psrai_d:
6092 NewIntNo = Intrinsic::x86_mmx_psra_d;
6093 break;
6094 default: abort(); // Can't reach here.
6095 }
6096 break;
6097 }
6098 }
Duncan Sands92c43912008-06-06 12:08:01 +00006099 MVT VT = Op.getValueType();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006100 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6101 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6102 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006103 DAG.getConstant(NewIntNo, MVT::i32),
6104 Op.getOperand(1), ShAmt);
6105 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006106 }
6107}
6108
Dan Gohman8181bd12008-07-27 21:46:04 +00006109SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006110 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006111 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006112
6113 if (Depth > 0) {
6114 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6115 SDValue Offset =
6116 DAG.getConstant(TD->getPointerSize(),
6117 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006118 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
6119 DAG.getNode(ISD::ADD, dl, getPointerTy(),
6120 FrameAddr, Offset),
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006121 NULL, 0);
6122 }
6123
6124 // Just load the return address.
Dan Gohman8181bd12008-07-27 21:46:04 +00006125 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006126 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
6127 RetAddrFI, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006128}
6129
Dan Gohman8181bd12008-07-27 21:46:04 +00006130SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00006131 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6132 MFI->setFrameAddressIsTaken(true);
6133 MVT VT = Op.getValueType();
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006134 DebugLoc dl = Op.getNode()->getDebugLoc(); // FIXME probably not meaningful
Evan Cheng33633672008-09-27 01:56:22 +00006135 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6136 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006137 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng33633672008-09-27 01:56:22 +00006138 while (Depth--)
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006139 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng33633672008-09-27 01:56:22 +00006140 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006141}
6142
Dan Gohman8181bd12008-07-27 21:46:04 +00006143SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00006144 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006145 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006146}
6147
Dan Gohman8181bd12008-07-27 21:46:04 +00006148SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006149{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006150 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00006151 SDValue Chain = Op.getOperand(0);
6152 SDValue Offset = Op.getOperand(1);
6153 SDValue Handler = Op.getOperand(2);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006154 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006155
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006156 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6157 getPointerTy());
6158 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006159
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006160 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006161 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006162 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6163 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006164 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006165 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006166
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006167 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006168 MVT::Other,
6169 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006170}
6171
Dan Gohman8181bd12008-07-27 21:46:04 +00006172SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006173 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006174 SDValue Root = Op.getOperand(0);
6175 SDValue Trmp = Op.getOperand(1); // trampoline
6176 SDValue FPtr = Op.getOperand(2); // nested function
6177 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006178 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006179
Dan Gohman12a9c082008-02-06 22:27:42 +00006180 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006181
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006182 const X86InstrInfo *TII =
6183 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6184
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006185 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006186 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006187
6188 // Large code-model.
6189
6190 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6191 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6192
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006193 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6194 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006195
6196 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6197
6198 // Load the pointer to the nested function into R11.
6199 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00006200 SDValue Addr = Trmp;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006201 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6202 Addr, TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006203
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006204 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6205 DAG.getConstant(2, MVT::i64));
6206 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006207
6208 // Load the 'nest' parameter value into R10.
6209 // R10 is specified in X86CallingConv.td
6210 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006211 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6212 DAG.getConstant(10, MVT::i64));
6213 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6214 Addr, TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006215
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006216 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6217 DAG.getConstant(12, MVT::i64));
6218 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006219
6220 // Jump to the nested function.
6221 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006222 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6223 DAG.getConstant(20, MVT::i64));
6224 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6225 Addr, TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006226
6227 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006228 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6229 DAG.getConstant(22, MVT::i64));
6230 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006231 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006232
Dan Gohman8181bd12008-07-27 21:46:04 +00006233 SDValue Ops[] =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006234 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6235 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006236 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00006237 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006238 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6239 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00006240 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006241
6242 switch (CC) {
6243 default:
6244 assert(0 && "Unsupported calling convention");
6245 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006246 case CallingConv::X86_StdCall: {
6247 // Pass 'nest' parameter in ECX.
6248 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006249 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006250
6251 // Check that ECX wasn't needed by an 'inreg' parameter.
6252 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00006253 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006254
Chris Lattner1c8733e2008-03-12 17:45:29 +00006255 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006256 unsigned InRegCount = 0;
6257 unsigned Idx = 1;
6258
6259 for (FunctionType::param_iterator I = FTy->param_begin(),
6260 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00006261 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006262 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006263 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006264
6265 if (InRegCount > 2) {
6266 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6267 abort();
6268 }
6269 }
6270 break;
6271 }
6272 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00006273 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006274 // Pass 'nest' parameter in EAX.
6275 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006276 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006277 break;
6278 }
6279
Dan Gohman8181bd12008-07-27 21:46:04 +00006280 SDValue OutChains[4];
6281 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006282
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006283 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6284 DAG.getConstant(10, MVT::i32));
6285 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006286
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006287 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006288 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006289 OutChains[0] = DAG.getStore(Root, dl,
6290 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00006291 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006292
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006293 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6294 DAG.getConstant(1, MVT::i32));
6295 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006296
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006297 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006298 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6299 DAG.getConstant(5, MVT::i32));
6300 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006301 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006302
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006303 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6304 DAG.getConstant(6, MVT::i32));
6305 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006306
Dan Gohman8181bd12008-07-27 21:46:04 +00006307 SDValue Ops[] =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006308 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6309 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006310 }
6311}
6312
Dan Gohman8181bd12008-07-27 21:46:04 +00006313SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006314 /*
6315 The rounding mode is in bits 11:10 of FPSR, and has the following
6316 settings:
6317 00 Round to nearest
6318 01 Round to -inf
6319 10 Round to +inf
6320 11 Round to 0
6321
6322 FLT_ROUNDS, on the other hand, expects the following:
6323 -1 Undefined
6324 0 Round to 0
6325 1 Round to nearest
6326 2 Round to +inf
6327 3 Round to -inf
6328
6329 To perform the conversion, we do:
6330 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6331 */
6332
6333 MachineFunction &MF = DAG.getMachineFunction();
6334 const TargetMachine &TM = MF.getTarget();
6335 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6336 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00006337 MVT VT = Op.getValueType();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006338 DebugLoc dl = Op.getNode()->getDebugLoc();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006339
6340 // Save FP Control Word to stack slot
6341 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00006342 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006343
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006344 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00006345 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006346
6347 // Load FP Control Word from stack slot
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006348 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006349
6350 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00006351 SDValue CWD1 =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006352 DAG.getNode(ISD::SRL, dl, MVT::i16,
6353 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006354 CWD, DAG.getConstant(0x800, MVT::i16)),
6355 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006356 SDValue CWD2 =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006357 DAG.getNode(ISD::SRL, dl, MVT::i16,
6358 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006359 CWD, DAG.getConstant(0x400, MVT::i16)),
6360 DAG.getConstant(9, MVT::i8));
6361
Dan Gohman8181bd12008-07-27 21:46:04 +00006362 SDValue RetVal =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006363 DAG.getNode(ISD::AND, dl, MVT::i16,
6364 DAG.getNode(ISD::ADD, dl, MVT::i16,
6365 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006366 DAG.getConstant(1, MVT::i16)),
6367 DAG.getConstant(3, MVT::i16));
6368
6369
Duncan Sands92c43912008-06-06 12:08:01 +00006370 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006371 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6372}
6373
Dan Gohman8181bd12008-07-27 21:46:04 +00006374SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006375 MVT VT = Op.getValueType();
6376 MVT OpVT = VT;
6377 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006378 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng48679f42007-12-14 02:13:44 +00006379
6380 Op = Op.getOperand(0);
6381 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006382 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00006383 OpVT = MVT::i32;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006384 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006385 }
Evan Cheng48679f42007-12-14 02:13:44 +00006386
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006387 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6388 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006389 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006390
6391 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006392 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006393 Ops.push_back(Op);
6394 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6395 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6396 Ops.push_back(Op.getValue(1));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006397 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006398
6399 // Finally xor with NumBits-1.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006400 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006401
Evan Cheng48679f42007-12-14 02:13:44 +00006402 if (VT == MVT::i8)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006403 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006404 return Op;
6405}
6406
Dan Gohman8181bd12008-07-27 21:46:04 +00006407SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006408 MVT VT = Op.getValueType();
6409 MVT OpVT = VT;
6410 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006411 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng48679f42007-12-14 02:13:44 +00006412
6413 Op = Op.getOperand(0);
6414 if (VT == MVT::i8) {
6415 OpVT = MVT::i32;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006416 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006417 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006418
6419 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6420 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006421 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006422
6423 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006424 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006425 Ops.push_back(Op);
6426 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6427 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6428 Ops.push_back(Op.getValue(1));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006429 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006430
Evan Cheng48679f42007-12-14 02:13:44 +00006431 if (VT == MVT::i8)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006432 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006433 return Op;
6434}
6435
Mon P Wang14edb092008-12-18 21:42:19 +00006436SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6437 MVT VT = Op.getValueType();
6438 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006439 DebugLoc dl = Op.getNode()->getDebugLoc();
Mon P Wang14edb092008-12-18 21:42:19 +00006440
6441 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6442 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6443 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6444 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6445 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6446 //
6447 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6448 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6449 // return AloBlo + AloBhi + AhiBlo;
6450
6451 SDValue A = Op.getOperand(0);
6452 SDValue B = Op.getOperand(1);
6453
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006454 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006455 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6456 A, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006457 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006458 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6459 B, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006460 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006461 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6462 A, B);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006463 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006464 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6465 A, Bhi);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006466 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006467 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6468 Ahi, B);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006469 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006470 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6471 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006472 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006473 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6474 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006475 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6476 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wang14edb092008-12-18 21:42:19 +00006477 return Res;
6478}
6479
6480
Bill Wendling7e04be62008-12-09 22:08:41 +00006481SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6482 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6483 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendlingd3511522008-12-02 01:06:39 +00006484 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6485 // has only one use.
Bill Wendlingd06b4202008-11-26 22:37:40 +00006486 SDNode *N = Op.getNode();
Bill Wendlingd3511522008-12-02 01:06:39 +00006487 SDValue LHS = N->getOperand(0);
6488 SDValue RHS = N->getOperand(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00006489 unsigned BaseOp = 0;
6490 unsigned Cond = 0;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006491 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling7e04be62008-12-09 22:08:41 +00006492
6493 switch (Op.getOpcode()) {
6494 default: assert(0 && "Unknown ovf instruction!");
6495 case ISD::SADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006496 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00006497 Cond = X86::COND_O;
6498 break;
6499 case ISD::UADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006500 BaseOp = X86ISD::ADD;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006501 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006502 break;
6503 case ISD::SSUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006504 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00006505 Cond = X86::COND_O;
6506 break;
6507 case ISD::USUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006508 BaseOp = X86ISD::SUB;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006509 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006510 break;
6511 case ISD::SMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006512 BaseOp = X86ISD::SMUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00006513 Cond = X86::COND_O;
6514 break;
6515 case ISD::UMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006516 BaseOp = X86ISD::UMUL;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006517 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006518 break;
6519 }
Bill Wendlingd06b4202008-11-26 22:37:40 +00006520
Bill Wendlingd3511522008-12-02 01:06:39 +00006521 // Also sets EFLAGS.
6522 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006523 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendlingd06b4202008-11-26 22:37:40 +00006524
Bill Wendlingd3511522008-12-02 01:06:39 +00006525 SDValue SetCC =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006526 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendling35f1a9d2008-12-10 02:01:32 +00006527 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendlingd06b4202008-11-26 22:37:40 +00006528
Bill Wendlingd3511522008-12-02 01:06:39 +00006529 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6530 return Sum;
Bill Wendling4c134df2008-11-24 19:21:46 +00006531}
6532
Dan Gohman8181bd12008-07-27 21:46:04 +00006533SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006534 MVT T = Op.getValueType();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006535 DebugLoc dl = Op.getNode()->getDebugLoc();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00006536 unsigned Reg = 0;
6537 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006538 switch(T.getSimpleVT()) {
6539 default:
6540 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006541 case MVT::i8: Reg = X86::AL; size = 1; break;
6542 case MVT::i16: Reg = X86::AX; size = 2; break;
6543 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00006544 case MVT::i64:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006545 assert(Subtarget->is64Bit() && "Node not type legal!");
6546 Reg = X86::RAX; size = 8;
Andrew Lenharth81580822008-03-05 01:15:49 +00006547 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00006548 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006549 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00006550 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00006551 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006552 Op.getOperand(1),
6553 Op.getOperand(3),
6554 DAG.getTargetConstant(size, MVT::i8),
6555 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006556 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006557 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Dan Gohman8181bd12008-07-27 21:46:04 +00006558 SDValue cpOut =
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006559 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006560 return cpOut;
6561}
6562
Duncan Sands7d9834b2008-12-01 11:39:25 +00006563SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif825aa892008-08-28 23:19:51 +00006564 SelectionDAG &DAG) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006565 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharth81580822008-03-05 01:15:49 +00006566 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006567 SDValue TheChain = Op.getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006568 DebugLoc dl = Op.getNode()->getDebugLoc();
6569 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006570 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6571 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006572 rax.getValue(2));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006573 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006574 DAG.getConstant(32, MVT::i8));
6575 SDValue Ops[] = {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006576 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006577 rdx.getValue(1)
6578 };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006579 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesenf160d802008-10-02 18:53:47 +00006580}
6581
Dale Johannesen9011d872008-09-29 22:25:26 +00006582SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6583 SDNode *Node = Op.getNode();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006584 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen9011d872008-09-29 22:25:26 +00006585 MVT T = Node->getValueType(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006586 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006587 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006588 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006589 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen9011d872008-09-29 22:25:26 +00006590 Node->getOperand(0),
6591 Node->getOperand(1), negOp,
6592 cast<AtomicSDNode>(Node)->getSrcValue(),
6593 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006594}
6595
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006596/// LowerOperation - Provide custom lowering hooks for some operations.
6597///
Dan Gohman8181bd12008-07-27 21:46:04 +00006598SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006599 switch (Op.getOpcode()) {
6600 default: assert(0 && "Should not custom lower this!");
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006601 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6602 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006603 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6604 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6605 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6606 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6607 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6608 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6609 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6610 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006611 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006612 case ISD::SHL_PARTS:
6613 case ISD::SRA_PARTS:
6614 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6615 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00006616 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006617 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6618 case ISD::FABS: return LowerFABS(Op, DAG);
6619 case ISD::FNEG: return LowerFNEG(Op, DAG);
6620 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006621 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006622 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006623 case ISD::SELECT: return LowerSELECT(Op, DAG);
6624 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006625 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6626 case ISD::CALL: return LowerCALL(Op, DAG);
6627 case ISD::RET: return LowerRET(Op, DAG);
6628 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006629 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006630 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006631 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6632 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6633 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6634 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6635 case ISD::FRAME_TO_ARGS_OFFSET:
6636 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6637 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6638 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006639 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006640 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006641 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6642 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wang14edb092008-12-18 21:42:19 +00006643 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling7e04be62008-12-09 22:08:41 +00006644 case ISD::SADDO:
6645 case ISD::UADDO:
6646 case ISD::SSUBO:
6647 case ISD::USUBO:
6648 case ISD::SMULO:
6649 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006650 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006651 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006652}
6653
Duncan Sands7d9834b2008-12-01 11:39:25 +00006654void X86TargetLowering::
6655ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6656 SelectionDAG &DAG, unsigned NewOp) {
6657 MVT T = Node->getValueType(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006658 DebugLoc dl = Node->getDebugLoc();
Duncan Sands7d9834b2008-12-01 11:39:25 +00006659 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6660
6661 SDValue Chain = Node->getOperand(0);
6662 SDValue In1 = Node->getOperand(1);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006663 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006664 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006665 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006666 Node->getOperand(2), DAG.getIntPtrConstant(1));
6667 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6668 // have a MemOperand. Pass the info through as a normal operand.
6669 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6670 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6671 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006672 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006673 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006674 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006675 Results.push_back(Result.getValue(2));
6676}
6677
Duncan Sandsac496a12008-07-04 11:47:58 +00006678/// ReplaceNodeResults - Replace a node with an illegal result type
6679/// with a new node built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00006680void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6681 SmallVectorImpl<SDValue>&Results,
6682 SelectionDAG &DAG) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006683 DebugLoc dl = N->getDebugLoc();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006684 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006685 default:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006686 assert(false && "Do not know how to custom type legalize this operation!");
6687 return;
6688 case ISD::FP_TO_SINT: {
6689 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
6690 SDValue FIST = Vals.first, StackSlot = Vals.second;
6691 if (FIST.getNode() != 0) {
6692 MVT VT = N->getValueType(0);
6693 // Return a load from the stack slot.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006694 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006695 }
6696 return;
6697 }
6698 case ISD::READCYCLECOUNTER: {
6699 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6700 SDValue TheChain = N->getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006701 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006702 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
6703 rd.getValue(1));
6704 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006705 eax.getValue(2));
6706 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6707 SDValue Ops[] = { eax, edx };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006708 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006709 Results.push_back(edx.getValue(1));
6710 return;
6711 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006712 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006713 MVT T = N->getValueType(0);
6714 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6715 SDValue cpInL, cpInH;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006716 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006717 DAG.getConstant(0, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006718 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006719 DAG.getConstant(1, MVT::i32));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006720 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6721 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006722 cpInL.getValue(1));
6723 SDValue swapInL, swapInH;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006724 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006725 DAG.getConstant(0, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006726 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006727 DAG.getConstant(1, MVT::i32));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006728 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006729 cpInH.getValue(1));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006730 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006731 swapInL.getValue(1));
6732 SDValue Ops[] = { swapInH.getValue(0),
6733 N->getOperand(1),
6734 swapInH.getValue(1) };
6735 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006736 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006737 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6738 MVT::i32, Result.getValue(1));
6739 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6740 MVT::i32, cpOutL.getValue(2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006741 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006742 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006743 Results.push_back(cpOutH.getValue(1));
6744 return;
6745 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006746 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006747 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6748 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006749 case ISD::ATOMIC_LOAD_AND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006750 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6751 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006752 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006753 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6754 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006755 case ISD::ATOMIC_LOAD_OR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006756 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6757 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006758 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006759 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6760 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006761 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006762 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6763 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006764 case ISD::ATOMIC_SWAP:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006765 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6766 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006767 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006768}
6769
6770const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6771 switch (Opcode) {
6772 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006773 case X86ISD::BSF: return "X86ISD::BSF";
6774 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006775 case X86ISD::SHLD: return "X86ISD::SHLD";
6776 case X86ISD::SHRD: return "X86ISD::SHRD";
6777 case X86ISD::FAND: return "X86ISD::FAND";
6778 case X86ISD::FOR: return "X86ISD::FOR";
6779 case X86ISD::FXOR: return "X86ISD::FXOR";
6780 case X86ISD::FSRL: return "X86ISD::FSRL";
6781 case X86ISD::FILD: return "X86ISD::FILD";
6782 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6783 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6784 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6785 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6786 case X86ISD::FLD: return "X86ISD::FLD";
6787 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006788 case X86ISD::CALL: return "X86ISD::CALL";
6789 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6790 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohman7fe9b7f2008-12-23 22:45:23 +00006791 case X86ISD::BT: return "X86ISD::BT";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006792 case X86ISD::CMP: return "X86ISD::CMP";
6793 case X86ISD::COMI: return "X86ISD::COMI";
6794 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6795 case X86ISD::SETCC: return "X86ISD::SETCC";
6796 case X86ISD::CMOV: return "X86ISD::CMOV";
6797 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6798 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6799 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6800 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006801 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6802 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006803 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006804 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006805 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6806 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006807 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6808 case X86ISD::FMAX: return "X86ISD::FMAX";
6809 case X86ISD::FMIN: return "X86ISD::FMIN";
6810 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6811 case X86ISD::FRCP: return "X86ISD::FRCP";
6812 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6813 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6814 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006815 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006816 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006817 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6818 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006819 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6820 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6821 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6822 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6823 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6824 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006825 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6826 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006827 case X86ISD::VSHL: return "X86ISD::VSHL";
6828 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006829 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6830 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6831 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6832 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6833 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6834 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6835 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6836 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6837 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6838 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingae034ed2008-12-12 00:56:36 +00006839 case X86ISD::ADD: return "X86ISD::ADD";
6840 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingf5399032008-12-12 21:15:41 +00006841 case X86ISD::SMUL: return "X86ISD::SMUL";
6842 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006843 }
6844}
6845
6846// isLegalAddressingMode - Return true if the addressing mode represented
6847// by AM is legal for this target, for a load/store of the specified type.
6848bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6849 const Type *Ty) const {
6850 // X86 supports extremely general addressing modes.
6851
6852 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6853 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6854 return false;
6855
6856 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006857 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006858 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6859 return false;
Dale Johannesen64660e92008-12-05 21:47:27 +00006860 // If BaseGV requires a register, we cannot also have a BaseReg.
6861 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6862 AM.HasBaseReg)
6863 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006864
6865 // X86-64 only supports addr of globals in small code model.
6866 if (Subtarget->is64Bit()) {
6867 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6868 return false;
6869 // If lower 4G is not available, then we must use rip-relative addressing.
6870 if (AM.BaseOffs || AM.Scale > 1)
6871 return false;
6872 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006873 }
6874
6875 switch (AM.Scale) {
6876 case 0:
6877 case 1:
6878 case 2:
6879 case 4:
6880 case 8:
6881 // These scales always work.
6882 break;
6883 case 3:
6884 case 5:
6885 case 9:
6886 // These scales are formed with basereg+scalereg. Only accept if there is
6887 // no basereg yet.
6888 if (AM.HasBaseReg)
6889 return false;
6890 break;
6891 default: // Other stuff never works.
6892 return false;
6893 }
6894
6895 return true;
6896}
6897
6898
Evan Cheng27a820a2007-10-26 01:56:11 +00006899bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6900 if (!Ty1->isInteger() || !Ty2->isInteger())
6901 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006902 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6903 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006904 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006905 return false;
6906 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006907}
6908
Duncan Sands92c43912008-06-06 12:08:01 +00006909bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6910 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006911 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006912 unsigned NumBits1 = VT1.getSizeInBits();
6913 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006914 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006915 return false;
6916 return Subtarget->is64Bit() || NumBits1 < 64;
6917}
Evan Cheng27a820a2007-10-26 01:56:11 +00006918
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006919/// isShuffleMaskLegal - Targets can use this to indicate that they only
6920/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6921/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6922/// are assumed to be legal.
6923bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006924X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006925 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006926 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006927 return (Mask.getNode()->getNumOperands() <= 4 ||
6928 isIdentityMask(Mask.getNode()) ||
6929 isIdentityMask(Mask.getNode(), true) ||
6930 isSplatMask(Mask.getNode()) ||
6931 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6932 X86::isUNPCKLMask(Mask.getNode()) ||
6933 X86::isUNPCKHMask(Mask.getNode()) ||
6934 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6935 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006936}
6937
Dan Gohman48d5f062008-04-09 20:09:42 +00006938bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006939X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006940 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006941 unsigned NumElts = BVOps.size();
6942 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006943 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006944 if (NumElts == 2) return true;
6945 if (NumElts == 4) {
6946 return (isMOVLMask(&BVOps[0], 4) ||
6947 isCommutedMOVL(&BVOps[0], 4, true) ||
6948 isSHUFPMask(&BVOps[0], 4) ||
6949 isCommutedSHUFP(&BVOps[0], 4));
6950 }
6951 return false;
6952}
6953
6954//===----------------------------------------------------------------------===//
6955// X86 Scheduler Hooks
6956//===----------------------------------------------------------------------===//
6957
Mon P Wang078a62d2008-05-05 19:05:59 +00006958// private utility function
6959MachineBasicBlock *
6960X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6961 MachineBasicBlock *MBB,
6962 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006963 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006964 unsigned LoadOpc,
6965 unsigned CXchgOpc,
6966 unsigned copyOpc,
6967 unsigned notOpc,
6968 unsigned EAXreg,
6969 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006970 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006971 // For the atomic bitwise operator, we generate
6972 // thisMBB:
6973 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006974 // ld t1 = [bitinstr.addr]
6975 // op t2 = t1, [bitinstr.val]
6976 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006977 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6978 // bz newMBB
6979 // fallthrough -->nextMBB
6980 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6981 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006982 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006983 ++MBBIter;
6984
6985 /// First build the CFG
6986 MachineFunction *F = MBB->getParent();
6987 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006988 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6989 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6990 F->insert(MBBIter, newMBB);
6991 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006992
6993 // Move all successors to thisMBB to nextMBB
6994 nextMBB->transferSuccessors(thisMBB);
6995
6996 // Update thisMBB to fall through to newMBB
6997 thisMBB->addSuccessor(newMBB);
6998
6999 // newMBB jumps to itself and fall through to nextMBB
7000 newMBB->addSuccessor(nextMBB);
7001 newMBB->addSuccessor(newMBB);
7002
7003 // Insert instructions into newMBB based on incoming instruction
7004 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007005 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang078a62d2008-05-05 19:05:59 +00007006 MachineOperand& destOper = bInstr->getOperand(0);
7007 MachineOperand* argOpers[6];
7008 int numArgs = bInstr->getNumOperands() - 1;
7009 for (int i=0; i < numArgs; ++i)
7010 argOpers[i] = &bInstr->getOperand(i+1);
7011
7012 // x86 address has 4 operands: base, index, scale, and displacement
7013 int lastAddrIndx = 3; // [0,3]
7014 int valArgIndx = 4;
7015
Dale Johannesend20e4452008-08-19 18:47:28 +00007016 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007017 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00007018 for (int i=0; i <= lastAddrIndx; ++i)
7019 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007020
Dale Johannesend20e4452008-08-19 18:47:28 +00007021 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007022 if (invSrc) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007023 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007024 }
7025 else
7026 tt = t1;
7027
Dale Johannesend20e4452008-08-19 18:47:28 +00007028 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007029 assert((argOpers[valArgIndx]->isReg() ||
7030 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00007031 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007032 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007033 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00007034 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007035 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007036 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00007037 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007038
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007039 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00007040 MIB.addReg(t1);
7041
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007042 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00007043 for (int i=0; i <= lastAddrIndx; ++i)
7044 (*MIB).addOperand(*argOpers[i]);
7045 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00007046 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7047 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7048
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007049 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesend20e4452008-08-19 18:47:28 +00007050 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00007051
7052 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007053 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007054
Dan Gohman221a4372008-07-07 23:14:23 +00007055 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00007056 return nextMBB;
7057}
7058
Dale Johannesen44eb5372008-10-03 19:41:08 +00007059// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00007060MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00007061X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7062 MachineBasicBlock *MBB,
7063 unsigned regOpcL,
7064 unsigned regOpcH,
7065 unsigned immOpcL,
7066 unsigned immOpcH,
7067 bool invSrc) {
7068 // For the atomic bitwise operator, we generate
7069 // thisMBB (instructions are in pairs, except cmpxchg8b)
7070 // ld t1,t2 = [bitinstr.addr]
7071 // newMBB:
7072 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7073 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007074 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00007075 // mov ECX, EBX <- t5, t6
7076 // mov EAX, EDX <- t1, t2
7077 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7078 // mov t3, t4 <- EAX, EDX
7079 // bz newMBB
7080 // result in out1, out2
7081 // fallthrough -->nextMBB
7082
7083 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7084 const unsigned LoadOpc = X86::MOV32rm;
7085 const unsigned copyOpc = X86::MOV32rr;
7086 const unsigned NotOpc = X86::NOT32r;
7087 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7088 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7089 MachineFunction::iterator MBBIter = MBB;
7090 ++MBBIter;
7091
7092 /// First build the CFG
7093 MachineFunction *F = MBB->getParent();
7094 MachineBasicBlock *thisMBB = MBB;
7095 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7096 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7097 F->insert(MBBIter, newMBB);
7098 F->insert(MBBIter, nextMBB);
7099
7100 // Move all successors to thisMBB to nextMBB
7101 nextMBB->transferSuccessors(thisMBB);
7102
7103 // Update thisMBB to fall through to newMBB
7104 thisMBB->addSuccessor(newMBB);
7105
7106 // newMBB jumps to itself and fall through to nextMBB
7107 newMBB->addSuccessor(nextMBB);
7108 newMBB->addSuccessor(newMBB);
7109
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007110 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesenf160d802008-10-02 18:53:47 +00007111 // Insert instructions into newMBB based on incoming instruction
7112 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
7113 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
7114 MachineOperand& dest1Oper = bInstr->getOperand(0);
7115 MachineOperand& dest2Oper = bInstr->getOperand(1);
7116 MachineOperand* argOpers[6];
7117 for (int i=0; i < 6; ++i)
7118 argOpers[i] = &bInstr->getOperand(i+2);
7119
7120 // x86 address has 4 operands: base, index, scale, and displacement
7121 int lastAddrIndx = 3; // [0,3]
7122
7123 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007124 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesenf160d802008-10-02 18:53:47 +00007125 for (int i=0; i <= lastAddrIndx; ++i)
7126 (*MIB).addOperand(*argOpers[i]);
7127 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007128 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007129 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00007130 for (int i=0; i <= lastAddrIndx-1; ++i)
7131 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007132 MachineOperand newOp3 = *(argOpers[3]);
7133 if (newOp3.isImm())
7134 newOp3.setImm(newOp3.getImm()+4);
7135 else
7136 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00007137 (*MIB).addOperand(newOp3);
7138
7139 // t3/4 are defined later, at the bottom of the loop
7140 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7141 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007142 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007143 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007144 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007145 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7146
7147 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7148 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
7149 if (invSrc) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007150 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7151 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesenf160d802008-10-02 18:53:47 +00007152 } else {
7153 tt1 = t1;
7154 tt2 = t2;
7155 }
7156
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007157 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00007158 "invalid operand");
7159 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7160 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007161 if (argOpers[4]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007162 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesenf160d802008-10-02 18:53:47 +00007163 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007164 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007165 if (regOpcL != X86::MOV32rr)
7166 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00007167 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007168 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
7169 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
7170 if (argOpers[5]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007171 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesenf160d802008-10-02 18:53:47 +00007172 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007173 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007174 if (regOpcH != X86::MOV32rr)
7175 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00007176 (*MIB).addOperand(*argOpers[5]);
7177
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007178 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007179 MIB.addReg(t1);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007180 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007181 MIB.addReg(t2);
7182
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007183 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007184 MIB.addReg(t5);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007185 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007186 MIB.addReg(t6);
7187
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007188 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesenf160d802008-10-02 18:53:47 +00007189 for (int i=0; i <= lastAddrIndx; ++i)
7190 (*MIB).addOperand(*argOpers[i]);
7191
7192 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7193 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7194
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007195 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesenf160d802008-10-02 18:53:47 +00007196 MIB.addReg(X86::EAX);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007197 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesenf160d802008-10-02 18:53:47 +00007198 MIB.addReg(X86::EDX);
7199
7200 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007201 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesenf160d802008-10-02 18:53:47 +00007202
7203 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7204 return nextMBB;
7205}
7206
7207// private utility function
7208MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00007209X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7210 MachineBasicBlock *MBB,
7211 unsigned cmovOpc) {
7212 // For the atomic min/max operator, we generate
7213 // thisMBB:
7214 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00007215 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00007216 // mov t2 = [min/max.val]
7217 // cmp t1, t2
7218 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00007219 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00007220 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7221 // bz newMBB
7222 // fallthrough -->nextMBB
7223 //
7224 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7225 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007226 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00007227 ++MBBIter;
7228
7229 /// First build the CFG
7230 MachineFunction *F = MBB->getParent();
7231 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00007232 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7233 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7234 F->insert(MBBIter, newMBB);
7235 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007236
7237 // Move all successors to thisMBB to nextMBB
7238 nextMBB->transferSuccessors(thisMBB);
7239
7240 // Update thisMBB to fall through to newMBB
7241 thisMBB->addSuccessor(newMBB);
7242
7243 // newMBB jumps to newMBB and fall through to nextMBB
7244 newMBB->addSuccessor(nextMBB);
7245 newMBB->addSuccessor(newMBB);
7246
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007247 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang078a62d2008-05-05 19:05:59 +00007248 // Insert instructions into newMBB based on incoming instruction
7249 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
7250 MachineOperand& destOper = mInstr->getOperand(0);
7251 MachineOperand* argOpers[6];
7252 int numArgs = mInstr->getNumOperands() - 1;
7253 for (int i=0; i < numArgs; ++i)
7254 argOpers[i] = &mInstr->getOperand(i+1);
7255
7256 // x86 address has 4 operands: base, index, scale, and displacement
7257 int lastAddrIndx = 3; // [0,3]
7258 int valArgIndx = 4;
7259
Mon P Wang318b0372008-05-05 22:56:23 +00007260 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007261 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00007262 for (int i=0; i <= lastAddrIndx; ++i)
7263 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00007264
Mon P Wang078a62d2008-05-05 19:05:59 +00007265 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007266 assert((argOpers[valArgIndx]->isReg() ||
7267 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00007268 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00007269
7270 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007271 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007272 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00007273 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007274 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00007275 (*MIB).addOperand(*argOpers[valArgIndx]);
7276
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007277 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wang318b0372008-05-05 22:56:23 +00007278 MIB.addReg(t1);
7279
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007280 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang078a62d2008-05-05 19:05:59 +00007281 MIB.addReg(t1);
7282 MIB.addReg(t2);
7283
7284 // Generate movc
7285 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007286 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang078a62d2008-05-05 19:05:59 +00007287 MIB.addReg(t2);
7288 MIB.addReg(t1);
7289
7290 // Cmp and exchange if none has modified the memory location
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007291 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang078a62d2008-05-05 19:05:59 +00007292 for (int i=0; i <= lastAddrIndx; ++i)
7293 (*MIB).addOperand(*argOpers[i]);
7294 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00007295 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7296 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00007297
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007298 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang078a62d2008-05-05 19:05:59 +00007299 MIB.addReg(X86::EAX);
7300
7301 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007302 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007303
Dan Gohman221a4372008-07-07 23:14:23 +00007304 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00007305 return nextMBB;
7306}
7307
7308
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007309MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00007310X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
7311 MachineBasicBlock *BB) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007312 DebugLoc dl = MI->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007313 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7314 switch (MI->getOpcode()) {
7315 default: assert(false && "Unexpected instr type to insert");
Mon P Wang83edba52008-12-12 01:25:51 +00007316 case X86::CMOV_V1I64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007317 case X86::CMOV_FR32:
7318 case X86::CMOV_FR64:
7319 case X86::CMOV_V4F32:
7320 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00007321 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007322 // To "insert" a SELECT_CC instruction, we actually have to insert the
7323 // diamond control-flow pattern. The incoming instruction knows the
7324 // destination vreg to set, the condition code register to branch on, the
7325 // true/false values to select between, and a branch opcode to use.
7326 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007327 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007328 ++It;
7329
7330 // thisMBB:
7331 // ...
7332 // TrueVal = ...
7333 // cmpTY ccX, r1, r2
7334 // bCC copy1MBB
7335 // fallthrough --> copy0MBB
7336 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00007337 MachineFunction *F = BB->getParent();
7338 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7339 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007340 unsigned Opc =
7341 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007342 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00007343 F->insert(It, copy0MBB);
7344 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007345 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007346 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00007347 sinkMBB->transferSuccessors(BB);
7348
7349 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007350 BB->addSuccessor(copy0MBB);
7351 BB->addSuccessor(sinkMBB);
7352
7353 // copy0MBB:
7354 // %FalseValue = ...
7355 // # fallthrough to sinkMBB
7356 BB = copy0MBB;
7357
7358 // Update machine-CFG edges
7359 BB->addSuccessor(sinkMBB);
7360
7361 // sinkMBB:
7362 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7363 // ...
7364 BB = sinkMBB;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007365 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007366 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7367 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7368
Dan Gohman221a4372008-07-07 23:14:23 +00007369 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007370 return BB;
7371 }
7372
7373 case X86::FP32_TO_INT16_IN_MEM:
7374 case X86::FP32_TO_INT32_IN_MEM:
7375 case X86::FP32_TO_INT64_IN_MEM:
7376 case X86::FP64_TO_INT16_IN_MEM:
7377 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007378 case X86::FP64_TO_INT64_IN_MEM:
7379 case X86::FP80_TO_INT16_IN_MEM:
7380 case X86::FP80_TO_INT32_IN_MEM:
7381 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007382 // Change the floating point control register to use "round towards zero"
7383 // mode when truncating to an integer value.
7384 MachineFunction *F = BB->getParent();
7385 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007386 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007387
7388 // Load the old value of the high byte of the control word...
7389 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00007390 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007391 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
7392 CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007393
7394 // Set the high part to be round to zero...
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007395 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007396 .addImm(0xC7F);
7397
7398 // Reload the modified control word now...
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007399 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007400
7401 // Restore the memory image of control word to original value
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007402 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007403 .addReg(OldCW);
7404
7405 // Get the X86 opcode to use.
7406 unsigned Opc;
7407 switch (MI->getOpcode()) {
7408 default: assert(0 && "illegal opcode!");
7409 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7410 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7411 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7412 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7413 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7414 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007415 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7416 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7417 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007418 }
7419
7420 X86AddressMode AM;
7421 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007422 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007423 AM.BaseType = X86AddressMode::RegBase;
7424 AM.Base.Reg = Op.getReg();
7425 } else {
7426 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00007427 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007428 }
7429 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007430 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007431 AM.Scale = Op.getImm();
7432 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007433 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007434 AM.IndexReg = Op.getImm();
7435 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007436 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007437 AM.GV = Op.getGlobal();
7438 } else {
7439 AM.Disp = Op.getImm();
7440 }
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007441 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007442 .addReg(MI->getOperand(4).getReg());
7443
7444 // Reload the original control word now.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007445 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007446
Dan Gohman221a4372008-07-07 23:14:23 +00007447 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007448 return BB;
7449 }
Mon P Wang078a62d2008-05-05 19:05:59 +00007450 case X86::ATOMAND32:
7451 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007452 X86::AND32ri, X86::MOV32rm,
7453 X86::LCMPXCHG32, X86::MOV32rr,
7454 X86::NOT32r, X86::EAX,
7455 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007456 case X86::ATOMOR32:
7457 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007458 X86::OR32ri, X86::MOV32rm,
7459 X86::LCMPXCHG32, X86::MOV32rr,
7460 X86::NOT32r, X86::EAX,
7461 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007462 case X86::ATOMXOR32:
7463 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007464 X86::XOR32ri, X86::MOV32rm,
7465 X86::LCMPXCHG32, X86::MOV32rr,
7466 X86::NOT32r, X86::EAX,
7467 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007468 case X86::ATOMNAND32:
7469 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007470 X86::AND32ri, X86::MOV32rm,
7471 X86::LCMPXCHG32, X86::MOV32rr,
7472 X86::NOT32r, X86::EAX,
7473 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00007474 case X86::ATOMMIN32:
7475 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7476 case X86::ATOMMAX32:
7477 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7478 case X86::ATOMUMIN32:
7479 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7480 case X86::ATOMUMAX32:
7481 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00007482
7483 case X86::ATOMAND16:
7484 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7485 X86::AND16ri, X86::MOV16rm,
7486 X86::LCMPXCHG16, X86::MOV16rr,
7487 X86::NOT16r, X86::AX,
7488 X86::GR16RegisterClass);
7489 case X86::ATOMOR16:
7490 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7491 X86::OR16ri, X86::MOV16rm,
7492 X86::LCMPXCHG16, X86::MOV16rr,
7493 X86::NOT16r, X86::AX,
7494 X86::GR16RegisterClass);
7495 case X86::ATOMXOR16:
7496 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7497 X86::XOR16ri, X86::MOV16rm,
7498 X86::LCMPXCHG16, X86::MOV16rr,
7499 X86::NOT16r, X86::AX,
7500 X86::GR16RegisterClass);
7501 case X86::ATOMNAND16:
7502 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7503 X86::AND16ri, X86::MOV16rm,
7504 X86::LCMPXCHG16, X86::MOV16rr,
7505 X86::NOT16r, X86::AX,
7506 X86::GR16RegisterClass, true);
7507 case X86::ATOMMIN16:
7508 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7509 case X86::ATOMMAX16:
7510 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7511 case X86::ATOMUMIN16:
7512 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7513 case X86::ATOMUMAX16:
7514 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7515
7516 case X86::ATOMAND8:
7517 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7518 X86::AND8ri, X86::MOV8rm,
7519 X86::LCMPXCHG8, X86::MOV8rr,
7520 X86::NOT8r, X86::AL,
7521 X86::GR8RegisterClass);
7522 case X86::ATOMOR8:
7523 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7524 X86::OR8ri, X86::MOV8rm,
7525 X86::LCMPXCHG8, X86::MOV8rr,
7526 X86::NOT8r, X86::AL,
7527 X86::GR8RegisterClass);
7528 case X86::ATOMXOR8:
7529 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7530 X86::XOR8ri, X86::MOV8rm,
7531 X86::LCMPXCHG8, X86::MOV8rr,
7532 X86::NOT8r, X86::AL,
7533 X86::GR8RegisterClass);
7534 case X86::ATOMNAND8:
7535 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7536 X86::AND8ri, X86::MOV8rm,
7537 X86::LCMPXCHG8, X86::MOV8rr,
7538 X86::NOT8r, X86::AL,
7539 X86::GR8RegisterClass, true);
7540 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00007541 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007542 case X86::ATOMAND64:
7543 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7544 X86::AND64ri32, X86::MOV64rm,
7545 X86::LCMPXCHG64, X86::MOV64rr,
7546 X86::NOT64r, X86::RAX,
7547 X86::GR64RegisterClass);
7548 case X86::ATOMOR64:
7549 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7550 X86::OR64ri32, X86::MOV64rm,
7551 X86::LCMPXCHG64, X86::MOV64rr,
7552 X86::NOT64r, X86::RAX,
7553 X86::GR64RegisterClass);
7554 case X86::ATOMXOR64:
7555 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7556 X86::XOR64ri32, X86::MOV64rm,
7557 X86::LCMPXCHG64, X86::MOV64rr,
7558 X86::NOT64r, X86::RAX,
7559 X86::GR64RegisterClass);
7560 case X86::ATOMNAND64:
7561 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7562 X86::AND64ri32, X86::MOV64rm,
7563 X86::LCMPXCHG64, X86::MOV64rr,
7564 X86::NOT64r, X86::RAX,
7565 X86::GR64RegisterClass, true);
7566 case X86::ATOMMIN64:
7567 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7568 case X86::ATOMMAX64:
7569 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7570 case X86::ATOMUMIN64:
7571 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7572 case X86::ATOMUMAX64:
7573 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00007574
7575 // This group does 64-bit operations on a 32-bit host.
7576 case X86::ATOMAND6432:
7577 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7578 X86::AND32rr, X86::AND32rr,
7579 X86::AND32ri, X86::AND32ri,
7580 false);
7581 case X86::ATOMOR6432:
7582 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7583 X86::OR32rr, X86::OR32rr,
7584 X86::OR32ri, X86::OR32ri,
7585 false);
7586 case X86::ATOMXOR6432:
7587 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7588 X86::XOR32rr, X86::XOR32rr,
7589 X86::XOR32ri, X86::XOR32ri,
7590 false);
7591 case X86::ATOMNAND6432:
7592 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7593 X86::AND32rr, X86::AND32rr,
7594 X86::AND32ri, X86::AND32ri,
7595 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00007596 case X86::ATOMADD6432:
7597 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7598 X86::ADD32rr, X86::ADC32rr,
7599 X86::ADD32ri, X86::ADC32ri,
7600 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00007601 case X86::ATOMSUB6432:
7602 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7603 X86::SUB32rr, X86::SBB32rr,
7604 X86::SUB32ri, X86::SBB32ri,
7605 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007606 case X86::ATOMSWAP6432:
7607 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7608 X86::MOV32rr, X86::MOV32rr,
7609 X86::MOV32ri, X86::MOV32ri,
7610 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007611 }
7612}
7613
7614//===----------------------------------------------------------------------===//
7615// X86 Optimization Hooks
7616//===----------------------------------------------------------------------===//
7617
Dan Gohman8181bd12008-07-27 21:46:04 +00007618void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00007619 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00007620 APInt &KnownZero,
7621 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007622 const SelectionDAG &DAG,
7623 unsigned Depth) const {
7624 unsigned Opc = Op.getOpcode();
7625 assert((Opc >= ISD::BUILTIN_OP_END ||
7626 Opc == ISD::INTRINSIC_WO_CHAIN ||
7627 Opc == ISD::INTRINSIC_W_CHAIN ||
7628 Opc == ISD::INTRINSIC_VOID) &&
7629 "Should use MaskedValueIsZero if you don't know whether Op"
7630 " is a target node!");
7631
Dan Gohman1d79e432008-02-13 23:07:24 +00007632 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007633 switch (Opc) {
7634 default: break;
Evan Cheng8e9b21c2009-02-02 09:15:04 +00007635 case X86ISD::ADD:
7636 case X86ISD::SUB:
7637 case X86ISD::SMUL:
7638 case X86ISD::UMUL:
7639 // These nodes' second result is a boolean.
7640 if (Op.getResNo() == 0)
7641 break;
7642 // Fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007643 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007644 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7645 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007646 break;
7647 }
7648}
7649
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007650/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007651/// node is a GlobalAddress + offset.
7652bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7653 GlobalValue* &GA, int64_t &Offset) const{
7654 if (N->getOpcode() == X86ISD::Wrapper) {
7655 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007656 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00007657 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007658 return true;
7659 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007660 }
Evan Chengef7be082008-05-12 19:56:52 +00007661 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007662}
7663
Evan Chengef7be082008-05-12 19:56:52 +00007664static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7665 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007666 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007667 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007668 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007669 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007670 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007671 return false;
7672}
7673
Dan Gohman8181bd12008-07-27 21:46:04 +00007674static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007675 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007676 SDNode *&Base,
7677 SelectionDAG &DAG, MachineFrameInfo *MFI,
7678 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007679 Base = NULL;
7680 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007681 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007682 if (Idx.getOpcode() == ISD::UNDEF) {
7683 if (!Base)
7684 return false;
7685 continue;
7686 }
7687
Dan Gohman8181bd12008-07-27 21:46:04 +00007688 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007689 if (!Elt.getNode() ||
7690 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007691 return false;
7692 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007693 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007694 if (Base->getOpcode() == ISD::UNDEF)
7695 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007696 continue;
7697 }
7698 if (Elt.getOpcode() == ISD::UNDEF)
7699 continue;
7700
Gabor Greif1c80d112008-08-28 21:40:38 +00007701 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007702 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007703 return false;
7704 }
7705 return true;
7706}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007707
7708/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7709/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7710/// if the load addresses are consecutive, non-overlapping, and in the right
7711/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007712static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007713 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007714 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007715 DebugLoc dl = N->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00007716 MVT VT = N->getValueType(0);
7717 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007718 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007719 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007720 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007721 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7722 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007723 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007724
Dan Gohman11821702007-07-27 17:16:43 +00007725 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007726 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007727 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7728 LD->getSrcValue(), LD->getSrcValueOffset(),
7729 LD->isVolatile());
7730 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7731 LD->getSrcValue(), LD->getSrcValueOffset(),
7732 LD->isVolatile(), LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007733}
7734
Evan Chengb6290462008-05-12 23:04:07 +00007735/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007736static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohman22cefb02009-01-29 01:59:02 +00007737 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng6617eed2008-09-24 23:26:36 +00007738 const X86Subtarget *Subtarget,
7739 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007740 unsigned NumOps = N->getNumOperands();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007741 DebugLoc dl = N->getDebugLoc();
Evan Chengdea99362008-05-29 08:22:04 +00007742
Evan Chenge9b9c672008-05-09 21:53:03 +00007743 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007744 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007745 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007746
Duncan Sands92c43912008-06-06 12:08:01 +00007747 MVT VT = N->getValueType(0);
7748 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007749 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7750 // We are looking for load i64 and zero extend. We want to transform
7751 // it before legalizer has a chance to expand it. Also look for i64
7752 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007753 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007754 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007755 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007756 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007757 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007758
7759 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007760 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007761 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007762 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007763 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007764 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007765 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007766 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007767 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007768
7769 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007770 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007771
7772 // Load must not be an extload.
7773 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007774 return SDValue();
Mon P Wang28b36412009-01-30 07:07:40 +00007775
7776 // Load type should legal type so we don't have to legalize it.
7777 if (!TLI.isTypeLegal(VT))
7778 return SDValue();
7779
Evan Cheng6617eed2008-09-24 23:26:36 +00007780 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7781 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007782 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Dan Gohman22cefb02009-01-29 01:59:02 +00007783 TargetLowering::TargetLoweringOpt TLO(DAG);
7784 TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1));
7785 DCI.CommitTargetLoweringOpt(TLO);
Evan Cheng6617eed2008-09-24 23:26:36 +00007786 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007787}
7788
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007789/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007790static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007791 const X86Subtarget *Subtarget) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007792 DebugLoc dl = N->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00007793 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007794
7795 // If we have SSE[12] support, try to form min/max nodes.
7796 if (Subtarget->hasSSE2() &&
7797 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7798 if (Cond.getOpcode() == ISD::SETCC) {
7799 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007800 SDValue LHS = N->getOperand(1);
7801 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007802 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7803
7804 unsigned Opcode = 0;
7805 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7806 switch (CC) {
7807 default: break;
7808 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7809 case ISD::SETULE:
7810 case ISD::SETLE:
7811 if (!UnsafeFPMath) break;
7812 // FALL THROUGH.
7813 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7814 case ISD::SETLT:
7815 Opcode = X86ISD::FMIN;
7816 break;
7817
7818 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7819 case ISD::SETUGT:
7820 case ISD::SETGT:
7821 if (!UnsafeFPMath) break;
7822 // FALL THROUGH.
7823 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7824 case ISD::SETGE:
7825 Opcode = X86ISD::FMAX;
7826 break;
7827 }
7828 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7829 switch (CC) {
7830 default: break;
7831 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7832 case ISD::SETUGT:
7833 case ISD::SETGT:
7834 if (!UnsafeFPMath) break;
7835 // FALL THROUGH.
7836 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7837 case ISD::SETGE:
7838 Opcode = X86ISD::FMIN;
7839 break;
7840
7841 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7842 case ISD::SETULE:
7843 case ISD::SETLE:
7844 if (!UnsafeFPMath) break;
7845 // FALL THROUGH.
7846 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7847 case ISD::SETLT:
7848 Opcode = X86ISD::FMAX;
7849 break;
7850 }
7851 }
7852
7853 if (Opcode)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007854 return DAG.getNode(Opcode, dl, N->getValueType(0), LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007855 }
7856
7857 }
7858
Dan Gohman8181bd12008-07-27 21:46:04 +00007859 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007860}
7861
sampo025b75c2009-01-26 00:52:55 +00007862/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
7863/// when possible.
7864static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
7865 const X86Subtarget *Subtarget) {
7866 // On X86 with SSE2 support, we can transform this to a vector shift if
7867 // all elements are shifted by the same amount. We can't do this in legalize
7868 // because the a constant vector is typically transformed to a constant pool
7869 // so we have no knowledge of the shift amount.
sampo087d53c2009-01-26 03:15:31 +00007870 if (!Subtarget->hasSSE2())
7871 return SDValue();
7872
sampo025b75c2009-01-26 00:52:55 +00007873 MVT VT = N->getValueType(0);
sampo087d53c2009-01-26 03:15:31 +00007874 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
7875 return SDValue();
7876
Mon P Wanga91e9642009-01-28 08:12:05 +00007877 SDValue ShAmtOp = N->getOperand(1);
7878 MVT EltVT = VT.getVectorElementType();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007879 DebugLoc dl = N->getDebugLoc();
Mon P Wanga91e9642009-01-28 08:12:05 +00007880 SDValue BaseShAmt;
7881 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
7882 unsigned NumElts = VT.getVectorNumElements();
7883 unsigned i = 0;
7884 for (; i != NumElts; ++i) {
7885 SDValue Arg = ShAmtOp.getOperand(i);
7886 if (Arg.getOpcode() == ISD::UNDEF) continue;
7887 BaseShAmt = Arg;
7888 break;
7889 }
7890 for (; i != NumElts; ++i) {
7891 SDValue Arg = ShAmtOp.getOperand(i);
7892 if (Arg.getOpcode() == ISD::UNDEF) continue;
7893 if (Arg != BaseShAmt) {
7894 return SDValue();
7895 }
7896 }
7897 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
7898 isSplatMask(ShAmtOp.getOperand(2).getNode())) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007899 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, ShAmtOp,
Mon P Wanga91e9642009-01-28 08:12:05 +00007900 DAG.getIntPtrConstant(0));
7901 } else
sampo087d53c2009-01-26 03:15:31 +00007902 return SDValue();
sampo025b75c2009-01-26 00:52:55 +00007903
sampo087d53c2009-01-26 03:15:31 +00007904 if (EltVT.bitsGT(MVT::i32))
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007905 BaseShAmt = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007906 else if (EltVT.bitsLT(MVT::i32))
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007907 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BaseShAmt);
sampo025b75c2009-01-26 00:52:55 +00007908
sampo087d53c2009-01-26 03:15:31 +00007909 // The shift amount is identical so we can do a vector shift.
7910 SDValue ValOp = N->getOperand(0);
7911 switch (N->getOpcode()) {
7912 default:
7913 assert(0 && "Unknown shift opcode!");
7914 break;
7915 case ISD::SHL:
7916 if (VT == MVT::v2i64)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007917 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007918 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7919 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007920 if (VT == MVT::v4i32)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007921 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007922 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
7923 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007924 if (VT == MVT::v8i16)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007925 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007926 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
7927 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007928 break;
7929 case ISD::SRA:
7930 if (VT == MVT::v4i32)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007931 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007932 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
7933 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007934 if (VT == MVT::v8i16)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007935 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007936 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
7937 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007938 break;
7939 case ISD::SRL:
7940 if (VT == MVT::v2i64)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007941 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007942 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7943 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007944 if (VT == MVT::v4i32)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007945 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007946 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
7947 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007948 if (VT == MVT::v8i16)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007949 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007950 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
7951 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007952 break;
sampo025b75c2009-01-26 00:52:55 +00007953 }
7954 return SDValue();
7955}
7956
Chris Lattnerce84ae42008-02-22 02:09:43 +00007957/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007958static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007959 const X86Subtarget *Subtarget) {
7960 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7961 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007962 // A preferable solution to the general problem is to figure out the right
7963 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007964 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007965 if (St->getValue().getValueType().isVector() &&
7966 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007967 isa<LoadSDNode>(St->getValue()) &&
7968 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7969 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007970 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007971 LoadSDNode *Ld = 0;
7972 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007973 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007974 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007975 // Must be a store of a load. We currently handle two cases: the load
7976 // is a direct child, and it's under an intervening TokenFactor. It is
7977 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007978 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007979 Ld = cast<LoadSDNode>(St->getChain());
7980 else if (St->getValue().hasOneUse() &&
7981 ChainVal->getOpcode() == ISD::TokenFactor) {
7982 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007983 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007984 TokenFactorIndex = i;
7985 Ld = cast<LoadSDNode>(St->getValue());
7986 } else
7987 Ops.push_back(ChainVal->getOperand(i));
7988 }
7989 }
7990 if (Ld) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007991 DebugLoc dl = N->getDebugLoc();
Dale Johannesend112b802008-02-25 19:20:14 +00007992 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7993 if (Subtarget->is64Bit()) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007994 SDValue NewLd = DAG.getLoad(MVT::i64, dl, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007995 Ld->getBasePtr(), Ld->getSrcValue(),
7996 Ld->getSrcValueOffset(), Ld->isVolatile(),
7997 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007998 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007999 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00008000 Ops.push_back(NewChain);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008001 NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Ops[0],
Dale Johannesend112b802008-02-25 19:20:14 +00008002 Ops.size());
8003 }
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008004 return DAG.getStore(NewChain, dl, NewLd, St->getBasePtr(),
Dale Johannesend112b802008-02-25 19:20:14 +00008005 St->getSrcValue(), St->getSrcValueOffset(),
8006 St->isVolatile(), St->getAlignment());
8007 }
8008
8009 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00008010 SDValue LoAddr = Ld->getBasePtr();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008011 SDValue HiAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00008012 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00008013
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008014 SDValue LoLd = DAG.getLoad(MVT::i32, dl, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00008015 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8016 Ld->isVolatile(), Ld->getAlignment());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008017 SDValue HiLd = DAG.getLoad(MVT::i32, dl, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00008018 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8019 Ld->isVolatile(),
8020 MinAlign(Ld->getAlignment(), 4));
8021
Dan Gohman8181bd12008-07-27 21:46:04 +00008022 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00008023 if (TokenFactorIndex != -1) {
8024 Ops.push_back(LoLd);
8025 Ops.push_back(HiLd);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008026 NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Ops[0],
Dale Johannesend112b802008-02-25 19:20:14 +00008027 Ops.size());
8028 }
8029
8030 LoAddr = St->getBasePtr();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008031 HiAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00008032 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00008033
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008034 SDValue LoSt = DAG.getStore(NewChain, dl, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00008035 St->getSrcValue(), St->getSrcValueOffset(),
8036 St->isVolatile(), St->getAlignment());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008037 SDValue HiSt = DAG.getStore(NewChain, dl, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00008038 St->getSrcValue(),
8039 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00008040 St->isVolatile(),
8041 MinAlign(St->getAlignment(), 4));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008042 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00008043 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00008044 }
Dan Gohman8181bd12008-07-27 21:46:04 +00008045 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00008046}
8047
Chris Lattner470d5dc2008-01-25 06:14:17 +00008048/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8049/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00008050static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00008051 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8052 // F[X]OR(0.0, x) -> x
8053 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00008054 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8055 if (C->getValueAPF().isPosZero())
8056 return N->getOperand(1);
8057 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8058 if (C->getValueAPF().isPosZero())
8059 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00008060 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00008061}
8062
8063/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00008064static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00008065 // FAND(0.0, x) -> 0.0
8066 // FAND(x, 0.0) -> 0.0
8067 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8068 if (C->getValueAPF().isPosZero())
8069 return N->getOperand(0);
8070 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8071 if (C->getValueAPF().isPosZero())
8072 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00008073 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00008074}
8075
Dan Gohman22cefb02009-01-29 01:59:02 +00008076static SDValue PerformBTCombine(SDNode *N,
8077 SelectionDAG &DAG,
8078 TargetLowering::DAGCombinerInfo &DCI) {
8079 // BT ignores high bits in the bit index operand.
8080 SDValue Op1 = N->getOperand(1);
8081 if (Op1.hasOneUse()) {
8082 unsigned BitWidth = Op1.getValueSizeInBits();
8083 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8084 APInt KnownZero, KnownOne;
8085 TargetLowering::TargetLoweringOpt TLO(DAG);
8086 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8087 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8088 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8089 DCI.CommitTargetLoweringOpt(TLO);
8090 }
8091 return SDValue();
8092}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008093
Dan Gohman8181bd12008-07-27 21:46:04 +00008094SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00008095 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008096 SelectionDAG &DAG = DCI.DAG;
8097 switch (N->getOpcode()) {
8098 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00008099 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
8100 case ISD::BUILD_VECTOR:
Dan Gohman22cefb02009-01-29 01:59:02 +00008101 return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00008102 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
sampo025b75c2009-01-26 00:52:55 +00008103 case ISD::SHL:
8104 case ISD::SRA:
8105 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00008106 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00008107 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00008108 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8109 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohman22cefb02009-01-29 01:59:02 +00008110 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008111 }
8112
Dan Gohman8181bd12008-07-27 21:46:04 +00008113 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008114}
8115
8116//===----------------------------------------------------------------------===//
8117// X86 Inline Assembly Support
8118//===----------------------------------------------------------------------===//
8119
8120/// getConstraintType - Given a constraint letter, return the type of
8121/// constraint it is for this target.
8122X86TargetLowering::ConstraintType
8123X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8124 if (Constraint.size() == 1) {
8125 switch (Constraint[0]) {
8126 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +00008127 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +00008128 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008129 case 'r':
8130 case 'R':
8131 case 'l':
8132 case 'q':
8133 case 'Q':
8134 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00008135 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008136 case 'Y':
8137 return C_RegisterClass;
8138 default:
8139 break;
8140 }
8141 }
8142 return TargetLowering::getConstraintType(Constraint);
8143}
8144
Dale Johannesene99fc902008-01-29 02:21:21 +00008145/// LowerXConstraint - try to replace an X constraint, which matches anything,
8146/// with another that has more specific requirements based on the type of the
8147/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00008148const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00008149LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00008150 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8151 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00008152 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00008153 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00008154 return "Y";
8155 if (Subtarget->hasSSE1())
8156 return "x";
8157 }
8158
8159 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00008160}
8161
Chris Lattnera531abc2007-08-25 00:47:38 +00008162/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8163/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00008164void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00008165 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00008166 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00008167 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00008168 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00008169 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00008170
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008171 switch (Constraint) {
8172 default: break;
8173 case 'I':
8174 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008175 if (C->getZExtValue() <= 31) {
8176 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008177 break;
8178 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008179 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008180 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00008181 case 'J':
8182 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8183 if (C->getZExtValue() <= 63) {
8184 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8185 break;
8186 }
8187 }
8188 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008189 case 'N':
8190 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008191 if (C->getZExtValue() <= 255) {
8192 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008193 break;
8194 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008195 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008196 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008197 case 'i': {
8198 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00008199 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008200 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008201 break;
8202 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008203
8204 // If we are in non-pic codegen mode, we allow the address of a global (with
8205 // an optional displacement) to be used with 'i'.
8206 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
8207 int64_t Offset = 0;
8208
8209 // Match either (GA) or (GA+C)
8210 if (GA) {
8211 Offset = GA->getOffset();
8212 } else if (Op.getOpcode() == ISD::ADD) {
8213 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8214 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8215 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008216 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008217 } else {
8218 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8219 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8220 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008221 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008222 else
8223 C = 0, GA = 0;
8224 }
8225 }
8226
8227 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00008228 if (hasMemory)
Dan Gohman36322c72008-10-18 02:06:02 +00008229 Op = LowerGlobalAddress(GA->getGlobal(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00008230 else
8231 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8232 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00008233 Result = Op;
8234 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008235 }
8236
8237 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00008238 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008239 }
8240 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008241
Gabor Greif1c80d112008-08-28 21:40:38 +00008242 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00008243 Ops.push_back(Result);
8244 return;
8245 }
Evan Cheng7f250d62008-09-24 00:05:32 +00008246 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8247 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008248}
8249
8250std::vector<unsigned> X86TargetLowering::
8251getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00008252 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008253 if (Constraint.size() == 1) {
8254 // FIXME: not handling fp-stack yet!
8255 switch (Constraint[0]) { // GCC X86 Constraint Letters
8256 default: break; // Unknown constraint letter
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008257 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8258 case 'Q': // Q_REGS
8259 if (VT == MVT::i32)
8260 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8261 else if (VT == MVT::i16)
8262 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8263 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00008264 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00008265 else if (VT == MVT::i64)
8266 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8267 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008268 }
8269 }
8270
8271 return std::vector<unsigned>();
8272}
8273
8274std::pair<unsigned, const TargetRegisterClass*>
8275X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00008276 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008277 // First, see if this is a constraint that directly corresponds to an LLVM
8278 // register class.
8279 if (Constraint.size() == 1) {
8280 // GCC Constraint Letters
8281 switch (Constraint[0]) {
8282 default: break;
8283 case 'r': // GENERAL_REGS
8284 case 'R': // LEGACY_REGS
8285 case 'l': // INDEX_REGS
Chris Lattnerbbfea052008-10-17 18:15:05 +00008286 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008287 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00008288 if (VT == MVT::i16)
8289 return std::make_pair(0U, X86::GR16RegisterClass);
8290 if (VT == MVT::i32 || !Subtarget->is64Bit())
8291 return std::make_pair(0U, X86::GR32RegisterClass);
8292 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00008293 case 'f': // FP Stack registers.
8294 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8295 // value to the correct fpstack register class.
8296 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8297 return std::make_pair(0U, X86::RFP32RegisterClass);
8298 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8299 return std::make_pair(0U, X86::RFP64RegisterClass);
8300 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008301 case 'y': // MMX_REGS if MMX allowed.
8302 if (!Subtarget->hasMMX()) break;
8303 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008304 case 'Y': // SSE_REGS if SSE2 allowed
8305 if (!Subtarget->hasSSE2()) break;
8306 // FALL THROUGH.
8307 case 'x': // SSE_REGS if SSE1 allowed
8308 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00008309
8310 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008311 default: break;
8312 // Scalar SSE types.
8313 case MVT::f32:
8314 case MVT::i32:
8315 return std::make_pair(0U, X86::FR32RegisterClass);
8316 case MVT::f64:
8317 case MVT::i64:
8318 return std::make_pair(0U, X86::FR64RegisterClass);
8319 // Vector types.
8320 case MVT::v16i8:
8321 case MVT::v8i16:
8322 case MVT::v4i32:
8323 case MVT::v2i64:
8324 case MVT::v4f32:
8325 case MVT::v2f64:
8326 return std::make_pair(0U, X86::VR128RegisterClass);
8327 }
8328 break;
8329 }
8330 }
8331
8332 // Use the default implementation in TargetLowering to convert the register
8333 // constraint into a member of a register class.
8334 std::pair<unsigned, const TargetRegisterClass*> Res;
8335 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8336
8337 // Not found as a standard register?
8338 if (Res.second == 0) {
8339 // GCC calls "st(0)" just plain "st".
8340 if (StringsEqualNoCase("{st}", Constraint)) {
8341 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00008342 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008343 }
Dale Johannesen73920c02008-11-13 21:52:36 +00008344 // 'A' means EAX + EDX.
8345 if (Constraint == "A") {
8346 Res.first = X86::EAX;
8347 Res.second = X86::GRADRegisterClass;
8348 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008349 return Res;
8350 }
8351
8352 // Otherwise, check to see if this is a register class of the wrong value
8353 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8354 // turn into {ax},{dx}.
8355 if (Res.second->hasType(VT))
8356 return Res; // Correct type already, nothing to do.
8357
8358 // All of the single-register GCC register classes map their values onto
8359 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8360 // really want an 8-bit or 32-bit register, map to the appropriate register
8361 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00008362 if (Res.second == X86::GR16RegisterClass) {
8363 if (VT == MVT::i8) {
8364 unsigned DestReg = 0;
8365 switch (Res.first) {
8366 default: break;
8367 case X86::AX: DestReg = X86::AL; break;
8368 case X86::DX: DestReg = X86::DL; break;
8369 case X86::CX: DestReg = X86::CL; break;
8370 case X86::BX: DestReg = X86::BL; break;
8371 }
8372 if (DestReg) {
8373 Res.first = DestReg;
8374 Res.second = Res.second = X86::GR8RegisterClass;
8375 }
8376 } else if (VT == MVT::i32) {
8377 unsigned DestReg = 0;
8378 switch (Res.first) {
8379 default: break;
8380 case X86::AX: DestReg = X86::EAX; break;
8381 case X86::DX: DestReg = X86::EDX; break;
8382 case X86::CX: DestReg = X86::ECX; break;
8383 case X86::BX: DestReg = X86::EBX; break;
8384 case X86::SI: DestReg = X86::ESI; break;
8385 case X86::DI: DestReg = X86::EDI; break;
8386 case X86::BP: DestReg = X86::EBP; break;
8387 case X86::SP: DestReg = X86::ESP; break;
8388 }
8389 if (DestReg) {
8390 Res.first = DestReg;
8391 Res.second = Res.second = X86::GR32RegisterClass;
8392 }
8393 } else if (VT == MVT::i64) {
8394 unsigned DestReg = 0;
8395 switch (Res.first) {
8396 default: break;
8397 case X86::AX: DestReg = X86::RAX; break;
8398 case X86::DX: DestReg = X86::RDX; break;
8399 case X86::CX: DestReg = X86::RCX; break;
8400 case X86::BX: DestReg = X86::RBX; break;
8401 case X86::SI: DestReg = X86::RSI; break;
8402 case X86::DI: DestReg = X86::RDI; break;
8403 case X86::BP: DestReg = X86::RBP; break;
8404 case X86::SP: DestReg = X86::RSP; break;
8405 }
8406 if (DestReg) {
8407 Res.first = DestReg;
8408 Res.second = Res.second = X86::GR64RegisterClass;
8409 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008410 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00008411 } else if (Res.second == X86::FR32RegisterClass ||
8412 Res.second == X86::FR64RegisterClass ||
8413 Res.second == X86::VR128RegisterClass) {
8414 // Handle references to XMM physical registers that got mapped into the
8415 // wrong class. This can happen with constraints like {xmm0} where the
8416 // target independent register mapper will just pick the first match it can
8417 // find, ignoring the required type.
8418 if (VT == MVT::f32)
8419 Res.second = X86::FR32RegisterClass;
8420 else if (VT == MVT::f64)
8421 Res.second = X86::FR64RegisterClass;
8422 else if (X86::VR128RegisterClass->hasType(VT))
8423 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008424 }
8425
8426 return Res;
8427}
Mon P Wang1448aad2008-10-30 08:01:45 +00008428
8429//===----------------------------------------------------------------------===//
8430// X86 Widen vector type
8431//===----------------------------------------------------------------------===//
8432
8433/// getWidenVectorType: given a vector type, returns the type to widen
8434/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8435/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +00008436/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +00008437/// scalarizing vs using the wider vector type.
8438
Dan Gohman0fe66c92009-01-15 17:34:08 +00008439MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang1448aad2008-10-30 08:01:45 +00008440 assert(VT.isVector());
8441 if (isTypeLegal(VT))
8442 return VT;
8443
8444 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8445 // type based on element type. This would speed up our search (though
8446 // it may not be worth it since the size of the list is relatively
8447 // small).
8448 MVT EltVT = VT.getVectorElementType();
8449 unsigned NElts = VT.getVectorNumElements();
8450
8451 // On X86, it make sense to widen any vector wider than 1
8452 if (NElts <= 1)
8453 return MVT::Other;
8454
8455 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
8456 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8457 MVT SVT = (MVT::SimpleValueType)nVT;
8458
8459 if (isTypeLegal(SVT) &&
8460 SVT.getVectorElementType() == EltVT &&
8461 SVT.getVectorNumElements() > NElts)
8462 return SVT;
8463 }
8464 return MVT::Other;
8465}