blob: 416029b6b491625dfea7805e63c0d41868567d90 [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) {
2320 if (isUndefOrEqual(BitI1, NumElts))
2321 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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002655 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002656 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002657 Mask |= (Val - 4);
2658 if (i != 4)
2659 Mask <<= 2;
2660 }
2661
2662 return Mask;
2663}
2664
2665/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2666/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2667/// instructions.
2668unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2669 unsigned Mask = 0;
2670 // 8 nodes, but we only care about the first 4.
2671 for (int i = 3; i >= 0; --i) {
2672 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002673 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002674 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002675 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002676 Mask |= Val;
2677 if (i != 0)
2678 Mask <<= 2;
2679 }
2680
2681 return Mask;
2682}
2683
2684/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2685/// specifies a 8 element shuffle that can be broken into a pair of
2686/// PSHUFHW and PSHUFLW.
2687static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2688 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2689
2690 if (N->getNumOperands() != 8)
2691 return false;
2692
2693 // Lower quadword shuffled.
2694 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002695 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002696 if (Arg.getOpcode() == ISD::UNDEF) continue;
2697 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002698 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002699 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002700 return false;
2701 }
2702
2703 // Upper quadword shuffled.
2704 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002705 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002706 if (Arg.getOpcode() == ISD::UNDEF) continue;
2707 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002708 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002709 if (Val < 4 || Val > 7)
2710 return false;
2711 }
2712
2713 return true;
2714}
2715
Chris Lattnere6aa3862007-11-25 00:24:49 +00002716/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002717/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002718static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2719 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002720 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002721 MVT VT = Op.getValueType();
2722 MVT MaskVT = Mask.getValueType();
2723 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002724 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002725 SmallVector<SDValue, 8> MaskVec;
Dale Johannesence0805b2009-02-03 19:33:06 +00002726 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002727
2728 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002729 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002730 if (Arg.getOpcode() == ISD::UNDEF) {
Dale Johannesence0805b2009-02-03 19:33:06 +00002731 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002732 continue;
2733 }
2734 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002735 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002736 if (Val < NumElems)
2737 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2738 else
2739 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2740 }
2741
2742 std::swap(V1, V2);
Dale Johannesence0805b2009-02-03 19:33:06 +00002743 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &MaskVec[0], NumElems);
2744 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002745}
2746
Evan Chenga6769df2007-12-07 21:30:01 +00002747/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2748/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002749static
Dale Johannesence0805b2009-02-03 19:33:06 +00002750SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002751 MVT MaskVT = Mask.getValueType();
2752 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002753 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002754 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002755 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002756 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002757 if (Arg.getOpcode() == ISD::UNDEF) {
Dale Johannesence0805b2009-02-03 19:33:06 +00002758 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, EltVT));
Evan Chengfca29242007-12-07 08:07:39 +00002759 continue;
2760 }
2761 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002762 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002763 if (Val < NumElems)
2764 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2765 else
2766 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2767 }
Dale Johannesence0805b2009-02-03 19:33:06 +00002768 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &MaskVec[0], NumElems);
Evan Chengfca29242007-12-07 08:07:39 +00002769}
2770
2771
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002772/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2773/// match movhlps. The lower half elements should come from upper half of
2774/// V1 (and in order), and the upper half elements should come from the upper
2775/// half of V2 (and in order).
2776static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2777 unsigned NumElems = Mask->getNumOperands();
2778 if (NumElems != 4)
2779 return false;
2780 for (unsigned i = 0, e = 2; i != e; ++i)
2781 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2782 return false;
2783 for (unsigned i = 2; i != 4; ++i)
2784 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2785 return false;
2786 return true;
2787}
2788
2789/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002790/// is promoted to a vector. It also returns the LoadSDNode by reference if
2791/// required.
2792static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002793 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2794 return false;
2795 N = N->getOperand(0).getNode();
2796 if (!ISD::isNON_EXTLoad(N))
2797 return false;
2798 if (LD)
2799 *LD = cast<LoadSDNode>(N);
2800 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002801}
2802
2803/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2804/// match movlp{s|d}. The lower half elements should come from lower half of
2805/// V1 (and in order), and the upper half elements should come from the upper
2806/// half of V2 (and in order). And since V1 will become the source of the
2807/// MOVLP, it must be either a vector load or a scalar load to vector.
2808static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2809 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2810 return false;
2811 // Is V2 is a vector load, don't do this transformation. We will try to use
2812 // load folding shufps op.
2813 if (ISD::isNON_EXTLoad(V2))
2814 return false;
2815
2816 unsigned NumElems = Mask->getNumOperands();
2817 if (NumElems != 2 && NumElems != 4)
2818 return false;
2819 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2820 if (!isUndefOrEqual(Mask->getOperand(i), i))
2821 return false;
2822 for (unsigned i = NumElems/2; i != NumElems; ++i)
2823 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2824 return false;
2825 return true;
2826}
2827
2828/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2829/// all the same.
2830static bool isSplatVector(SDNode *N) {
2831 if (N->getOpcode() != ISD::BUILD_VECTOR)
2832 return false;
2833
Dan Gohman8181bd12008-07-27 21:46:04 +00002834 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2836 if (N->getOperand(i) != SplatValue)
2837 return false;
2838 return true;
2839}
2840
2841/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2842/// to an undef.
2843static bool isUndefShuffle(SDNode *N) {
2844 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2845 return false;
2846
Dan Gohman8181bd12008-07-27 21:46:04 +00002847 SDValue V1 = N->getOperand(0);
2848 SDValue V2 = N->getOperand(1);
2849 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002850 unsigned NumElems = Mask.getNumOperands();
2851 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002852 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002853 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002854 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002855 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2856 return false;
2857 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2858 return false;
2859 }
2860 }
2861 return true;
2862}
2863
2864/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2865/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002866static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002867 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002868 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002869 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002870 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002871}
2872
2873/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2874/// to an zero vector.
2875static bool isZeroShuffle(SDNode *N) {
2876 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2877 return false;
2878
Dan Gohman8181bd12008-07-27 21:46:04 +00002879 SDValue V1 = N->getOperand(0);
2880 SDValue V2 = N->getOperand(1);
2881 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002882 unsigned NumElems = Mask.getNumOperands();
2883 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002884 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002885 if (Arg.getOpcode() == ISD::UNDEF)
2886 continue;
2887
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002888 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002889 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002890 unsigned Opc = V1.getNode()->getOpcode();
2891 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002892 continue;
2893 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002894 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002895 return false;
2896 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002897 unsigned Opc = V2.getNode()->getOpcode();
2898 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002899 continue;
2900 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002901 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002902 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002903 }
2904 }
2905 return true;
2906}
2907
2908/// getZeroVector - Returns a vector of specified type with all zero elements.
2909///
Dale Johannesence0805b2009-02-03 19:33:06 +00002910static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2911 DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002912 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002913
2914 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2915 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002916 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002917 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002918 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Dale Johannesence0805b2009-02-03 19:33:06 +00002919 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002920 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002921 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Dale Johannesence0805b2009-02-03 19:33:06 +00002922 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002923 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002924 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Dale Johannesence0805b2009-02-03 19:33:06 +00002925 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002926 }
Dale Johannesence0805b2009-02-03 19:33:06 +00002927 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002928}
2929
Chris Lattnere6aa3862007-11-25 00:24:49 +00002930/// getOnesVector - Returns a vector of specified type with all bits set.
2931///
Dale Johannesence0805b2009-02-03 19:33:06 +00002932static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002933 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002934
2935 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2936 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002937 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2938 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002939 if (VT.getSizeInBits() == 64) // MMX
Dale Johannesence0805b2009-02-03 19:33:06 +00002940 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002941 else // SSE
Dale Johannesence0805b2009-02-03 19:33:06 +00002942 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
2943 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002944}
2945
2946
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002947/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2948/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002949static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002950 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2951
2952 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002953 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002954 unsigned NumElems = Mask.getNumOperands();
2955 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002956 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002957 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002958 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002959 if (Val > NumElems) {
2960 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2961 Changed = true;
2962 }
2963 }
2964 MaskVec.push_back(Arg);
2965 }
2966
2967 if (Changed)
Dale Johannesence0805b2009-02-03 19:33:06 +00002968 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getNode()->getDebugLoc(),
2969 Mask.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002970 &MaskVec[0], MaskVec.size());
2971 return Mask;
2972}
2973
2974/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2975/// operation of specified width.
Dale Johannesence0805b2009-02-03 19:33:06 +00002976static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002977 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2978 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002979
Dan Gohman8181bd12008-07-27 21:46:04 +00002980 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002981 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2982 for (unsigned i = 1; i != NumElems; ++i)
2983 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00002984 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
2985 &MaskVec[0], MaskVec.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002986}
2987
2988/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2989/// of specified width.
Dale Johannesence0805b2009-02-03 19:33:06 +00002990static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG,
2991 DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002992 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2993 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002994 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002995 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2996 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2997 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2998 }
Dale Johannesence0805b2009-02-03 19:33:06 +00002999 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3000 &MaskVec[0], MaskVec.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003001}
3002
3003/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
3004/// of specified width.
Dale Johannesence0805b2009-02-03 19:33:06 +00003005static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG,
3006 DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003007 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3008 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003009 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00003010 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003011 for (unsigned i = 0; i != Half; ++i) {
3012 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
3013 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
3014 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003015 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3016 &MaskVec[0], MaskVec.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003017}
3018
Chris Lattner2d91b962008-03-09 01:05:04 +00003019/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
3020/// element #0 of a vector with the specified index, leaving the rest of the
3021/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00003022static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Dale Johannesence0805b2009-02-03 19:33:06 +00003023 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003024 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3025 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003026 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00003027 // Element #0 of the result gets the elt we are replacing.
3028 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
3029 for (unsigned i = 1; i != NumElems; ++i)
3030 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003031 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3032 &MaskVec[0], MaskVec.size());
Chris Lattner2d91b962008-03-09 01:05:04 +00003033}
3034
Evan Chengbf8b2c52008-04-05 00:30:36 +00003035/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00003036static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003037 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
3038 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003039 if (PVT == VT)
3040 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00003041 SDValue V1 = Op.getOperand(0);
3042 SDValue Mask = Op.getOperand(2);
Mon P Wang532c9632008-12-23 04:03:27 +00003043 unsigned MaskNumElems = Mask.getNumOperands();
3044 unsigned NumElems = MaskNumElems;
Dale Johannesence0805b2009-02-03 19:33:06 +00003045 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003046 // Special handling of v4f32 -> v4i32.
3047 if (VT != MVT::v4f32) {
Mon P Wang532c9632008-12-23 04:03:27 +00003048 // Find which element we want to splat.
3049 SDNode* EltNoNode = getSplatMaskEltNo(Mask.getNode()).getNode();
3050 unsigned EltNo = cast<ConstantSDNode>(EltNoNode)->getZExtValue();
3051 // unpack elements to the correct location
Evan Chengbf8b2c52008-04-05 00:30:36 +00003052 while (NumElems > 4) {
Mon P Wang532c9632008-12-23 04:03:27 +00003053 if (EltNo < NumElems/2) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003054 Mask = getUnpacklMask(MaskNumElems, DAG, dl);
Mon P Wang532c9632008-12-23 04:03:27 +00003055 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00003056 Mask = getUnpackhMask(MaskNumElems, DAG, dl);
Mon P Wang532c9632008-12-23 04:03:27 +00003057 EltNo -= NumElems/2;
3058 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003059 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V1, Mask);
Evan Chengbf8b2c52008-04-05 00:30:36 +00003060 NumElems >>= 1;
3061 }
Mon P Wang532c9632008-12-23 04:03:27 +00003062 SDValue Cst = DAG.getConstant(EltNo, MVT::i32);
Dale Johannesence0805b2009-02-03 19:33:06 +00003063 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003064 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065
Dale Johannesence0805b2009-02-03 19:33:06 +00003066 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
3067 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00003068 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dale Johannesence0805b2009-02-03 19:33:06 +00003069 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuffle);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003070}
3071
Evan Chenga2497eb2008-09-25 20:50:48 +00003072/// isVectorLoad - Returns true if the node is a vector load, a scalar
3073/// load that's promoted to vector, or a load bitcasted.
3074static bool isVectorLoad(SDValue Op) {
3075 assert(Op.getValueType().isVector() && "Expected a vector type");
3076 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
3077 Op.getOpcode() == ISD::BIT_CONVERT) {
3078 return isa<LoadSDNode>(Op.getOperand(0));
3079 }
3080 return isa<LoadSDNode>(Op);
3081}
3082
3083
3084/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3085///
3086static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3087 SelectionDAG &DAG, bool HasSSE3) {
3088 // If we have sse3 and shuffle has more than one use or input is a load, then
3089 // use movddup. Otherwise, use movlhps.
3090 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3091 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3092 MVT VT = Op.getValueType();
3093 if (VT == PVT)
3094 return Op;
Dale Johannesence0805b2009-02-03 19:33:06 +00003095 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Chenga2497eb2008-09-25 20:50:48 +00003096 unsigned NumElems = PVT.getVectorNumElements();
3097 if (NumElems == 2) {
3098 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Dale Johannesence0805b2009-02-03 19:33:06 +00003099 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chenga2497eb2008-09-25 20:50:48 +00003100 } else {
3101 assert(NumElems == 4);
3102 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3103 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
Dale Johannesence0805b2009-02-03 19:33:06 +00003104 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
3105 Cst0, Cst1, Cst0, Cst1);
Evan Chenga2497eb2008-09-25 20:50:48 +00003106 }
3107
Dale Johannesence0805b2009-02-03 19:33:06 +00003108 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
3109 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, PVT, V1,
3110 DAG.getNode(ISD::UNDEF, dl, PVT), Mask);
3111 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuffle);
Evan Chenga2497eb2008-09-25 20:50:48 +00003112}
3113
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003114/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003115/// vector of zero or undef vector. This produces a shuffle where the low
3116/// element of V2 is swizzled into the zero/undef vector, landing at element
3117/// 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 +00003118static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003119 bool isZero, bool HasSSE2,
3120 SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003121 DebugLoc dl = V2.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00003122 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003123 SDValue V1 = isZero
Dale Johannesence0805b2009-02-03 19:33:06 +00003124 ? getZeroVector(VT, HasSSE2, DAG, dl) : DAG.getNode(ISD::UNDEF, dl, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003125 unsigned NumElems = V2.getValueType().getVectorNumElements();
3126 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3127 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003128 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003129 for (unsigned i = 0; i != NumElems; ++i)
3130 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3131 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3132 else
3133 MaskVec.push_back(DAG.getConstant(i, EVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003134 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003135 &MaskVec[0], MaskVec.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00003136 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003137}
3138
Evan Chengdea99362008-05-29 08:22:04 +00003139/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3140/// a shuffle that is zero.
3141static
Dan Gohman8181bd12008-07-27 21:46:04 +00003142unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003143 unsigned NumElems, bool Low,
3144 SelectionDAG &DAG) {
3145 unsigned NumZeros = 0;
3146 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003147 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003148 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003149 if (Idx.getOpcode() == ISD::UNDEF) {
3150 ++NumZeros;
3151 continue;
3152 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003153 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3154 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003155 ++NumZeros;
3156 else
3157 break;
3158 }
3159 return NumZeros;
3160}
3161
3162/// isVectorShift - Returns true if the shuffle can be implemented as a
3163/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003164static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3165 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003166 unsigned NumElems = Mask.getNumOperands();
3167
3168 isLeft = true;
3169 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3170 if (!NumZeros) {
3171 isLeft = false;
3172 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3173 if (!NumZeros)
3174 return false;
3175 }
3176
3177 bool SeenV1 = false;
3178 bool SeenV2 = false;
3179 for (unsigned i = NumZeros; i < NumElems; ++i) {
3180 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003181 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003182 if (Idx.getOpcode() == ISD::UNDEF)
3183 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003184 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003185 if (Index < NumElems)
3186 SeenV1 = true;
3187 else {
3188 Index -= NumElems;
3189 SeenV2 = true;
3190 }
3191 if (Index != Val)
3192 return false;
3193 }
3194 if (SeenV1 && SeenV2)
3195 return false;
3196
3197 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3198 ShAmt = NumZeros;
3199 return true;
3200}
3201
3202
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003203/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3204///
Dan Gohman8181bd12008-07-27 21:46:04 +00003205static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003206 unsigned NumNonZero, unsigned NumZero,
3207 SelectionDAG &DAG, TargetLowering &TLI) {
3208 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003209 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003210
Dale Johannesence0805b2009-02-03 19:33:06 +00003211 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00003212 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003213 bool First = true;
3214 for (unsigned i = 0; i < 16; ++i) {
3215 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3216 if (ThisIsNonZero && First) {
3217 if (NumZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003218 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003219 else
Dale Johannesence0805b2009-02-03 19:33:06 +00003220 V = DAG.getNode(ISD::UNDEF, dl, MVT::v8i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003221 First = false;
3222 }
3223
3224 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003225 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003226 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3227 if (LastIsNonZero) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003228 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
3229 MVT::i16, Op.getOperand(i-1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003230 }
3231 if (ThisIsNonZero) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003232 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3233 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003234 ThisElt, DAG.getConstant(8, MVT::i8));
3235 if (LastIsNonZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003236 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003237 } else
3238 ThisElt = LastElt;
3239
Gabor Greif1c80d112008-08-28 21:40:38 +00003240 if (ThisElt.getNode())
Dale Johannesence0805b2009-02-03 19:33:06 +00003241 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003242 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003243 }
3244 }
3245
Dale Johannesence0805b2009-02-03 19:33:06 +00003246 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003247}
3248
3249/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3250///
Dan Gohman8181bd12008-07-27 21:46:04 +00003251static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003252 unsigned NumNonZero, unsigned NumZero,
3253 SelectionDAG &DAG, TargetLowering &TLI) {
3254 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003255 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003256
Dale Johannesence0805b2009-02-03 19:33:06 +00003257 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00003258 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003259 bool First = true;
3260 for (unsigned i = 0; i < 8; ++i) {
3261 bool isNonZero = (NonZeros & (1 << i)) != 0;
3262 if (isNonZero) {
3263 if (First) {
3264 if (NumZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003265 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003266 else
Dale Johannesence0805b2009-02-03 19:33:06 +00003267 V = DAG.getNode(ISD::UNDEF, dl, MVT::v8i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003268 First = false;
3269 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003270 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
3271 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003272 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003273 }
3274 }
3275
3276 return V;
3277}
3278
Evan Chengdea99362008-05-29 08:22:04 +00003279/// getVShift - Return a vector logical shift node.
3280///
Dan Gohman8181bd12008-07-27 21:46:04 +00003281static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003282 unsigned NumBits, SelectionDAG &DAG,
Dale Johannesence0805b2009-02-03 19:33:06 +00003283 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003284 bool isMMX = VT.getSizeInBits() == 64;
3285 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003286 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesence0805b2009-02-03 19:33:06 +00003287 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3288 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3289 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003290 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003291}
3292
Dan Gohman8181bd12008-07-27 21:46:04 +00003293SDValue
3294X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003295 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattnere6aa3862007-11-25 00:24:49 +00003296 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003297 if (ISD::isBuildVectorAllZeros(Op.getNode())
3298 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003299 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3300 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3301 // eliminated on x86-32 hosts.
3302 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3303 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003304
Gabor Greif1c80d112008-08-28 21:40:38 +00003305 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00003306 return getOnesVector(Op.getValueType(), DAG, dl);
3307 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003308 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003309
Duncan Sands92c43912008-06-06 12:08:01 +00003310 MVT VT = Op.getValueType();
3311 MVT EVT = VT.getVectorElementType();
3312 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003313
3314 unsigned NumElems = Op.getNumOperands();
3315 unsigned NumZero = 0;
3316 unsigned NumNonZero = 0;
3317 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003318 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003319 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003320 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003321 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003322 if (Elt.getOpcode() == ISD::UNDEF)
3323 continue;
3324 Values.insert(Elt);
3325 if (Elt.getOpcode() != ISD::Constant &&
3326 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003327 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003328 if (isZeroNode(Elt))
3329 NumZero++;
3330 else {
3331 NonZeros |= (1 << i);
3332 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003333 }
3334 }
3335
3336 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003337 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesence0805b2009-02-03 19:33:06 +00003338 return DAG.getNode(ISD::UNDEF, dl, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003339 }
3340
Chris Lattner66a4dda2008-03-09 05:42:06 +00003341 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003342 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003343 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003344 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003345
Chris Lattner2d91b962008-03-09 01:05:04 +00003346 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3347 // the value are obviously zero, truncate the value to i32 and do the
3348 // insertion that way. Only do this if the value is non-constant or if the
3349 // value is a constant being inserted into element 0. It is cheaper to do
3350 // a constant pool load than it is to do a movd + shuffle.
3351 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3352 (!IsAllConstants || Idx == 0)) {
3353 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3354 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003355 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3356 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003357
3358 // Truncate the value (which may itself be a constant) to i32, and
3359 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesence0805b2009-02-03 19:33:06 +00003360 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3361 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003362 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3363 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003364
3365 // Now we have our 32-bit value zero extended in the low element of
3366 // a vector. If Idx != 0, swizzle it into place.
3367 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003368 SDValue Ops[] = {
Dale Johannesence0805b2009-02-03 19:33:06 +00003369 Item, DAG.getNode(ISD::UNDEF, dl, Item.getValueType()),
3370 getSwapEltZeroMask(VecElts, Idx, DAG, dl)
Chris Lattner2d91b962008-03-09 01:05:04 +00003371 };
Dale Johannesence0805b2009-02-03 19:33:06 +00003372 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VecVT, Ops, 3);
Chris Lattner2d91b962008-03-09 01:05:04 +00003373 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003374 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner2d91b962008-03-09 01:05:04 +00003375 }
3376 }
3377
Chris Lattnerac914892008-03-08 22:59:52 +00003378 // If we have a constant or non-constant insertion into the low element of
3379 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3380 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3381 // depending on what the source datatype is. Because we can only get here
3382 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3383 if (Idx == 0 &&
3384 // Don't do this for i64 values on x86-32.
3385 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003386 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003387 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003388 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3389 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003390 }
Evan Chengdea99362008-05-29 08:22:04 +00003391
3392 // Is it a vector logical left shift?
3393 if (NumElems == 2 && Idx == 1 &&
3394 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003395 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003396 return getVShift(true, VT,
3397 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
Dale Johannesence0805b2009-02-03 19:33:06 +00003398 NumBits/2, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00003399 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003400
3401 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003402 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003403
Chris Lattnerac914892008-03-08 22:59:52 +00003404 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3405 // is a non-constant being inserted into an element other than the low one,
3406 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3407 // movd/movss) to move this into the low element, then shuffle it into
3408 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003409 if (EVTBits == 32) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003410 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003411
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003412 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003413 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3414 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003415 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3416 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003417 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003418 for (unsigned i = 0; i < NumElems; i++)
3419 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003420 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003421 &MaskVec[0], MaskVec.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00003422 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, Item,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003423 DAG.getNode(ISD::UNDEF, VT), Mask);
3424 }
3425 }
3426
Chris Lattner66a4dda2008-03-09 05:42:06 +00003427 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3428 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003429 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003430
Dan Gohman21463242007-07-24 22:55:08 +00003431 // A vector full of immediates; various special cases are already
3432 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003433 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003434 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003435
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003436 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003437 if (EVTBits == 64) {
3438 if (NumNonZero == 1) {
3439 // One half is zero or undef.
3440 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesence0805b2009-02-03 19:33:06 +00003441 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003442 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003443 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3444 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003445 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003446 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003447 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003448
3449 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3450 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003451 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003452 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003453 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003454 }
3455
3456 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003457 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003458 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003459 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003460 }
3461
3462 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003463 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003464 V.resize(NumElems);
3465 if (NumElems == 4 && NumZero > 0) {
3466 for (unsigned i = 0; i < 4; ++i) {
3467 bool isZero = !(NonZeros & (1 << i));
3468 if (isZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003469 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003470 else
Dale Johannesence0805b2009-02-03 19:33:06 +00003471 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003472 }
3473
3474 for (unsigned i = 0; i < 2; ++i) {
3475 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3476 default: break;
3477 case 0:
3478 V[i] = V[i*2]; // Must be a zero vector.
3479 break;
3480 case 1:
Dale Johannesence0805b2009-02-03 19:33:06 +00003481 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i*2+1], V[i*2],
3482 getMOVLMask(NumElems, DAG, dl));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003483 break;
3484 case 2:
Dale Johannesence0805b2009-02-03 19:33:06 +00003485 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i*2], V[i*2+1],
3486 getMOVLMask(NumElems, DAG, dl));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003487 break;
3488 case 3:
Dale Johannesence0805b2009-02-03 19:33:06 +00003489 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i*2], V[i*2+1],
3490 getUnpacklMask(NumElems, DAG, dl));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003491 break;
3492 }
3493 }
3494
Duncan Sands92c43912008-06-06 12:08:01 +00003495 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3496 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003497 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003498 bool Reverse = (NonZeros & 0x3) == 2;
3499 for (unsigned i = 0; i < 2; ++i)
3500 if (Reverse)
3501 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3502 else
3503 MaskVec.push_back(DAG.getConstant(i, EVT));
3504 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3505 for (unsigned i = 0; i < 2; ++i)
3506 if (Reverse)
3507 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3508 else
3509 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003510 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003511 &MaskVec[0], MaskVec.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00003512 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[0], V[1], ShufMask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003513 }
3514
3515 if (Values.size() > 2) {
3516 // Expand into a number of unpckl*.
3517 // e.g. for v4f32
3518 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3519 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3520 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dale Johannesence0805b2009-02-03 19:33:06 +00003521 SDValue UnpckMask = getUnpacklMask(NumElems, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003522 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesence0805b2009-02-03 19:33:06 +00003523 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003524 NumElems >>= 1;
3525 while (NumElems != 0) {
3526 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesence0805b2009-02-03 19:33:06 +00003527 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i], V[i + NumElems],
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003528 UnpckMask);
3529 NumElems >>= 1;
3530 }
3531 return V[0];
3532 }
3533
Dan Gohman8181bd12008-07-27 21:46:04 +00003534 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003535}
3536
Evan Chengfca29242007-12-07 08:07:39 +00003537static
Dan Gohman8181bd12008-07-27 21:46:04 +00003538SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003539 SDValue PermMask, SelectionDAG &DAG,
Dale Johannesence0805b2009-02-03 19:33:06 +00003540 TargetLowering &TLI, DebugLoc dl) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003541 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003542 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3543 MVT MaskEVT = MaskVT.getVectorElementType();
3544 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003545 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3546 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003547
3548 // First record which half of which vector the low elements come from.
3549 SmallVector<unsigned, 4> LowQuad(4);
3550 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003551 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003552 if (Elt.getOpcode() == ISD::UNDEF)
3553 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003554 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003555 int QuadIdx = EltIdx / 4;
3556 ++LowQuad[QuadIdx];
3557 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003558
Evan Cheng75184a92007-12-11 01:46:18 +00003559 int BestLowQuad = -1;
3560 unsigned MaxQuad = 1;
3561 for (unsigned i = 0; i < 4; ++i) {
3562 if (LowQuad[i] > MaxQuad) {
3563 BestLowQuad = i;
3564 MaxQuad = LowQuad[i];
3565 }
Evan Chengfca29242007-12-07 08:07:39 +00003566 }
3567
Evan Cheng75184a92007-12-11 01:46:18 +00003568 // Record which half of which vector the high elements come from.
3569 SmallVector<unsigned, 4> HighQuad(4);
3570 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003571 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003572 if (Elt.getOpcode() == ISD::UNDEF)
3573 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003574 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003575 int QuadIdx = EltIdx / 4;
3576 ++HighQuad[QuadIdx];
3577 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003578
Evan Cheng75184a92007-12-11 01:46:18 +00003579 int BestHighQuad = -1;
3580 MaxQuad = 1;
3581 for (unsigned i = 0; i < 4; ++i) {
3582 if (HighQuad[i] > MaxQuad) {
3583 BestHighQuad = i;
3584 MaxQuad = HighQuad[i];
3585 }
3586 }
3587
3588 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3589 if (BestLowQuad != -1 || BestHighQuad != -1) {
3590 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003591 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003592
Evan Cheng75184a92007-12-11 01:46:18 +00003593 if (BestLowQuad != -1)
3594 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3595 else
3596 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003597
Evan Cheng75184a92007-12-11 01:46:18 +00003598 if (BestHighQuad != -1)
3599 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3600 else
3601 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003602
Dale Johannesence0805b2009-02-03 19:33:06 +00003603 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, &MaskVec[0],2);
3604 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v2i64,
3605 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3606 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), Mask);
3607 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng75184a92007-12-11 01:46:18 +00003608
3609 // Now sort high and low parts separately.
3610 BitVector InOrder(8);
3611 if (BestLowQuad != -1) {
3612 // Sort lower half in order using PSHUFLW.
3613 MaskVec.clear();
3614 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003615
Evan Cheng75184a92007-12-11 01:46:18 +00003616 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003617 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003618 if (Elt.getOpcode() == ISD::UNDEF) {
3619 MaskVec.push_back(Elt);
3620 InOrder.set(i);
3621 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003622 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003623 if (EltIdx != i)
3624 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003625
Evan Cheng75184a92007-12-11 01:46:18 +00003626 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003627
Evan Cheng75184a92007-12-11 01:46:18 +00003628 // If this element is in the right place after this shuffle, then
3629 // remember it.
3630 if ((int)(EltIdx / 4) == BestLowQuad)
3631 InOrder.set(i);
3632 }
3633 }
3634 if (AnyOutOrder) {
3635 for (unsigned i = 4; i != 8; ++i)
3636 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003637 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3638 &MaskVec[0], 8);
3639 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v8i16,
3640 NewV, NewV, Mask);
Evan Cheng75184a92007-12-11 01:46:18 +00003641 }
3642 }
3643
3644 if (BestHighQuad != -1) {
3645 // Sort high half in order using PSHUFHW if possible.
3646 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003647
Evan Cheng75184a92007-12-11 01:46:18 +00003648 for (unsigned i = 0; i != 4; ++i)
3649 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003650
Evan Cheng75184a92007-12-11 01:46:18 +00003651 bool AnyOutOrder = false;
3652 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003653 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003654 if (Elt.getOpcode() == ISD::UNDEF) {
3655 MaskVec.push_back(Elt);
3656 InOrder.set(i);
3657 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003658 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003659 if (EltIdx != i)
3660 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003661
Evan Cheng75184a92007-12-11 01:46:18 +00003662 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003663
Evan Cheng75184a92007-12-11 01:46:18 +00003664 // If this element is in the right place after this shuffle, then
3665 // remember it.
3666 if ((int)(EltIdx / 4) == BestHighQuad)
3667 InOrder.set(i);
3668 }
3669 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003670
Evan Cheng75184a92007-12-11 01:46:18 +00003671 if (AnyOutOrder) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003672 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl,
3673 MaskVT, &MaskVec[0], 8);
3674 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v8i16,
3675 NewV, NewV, Mask);
Evan Cheng75184a92007-12-11 01:46:18 +00003676 }
3677 }
3678
3679 // The other elements are put in the right place using pextrw and pinsrw.
3680 for (unsigned i = 0; i != 8; ++i) {
3681 if (InOrder[i])
3682 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003683 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003684 if (Elt.getOpcode() == ISD::UNDEF)
3685 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003686 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003687 SDValue ExtOp = (EltIdx < 8)
Dale Johannesence0805b2009-02-03 19:33:06 +00003688 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003689 DAG.getConstant(EltIdx, PtrVT))
Dale Johannesence0805b2009-02-03 19:33:06 +00003690 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003691 DAG.getConstant(EltIdx - 8, PtrVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003692 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Evan Cheng75184a92007-12-11 01:46:18 +00003693 DAG.getConstant(i, PtrVT));
3694 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003695
Evan Cheng75184a92007-12-11 01:46:18 +00003696 return NewV;
3697 }
3698
Bill Wendling2c7cd592008-08-21 22:35:37 +00003699 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3700 // few as possible. First, let's find out how many elements are already in the
3701 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003702 unsigned V1InOrder = 0;
3703 unsigned V1FromV1 = 0;
3704 unsigned V2InOrder = 0;
3705 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003706 SmallVector<SDValue, 8> V1Elts;
3707 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003708 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003709 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003710 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003711 V1Elts.push_back(Elt);
3712 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003713 ++V1InOrder;
3714 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003715 continue;
3716 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003717 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003718 if (EltIdx == i) {
3719 V1Elts.push_back(Elt);
3720 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3721 ++V1InOrder;
3722 } else if (EltIdx == i+8) {
3723 V1Elts.push_back(Elt);
3724 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3725 ++V2InOrder;
3726 } else if (EltIdx < 8) {
3727 V1Elts.push_back(Elt);
Mon P Wang75498182009-01-28 23:11:14 +00003728 V2Elts.push_back(DAG.getConstant(EltIdx+8, MaskEVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003729 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003730 } else {
Mon P Wang532c9632008-12-23 04:03:27 +00003731 V1Elts.push_back(Elt);
Evan Cheng75184a92007-12-11 01:46:18 +00003732 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3733 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003734 }
3735 }
3736
3737 if (V2InOrder > V1InOrder) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003738 PermMask = CommuteVectorShuffleMask(PermMask, DAG, dl);
Evan Chengfca29242007-12-07 08:07:39 +00003739 std::swap(V1, V2);
3740 std::swap(V1Elts, V2Elts);
3741 std::swap(V1FromV1, V2FromV2);
3742 }
3743
Evan Cheng75184a92007-12-11 01:46:18 +00003744 if ((V1FromV1 + V1InOrder) != 8) {
3745 // Some elements are from V2.
3746 if (V1FromV1) {
3747 // If there are elements that are from V1 but out of place,
3748 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003749 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003750 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003751 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003752 if (Elt.getOpcode() == ISD::UNDEF) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003753 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003754 continue;
3755 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003756 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003757 if (EltIdx >= 8)
Dale Johannesence0805b2009-02-03 19:33:06 +00003758 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003759 else
3760 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3761 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003762 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &MaskVec[0], 8);
3763 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003764 }
Evan Cheng75184a92007-12-11 01:46:18 +00003765
3766 NewV = V1;
3767 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003768 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003769 if (Elt.getOpcode() == ISD::UNDEF)
3770 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003771 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003772 if (EltIdx < 8)
3773 continue;
Dale Johannesence0805b2009-02-03 19:33:06 +00003774 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003775 DAG.getConstant(EltIdx - 8, PtrVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003776 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Evan Cheng75184a92007-12-11 01:46:18 +00003777 DAG.getConstant(i, PtrVT));
3778 }
3779 return NewV;
3780 } else {
3781 // All elements are from V1.
3782 NewV = V1;
3783 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003784 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003785 if (Elt.getOpcode() == ISD::UNDEF)
3786 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003787 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dale Johannesence0805b2009-02-03 19:33:06 +00003788 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003789 DAG.getConstant(EltIdx, PtrVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00003790 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Evan Cheng75184a92007-12-11 01:46:18 +00003791 DAG.getConstant(i, PtrVT));
3792 }
3793 return NewV;
3794 }
3795}
3796
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003797/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3798/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3799/// done when every pair / quad of shuffle mask elements point to elements in
3800/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003801/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3802static
Dan Gohman8181bd12008-07-27 21:46:04 +00003803SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003804 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003805 SDValue PermMask, SelectionDAG &DAG,
Dale Johannesence0805b2009-02-03 19:33:06 +00003806 TargetLowering &TLI, DebugLoc dl) {
Evan Cheng75184a92007-12-11 01:46:18 +00003807 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003808 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003809 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003810 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003811 MVT NewVT = MaskVT;
3812 switch (VT.getSimpleVT()) {
3813 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003814 case MVT::v4f32: NewVT = MVT::v2f64; break;
3815 case MVT::v4i32: NewVT = MVT::v2i64; break;
3816 case MVT::v8i16: NewVT = MVT::v4i32; break;
3817 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003818 }
3819
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003820 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003821 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003822 NewVT = MVT::v2i64;
3823 else
3824 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003825 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003826 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003827 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003828 for (unsigned i = 0; i < NumElems; i += Scale) {
3829 unsigned StartIdx = ~0U;
3830 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003831 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003832 if (Elt.getOpcode() == ISD::UNDEF)
3833 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003834 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003835 if (StartIdx == ~0U)
3836 StartIdx = EltIdx - (EltIdx % Scale);
3837 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003838 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003839 }
3840 if (StartIdx == ~0U)
Dale Johannesence0805b2009-02-03 19:33:06 +00003841 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003842 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003843 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003844 }
3845
Dale Johannesence0805b2009-02-03 19:33:06 +00003846 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3847 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
3848 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, NewVT, V1, V2,
3849 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003850 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003851}
3852
Evan Chenge9b9c672008-05-09 21:53:03 +00003853/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003854///
Dan Gohman8181bd12008-07-27 21:46:04 +00003855static SDValue getVZextMovL(MVT VT, MVT OpVT,
3856 SDValue SrcOp, SelectionDAG &DAG,
Dale Johannesence0805b2009-02-03 19:33:06 +00003857 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003858 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3859 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003860 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003861 LD = dyn_cast<LoadSDNode>(SrcOp);
3862 if (!LD) {
3863 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3864 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003865 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003866 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3867 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3868 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3869 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3870 // PR2108
3871 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesence0805b2009-02-03 19:33:06 +00003872 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3873 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3874 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3875 OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003876 SrcOp.getOperand(0)
3877 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003878 }
3879 }
3880 }
3881
Dale Johannesence0805b2009-02-03 19:33:06 +00003882 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3883 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3884 DAG.getNode(ISD::BIT_CONVERT, dl,
3885 OpVT, SrcOp)));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003886}
3887
Evan Chengf50554e2008-07-22 21:13:36 +00003888/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3889/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003890static SDValue
3891LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
Dale Johannesence0805b2009-02-03 19:33:06 +00003892 SDValue PermMask, MVT VT, SelectionDAG &DAG,
3893 DebugLoc dl) {
Evan Chengf50554e2008-07-22 21:13:36 +00003894 MVT MaskVT = PermMask.getValueType();
3895 MVT MaskEVT = MaskVT.getVectorElementType();
3896 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003897 Locs.resize(4);
Dale Johannesence0805b2009-02-03 19:33:06 +00003898 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003899 unsigned NumHi = 0;
3900 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003901 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003902 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003903 if (Elt.getOpcode() == ISD::UNDEF) {
3904 Locs[i] = std::make_pair(-1, -1);
3905 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003906 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003907 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003908 if (Val < 4) {
3909 Locs[i] = std::make_pair(0, NumLo);
3910 Mask1[NumLo] = Elt;
3911 NumLo++;
3912 } else {
3913 Locs[i] = std::make_pair(1, NumHi);
3914 if (2+NumHi < 4)
3915 Mask1[2+NumHi] = Elt;
3916 NumHi++;
3917 }
3918 }
3919 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003920
Evan Chengf50554e2008-07-22 21:13:36 +00003921 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003922 // If no more than two elements come from either vector. This can be
3923 // implemented with two shuffles. First shuffle gather the elements.
3924 // The second shuffle, which takes the first shuffle as both of its
3925 // vector operands, put the elements into the right order.
Dale Johannesence0805b2009-02-03 19:33:06 +00003926 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
3927 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengf50554e2008-07-22 21:13:36 +00003928 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003929
Dale Johannesence0805b2009-02-03 19:33:06 +00003930 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003931 for (unsigned i = 0; i != 4; ++i) {
3932 if (Locs[i].first == -1)
3933 continue;
3934 else {
3935 unsigned Idx = (i < 2) ? 0 : 4;
3936 Idx += Locs[i].first * 2 + Locs[i].second;
3937 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3938 }
3939 }
3940
Dale Johannesence0805b2009-02-03 19:33:06 +00003941 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V1,
3942 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengf50554e2008-07-22 21:13:36 +00003943 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003944 } else if (NumLo == 3 || NumHi == 3) {
3945 // Otherwise, we must have three elements from one vector, call it X, and
3946 // one element from the other, call it Y. First, use a shufps to build an
3947 // intermediate vector with the one element from Y and the element from X
3948 // that will be in the same half in the final destination (the indexes don't
3949 // matter). Then, use a shufps to build the final vector, taking the half
3950 // containing the element from Y from the intermediate, and the other half
3951 // from X.
3952 if (NumHi == 3) {
3953 // Normalize it so the 3 elements come from V1.
Dale Johannesence0805b2009-02-03 19:33:06 +00003954 PermMask = CommuteVectorShuffleMask(PermMask, DAG, dl);
Evan Cheng3cae0332008-07-23 00:22:17 +00003955 std::swap(V1, V2);
3956 }
3957
3958 // Find the element from V2.
3959 unsigned HiIndex;
3960 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003961 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003962 if (Elt.getOpcode() == ISD::UNDEF)
3963 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003964 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003965 if (Val >= 4)
3966 break;
3967 }
3968
3969 Mask1[0] = PermMask.getOperand(HiIndex);
Dale Johannesence0805b2009-02-03 19:33:06 +00003970 Mask1[1] = DAG.getNode(ISD::UNDEF, dl, MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003971 Mask1[2] = PermMask.getOperand(HiIndex^1);
Dale Johannesence0805b2009-02-03 19:33:06 +00003972 Mask1[3] = DAG.getNode(ISD::UNDEF, dl, MaskEVT);
3973 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
Evan Cheng3cae0332008-07-23 00:22:17 +00003974 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3975
3976 if (HiIndex >= 2) {
3977 Mask1[0] = PermMask.getOperand(0);
3978 Mask1[1] = PermMask.getOperand(1);
3979 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3980 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
Dale Johannesence0805b2009-02-03 19:33:06 +00003981 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
3982 DAG.getNode(ISD::BUILD_VECTOR, dl,
3983 MaskVT, &Mask1[0], 4));
Evan Cheng3cae0332008-07-23 00:22:17 +00003984 } else {
3985 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3986 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3987 Mask1[2] = PermMask.getOperand(2);
3988 Mask1[3] = PermMask.getOperand(3);
3989 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003990 Mask1[2] =
3991 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3992 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003993 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003994 Mask1[3] =
3995 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3996 MaskEVT);
Dale Johannesence0805b2009-02-03 19:33:06 +00003997 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V2, V1,
3998 DAG.getNode(ISD::BUILD_VECTOR, dl,
3999 MaskVT, &Mask1[0], 4));
Evan Cheng3cae0332008-07-23 00:22:17 +00004000 }
Evan Chengf50554e2008-07-22 21:13:36 +00004001 }
4002
4003 // Break it into (shuffle shuffle_hi, shuffle_lo).
4004 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00004005 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
4006 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
4007 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00004008 unsigned MaskIdx = 0;
4009 unsigned LoIdx = 0;
4010 unsigned HiIdx = 2;
4011 for (unsigned i = 0; i != 4; ++i) {
4012 if (i == 2) {
4013 MaskPtr = &HiMask;
4014 MaskIdx = 1;
4015 LoIdx = 0;
4016 HiIdx = 2;
4017 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004018 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00004019 if (Elt.getOpcode() == ISD::UNDEF) {
4020 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004021 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00004022 Locs[i] = std::make_pair(MaskIdx, LoIdx);
4023 (*MaskPtr)[LoIdx] = Elt;
4024 LoIdx++;
4025 } else {
4026 Locs[i] = std::make_pair(MaskIdx, HiIdx);
4027 (*MaskPtr)[HiIdx] = Elt;
4028 HiIdx++;
4029 }
4030 }
4031
Dale Johannesence0805b2009-02-03 19:33:06 +00004032 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
4033 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengf50554e2008-07-22 21:13:36 +00004034 &LoMask[0], LoMask.size()));
Dale Johannesence0805b2009-02-03 19:33:06 +00004035 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
4036 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengf50554e2008-07-22 21:13:36 +00004037 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004038 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00004039 for (unsigned i = 0; i != 4; ++i) {
4040 if (Locs[i].first == -1) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004041 MaskOps.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00004042 } else {
4043 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
4044 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
4045 }
4046 }
Dale Johannesence0805b2009-02-03 19:33:06 +00004047 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, LoShuffle, HiShuffle,
4048 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengf50554e2008-07-22 21:13:36 +00004049 &MaskOps[0], MaskOps.size()));
4050}
4051
Dan Gohman8181bd12008-07-27 21:46:04 +00004052SDValue
4053X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
4054 SDValue V1 = Op.getOperand(0);
4055 SDValue V2 = Op.getOperand(1);
4056 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00004057 MVT VT = Op.getValueType();
Dale Johannesence0805b2009-02-03 19:33:06 +00004058 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004059 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00004060 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004061 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4062 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
4063 bool V1IsSplat = false;
4064 bool V2IsSplat = false;
4065
Gabor Greif1c80d112008-08-28 21:40:38 +00004066 if (isUndefShuffle(Op.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00004067 return DAG.getNode(ISD::UNDEF, dl, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004068
Gabor Greif1c80d112008-08-28 21:40:38 +00004069 if (isZeroShuffle(Op.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00004070 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004071
Gabor Greif1c80d112008-08-28 21:40:38 +00004072 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004073 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00004074 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004075 return V2;
4076
Evan Chengae6c9212008-09-25 23:35:16 +00004077 // Canonicalize movddup shuffles.
4078 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Chengbdd9d9f2008-10-06 21:13:08 +00004079 VT.getSizeInBits() == 128 &&
Evan Chengae6c9212008-09-25 23:35:16 +00004080 X86::isMOVDDUPMask(PermMask.getNode()))
4081 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
4082
Gabor Greif1c80d112008-08-28 21:40:38 +00004083 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004084 if (isMMX || NumElems < 4) return Op;
4085 // Promote it to a v4{if}32 splat.
4086 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004087 }
4088
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004089 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4090 // do it!
4091 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004092 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG,
4093 *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004094 if (NewOp.getNode())
Dale Johannesence0805b2009-02-03 19:33:06 +00004095 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4096 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004097 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4098 // FIXME: Figure out a cleaner way to do this.
4099 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004100 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004101 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Dale Johannesence0805b2009-02-03 19:33:06 +00004102 DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004103 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004104 SDValue NewV1 = NewOp.getOperand(0);
4105 SDValue NewV2 = NewOp.getOperand(1);
4106 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00004107 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004108 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Dale Johannesence0805b2009-02-03 19:33:06 +00004109 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget,
4110 dl);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004111 }
4112 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004113 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004114 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Dale Johannesence0805b2009-02-03 19:33:06 +00004115 DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004116 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004117 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Dale Johannesence0805b2009-02-03 19:33:06 +00004118 DAG, Subtarget, dl);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004119 }
4120 }
4121
Evan Chengdea99362008-05-29 08:22:04 +00004122 // Check if this can be converted into a logical shift.
4123 bool isLeft = false;
4124 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004125 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00004126 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4127 if (isShift && ShVal.hasOneUse()) {
4128 // If the shifted value has multiple uses, it may be cheaper to use
4129 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004130 MVT EVT = VT.getVectorElementType();
4131 ShAmt *= EVT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004132 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00004133 }
4134
Gabor Greif1c80d112008-08-28 21:40:38 +00004135 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004136 if (V1IsUndef)
4137 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004138 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00004139 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004140 if (!isMMX)
4141 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004142 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004143
Gabor Greif1c80d112008-08-28 21:40:38 +00004144 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4145 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4146 X86::isMOVHLPSMask(PermMask.getNode()) ||
4147 X86::isMOVHPMask(PermMask.getNode()) ||
4148 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004149 return Op;
4150
Gabor Greif1c80d112008-08-28 21:40:38 +00004151 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4152 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004153 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4154
Evan Chengdea99362008-05-29 08:22:04 +00004155 if (isShift) {
4156 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004157 MVT EVT = VT.getVectorElementType();
4158 ShAmt *= EVT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004159 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00004160 }
4161
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004162 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004163 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4164 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004165 V1IsSplat = isSplatVector(V1.getNode());
4166 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004167
4168 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004169 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4170 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4171 std::swap(V1IsSplat, V2IsSplat);
4172 std::swap(V1IsUndef, V2IsUndef);
4173 Commuted = true;
4174 }
4175
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004176 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004177 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004178 if (V2IsUndef) return V1;
4179 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4180 if (V2IsSplat) {
4181 // V2 is a splat, so the mask may be malformed. That is, it may point
4182 // to any V2 element. The instruction selectior won't like this. Get
4183 // a corrected mask and commute to form a proper MOVS{S|D}.
Dale Johannesence0805b2009-02-03 19:33:06 +00004184 SDValue NewMask = getMOVLMask(NumElems, DAG, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004185 if (NewMask.getNode() != PermMask.getNode())
Dale Johannesence0805b2009-02-03 19:33:06 +00004186 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, NewMask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004187 }
4188 return Op;
4189 }
4190
Gabor Greif1c80d112008-08-28 21:40:38 +00004191 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4192 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4193 X86::isUNPCKLMask(PermMask.getNode()) ||
4194 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004195 return Op;
4196
4197 if (V2IsSplat) {
4198 // Normalize mask so all entries that point to V2 points to its first
4199 // element then try to match unpck{h|l} again. If match, return a
4200 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004201 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004202 if (NewMask.getNode() != PermMask.getNode()) {
4203 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004204 SDValue NewMask = getUnpacklMask(NumElems, DAG, dl);
4205 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004206 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004207 SDValue NewMask = getUnpackhMask(NumElems, DAG, dl);
4208 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, NewMask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004209 }
4210 }
4211 }
4212
4213 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004214 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004215 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4216
4217 if (Commuted) {
4218 // Commute is back and try unpck* again.
4219 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004220 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4221 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4222 X86::isUNPCKLMask(PermMask.getNode()) ||
4223 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004224 return Op;
4225 }
4226
Evan Chengbf8b2c52008-04-05 00:30:36 +00004227 // Try PSHUF* first, then SHUFP*.
4228 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4229 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004230 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004231 if (V2.getOpcode() != ISD::UNDEF)
Dale Johannesence0805b2009-02-03 19:33:06 +00004232 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00004233 DAG.getNode(ISD::UNDEF, VT), PermMask);
4234 return Op;
4235 }
4236
4237 if (!isMMX) {
4238 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004239 (X86::isPSHUFDMask(PermMask.getNode()) ||
4240 X86::isPSHUFHWMask(PermMask.getNode()) ||
4241 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004242 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004243 if (VT == MVT::v4f32) {
4244 RVT = MVT::v4i32;
Dale Johannesence0805b2009-02-03 19:33:06 +00004245 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, RVT,
4246 DAG.getNode(ISD::BIT_CONVERT, dl, RVT, V1),
4247 DAG.getNode(ISD::UNDEF, dl, RVT), PermMask);
Evan Chengbf8b2c52008-04-05 00:30:36 +00004248 } else if (V2.getOpcode() != ISD::UNDEF)
Dale Johannesence0805b2009-02-03 19:33:06 +00004249 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, RVT, V1,
4250 DAG.getNode(ISD::UNDEF, dl, RVT), PermMask);
Evan Chengbf8b2c52008-04-05 00:30:36 +00004251 if (RVT != VT)
Dale Johannesence0805b2009-02-03 19:33:06 +00004252 Op = DAG.getNode(ISD::BIT_CONVERT, dl, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004253 return Op;
4254 }
4255
Evan Chengbf8b2c52008-04-05 00:30:36 +00004256 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004257 if (X86::isSHUFPMask(PermMask.getNode()) ||
4258 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004259 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004260 }
4261
Evan Cheng75184a92007-12-11 01:46:18 +00004262 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4263 if (VT == MVT::v8i16) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004264 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004265 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004266 return NewOp;
4267 }
4268
Evan Chengf50554e2008-07-22 21:13:36 +00004269 // Handle all 4 wide cases with a number of shuffles except for MMX.
4270 if (NumElems == 4 && !isMMX)
Dale Johannesence0805b2009-02-03 19:33:06 +00004271 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004272
Dan Gohman8181bd12008-07-27 21:46:04 +00004273 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004274}
4275
Dan Gohman8181bd12008-07-27 21:46:04 +00004276SDValue
4277X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004278 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004279 MVT VT = Op.getValueType();
Dale Johannesence0805b2009-02-03 19:33:06 +00004280 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00004281 if (VT.getSizeInBits() == 8) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004282 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004283 Op.getOperand(0), Op.getOperand(1));
Dale Johannesence0805b2009-02-03 19:33:06 +00004284 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004285 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004286 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004287 } else if (VT.getSizeInBits() == 16) {
Evan Chengf9393b32009-01-02 05:29:08 +00004288 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4289 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4290 if (Idx == 0)
Dale Johannesence0805b2009-02-03 19:33:06 +00004291 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4292 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4293 DAG.getNode(ISD::BIT_CONVERT, dl,
4294 MVT::v4i32,
Evan Chengf9393b32009-01-02 05:29:08 +00004295 Op.getOperand(0)),
4296 Op.getOperand(1)));
Dale Johannesence0805b2009-02-03 19:33:06 +00004297 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004298 Op.getOperand(0), Op.getOperand(1));
Dale Johannesence0805b2009-02-03 19:33:06 +00004299 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004300 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004301 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004302 } else if (VT == MVT::f32) {
4303 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4304 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004305 // result has a single use which is a store or a bitcast to i32. And in
4306 // the case of a store, it's not worth it if the index is a constant 0,
4307 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004308 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004309 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004310 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004311 if ((User->getOpcode() != ISD::STORE ||
4312 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4313 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004314 (User->getOpcode() != ISD::BIT_CONVERT ||
4315 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004316 return SDValue();
Dale Johannesence0805b2009-02-03 19:33:06 +00004317 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4318 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
4319 Op.getOperand(0)),
4320 Op.getOperand(1));
4321 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004322 } else if (VT == MVT::i32) {
4323 // ExtractPS works with constant index.
4324 if (isa<ConstantSDNode>(Op.getOperand(1)))
4325 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004326 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004327 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004328}
4329
4330
Dan Gohman8181bd12008-07-27 21:46:04 +00004331SDValue
4332X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004333 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004334 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004335
Evan Cheng6c249332008-03-24 21:52:23 +00004336 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004337 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004338 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004339 return Res;
4340 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004341
Duncan Sands92c43912008-06-06 12:08:01 +00004342 MVT VT = Op.getValueType();
Dale Johannesence0805b2009-02-03 19:33:06 +00004343 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004344 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004345 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004346 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004347 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004348 if (Idx == 0)
Dale Johannesence0805b2009-02-03 19:33:06 +00004349 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4350 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4351 DAG.getNode(ISD::BIT_CONVERT, dl,
4352 MVT::v4i32, Vec),
Evan Cheng75184a92007-12-11 01:46:18 +00004353 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004354 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004355 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesence0805b2009-02-03 19:33:06 +00004356 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004357 Op.getOperand(0), Op.getOperand(1));
Dale Johannesence0805b2009-02-03 19:33:06 +00004358 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004359 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004360 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004361 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004362 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004363 if (Idx == 0)
4364 return Op;
4365 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004366 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004367 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004368 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004369 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004370 IdxVec.
Dale Johannesence0805b2009-02-03 19:33:06 +00004371 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004372 IdxVec.
Dale Johannesence0805b2009-02-03 19:33:06 +00004373 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004374 IdxVec.
Dale Johannesence0805b2009-02-03 19:33:06 +00004375 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
4376 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004377 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004378 SDValue Vec = Op.getOperand(0);
Dale Johannesence0805b2009-02-03 19:33:06 +00004379 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, Vec.getValueType(),
4380 Vec, DAG.getNode(ISD::UNDEF, dl, Vec.getValueType()),
4381 Mask);
4382 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004383 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004384 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004385 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4386 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4387 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004388 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004389 if (Idx == 0)
4390 return Op;
4391
4392 // UNPCKHPD the element to the lowest double word, then movsd.
4393 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4394 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004395 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004396 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004397 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004398 IdxVec.
Dale Johannesence0805b2009-02-03 19:33:06 +00004399 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
4400 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004401 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004402 SDValue Vec = Op.getOperand(0);
Dale Johannesence0805b2009-02-03 19:33:06 +00004403 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, Vec.getValueType(),
4404 Vec, DAG.getNode(ISD::UNDEF, dl, Vec.getValueType()),
4405 Mask);
4406 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004407 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408 }
4409
Dan Gohman8181bd12008-07-27 21:46:04 +00004410 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004411}
4412
Dan Gohman8181bd12008-07-27 21:46:04 +00004413SDValue
4414X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004415 MVT VT = Op.getValueType();
4416 MVT EVT = VT.getVectorElementType();
Dale Johannesence0805b2009-02-03 19:33:06 +00004417 DebugLoc dl = Op.getNode()->getDebugLoc();
Nate Begemand77e59e2008-02-11 04:19:36 +00004418
Dan Gohman8181bd12008-07-27 21:46:04 +00004419 SDValue N0 = Op.getOperand(0);
4420 SDValue N1 = Op.getOperand(1);
4421 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004422
Dan Gohman5a7af042008-08-14 22:53:18 +00004423 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4424 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004425 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004426 : X86ISD::PINSRW;
4427 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4428 // argument.
4429 if (N1.getValueType() != MVT::i32)
Dale Johannesence0805b2009-02-03 19:33:06 +00004430 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begemand77e59e2008-02-11 04:19:36 +00004431 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004432 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesence0805b2009-02-03 19:33:06 +00004433 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004434 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004435 // Bits [7:6] of the constant are the source select. This will always be
4436 // zero here. The DAG Combiner may combine an extract_elt index into these
4437 // bits. For example (insert (extract, 3), 2) could be matched by putting
4438 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4439 // Bits [5:4] of the constant are the destination select. This is the
4440 // value of the incoming immediate.
4441 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4442 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004443 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00004444 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004445 } else if (EVT == MVT::i32) {
4446 // InsertPS works with constant index.
4447 if (isa<ConstantSDNode>(N2))
4448 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004449 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004450 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004451}
4452
Dan Gohman8181bd12008-07-27 21:46:04 +00004453SDValue
4454X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004455 MVT VT = Op.getValueType();
4456 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004457
4458 if (Subtarget->hasSSE41())
4459 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4460
Evan Chenge12a7eb2007-12-12 07:55:34 +00004461 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004462 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004463
Dale Johannesence0805b2009-02-03 19:33:06 +00004464 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00004465 SDValue N0 = Op.getOperand(0);
4466 SDValue N1 = Op.getOperand(1);
4467 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004468
Duncan Sands92c43912008-06-06 12:08:01 +00004469 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004470 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4471 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004472 if (N1.getValueType() != MVT::i32)
Dale Johannesence0805b2009-02-03 19:33:06 +00004473 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004474 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004475 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesence0805b2009-02-03 19:33:06 +00004476 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004477 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004478 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004479}
4480
Dan Gohman8181bd12008-07-27 21:46:04 +00004481SDValue
4482X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004483 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng759fe022008-07-22 18:39:19 +00004484 if (Op.getValueType() == MVT::v2f32)
Dale Johannesence0805b2009-02-03 19:33:06 +00004485 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4486 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4487 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng759fe022008-07-22 18:39:19 +00004488 Op.getOperand(0))));
4489
Dale Johannesence0805b2009-02-03 19:33:06 +00004490 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004491 MVT VT = MVT::v2i32;
4492 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004493 default: break;
4494 case MVT::v16i8:
4495 case MVT::v8i16:
4496 VT = MVT::v4i32;
4497 break;
4498 }
Dale Johannesence0805b2009-02-03 19:33:06 +00004499 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4500 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004501}
4502
Bill Wendlingfef06052008-09-16 21:48:12 +00004503// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4504// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4505// one of the above mentioned nodes. It has to be wrapped because otherwise
4506// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4507// be used to form addressing mode. These wrapped nodes will be selected
4508// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004509SDValue
4510X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004511 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004512 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004513 getPointerTy(),
4514 CP->getAlignment());
4515 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4516 // With PIC, the address is actually $g + Offset.
4517 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4518 !Subtarget->isPICStyleRIPRel()) {
4519 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4520 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4521 Result);
4522 }
4523
4524 return Result;
4525}
4526
Dan Gohman8181bd12008-07-27 21:46:04 +00004527SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004528X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
Dan Gohman36322c72008-10-18 02:06:02 +00004529 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00004530 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00004531 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4532 bool ExtraLoadRequired =
4533 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4534
4535 // Create the TargetGlobalAddress node, folding in the constant
4536 // offset if it is legal.
4537 SDValue Result;
Dan Gohman3d5257c2008-10-21 03:38:42 +00004538 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +00004539 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4540 Offset = 0;
4541 } else
4542 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004543 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00004544
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004545 // With PIC, the address is actually $g + Offset.
Dan Gohman36322c72008-10-18 02:06:02 +00004546 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004547 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4548 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4549 Result);
4550 }
4551
4552 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4553 // load the value at address GV, not the value of GV itself. This means that
4554 // the GlobalAddress must be in the base or index register of the address, not
4555 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4556 // The same applies for external symbols during PIC codegen
Dan Gohman36322c72008-10-18 02:06:02 +00004557 if (ExtraLoadRequired)
Dan Gohman12a9c082008-02-06 22:27:42 +00004558 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004559 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004560
Dan Gohman36322c72008-10-18 02:06:02 +00004561 // If there was a non-zero offset that we didn't fold, create an explicit
4562 // addition for it.
4563 if (Offset != 0)
4564 Result = DAG.getNode(ISD::ADD, getPointerTy(), Result,
4565 DAG.getConstant(Offset, getPointerTy()));
4566
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004567 return Result;
4568}
4569
Evan Cheng7f250d62008-09-24 00:05:32 +00004570SDValue
4571X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4572 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00004573 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
4574 return LowerGlobalAddress(GV, Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00004575}
4576
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004577// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004578static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004579LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004580 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004581 SDValue InFlag;
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00004582 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4583 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004584 DAG.getNode(X86ISD::GlobalBaseReg,
4585 PtrVT), InFlag);
4586 InFlag = Chain.getValue(1);
4587
4588 // emit leal symbol@TLSGD(,%ebx,1), %eax
4589 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004590 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004591 GA->getValueType(0),
4592 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004593 SDValue Ops[] = { Chain, TGA, InFlag };
4594 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004595 InFlag = Result.getValue(2);
4596 Chain = Result.getValue(1);
4597
4598 // call ___tls_get_addr. This function receives its argument in
4599 // the register EAX.
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00004600 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Result, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004601 InFlag = Chain.getValue(1);
4602
4603 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004604 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004605 DAG.getTargetExternalSymbol("___tls_get_addr",
4606 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004607 DAG.getRegister(X86::EAX, PtrVT),
4608 DAG.getRegister(X86::EBX, PtrVT),
4609 InFlag };
4610 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4611 InFlag = Chain.getValue(1);
4612
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00004613 return DAG.getCopyFromReg(Chain, dl, X86::EAX, PtrVT, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004614}
4615
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004616// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004617static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004618LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004619 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004620 SDValue InFlag, Chain;
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00004621 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004622
4623 // emit leaq symbol@TLSGD(%rip), %rdi
4624 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004625 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004626 GA->getValueType(0),
4627 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004628 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4629 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004630 Chain = Result.getValue(1);
4631 InFlag = Result.getValue(2);
4632
aslb204cd52008-08-16 12:58:29 +00004633 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004634 // the register RDI.
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00004635 Chain = DAG.getCopyToReg(Chain, dl, X86::RDI, Result, InFlag);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004636 InFlag = Chain.getValue(1);
4637
4638 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004639 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004640 DAG.getTargetExternalSymbol("__tls_get_addr",
4641 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004642 DAG.getRegister(X86::RDI, PtrVT),
4643 InFlag };
4644 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4645 InFlag = Chain.getValue(1);
4646
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00004647 return DAG.getCopyFromReg(Chain, dl, X86::RAX, PtrVT, InFlag);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004648}
4649
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004650// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4651// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004652static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004653 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004654 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004655 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004656 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4657 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004658 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004659 GA->getValueType(0),
4660 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004661 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004662
4663 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004664 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004665 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004666
4667 // The address of the thread local variable is the add of the thread
4668 // pointer with the offset of the variable.
4669 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4670}
4671
Dan Gohman8181bd12008-07-27 21:46:04 +00004672SDValue
4673X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004674 // TODO: implement the "local dynamic" model
4675 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004676 assert(Subtarget->isTargetELF() &&
4677 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004678 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4679 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4680 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004681 if (Subtarget->is64Bit()) {
4682 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4683 } else {
4684 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4685 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4686 else
4687 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4688 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004689}
4690
Dan Gohman8181bd12008-07-27 21:46:04 +00004691SDValue
4692X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004693 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4694 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004695 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4696 // With PIC, the address is actually $g + Offset.
4697 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4698 !Subtarget->isPICStyleRIPRel()) {
4699 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4700 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4701 Result);
4702 }
4703
4704 return Result;
4705}
4706
Dan Gohman8181bd12008-07-27 21:46:04 +00004707SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004708 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004709 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004710 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4711 // With PIC, the address is actually $g + Offset.
4712 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4713 !Subtarget->isPICStyleRIPRel()) {
4714 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4715 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4716 Result);
4717 }
4718
4719 return Result;
4720}
4721
Chris Lattner62814a32007-10-17 06:02:13 +00004722/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4723/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004724SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004725 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004726 MVT VT = Op.getValueType();
4727 unsigned VTBits = VT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004728 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattner62814a32007-10-17 06:02:13 +00004729 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004730 SDValue ShOpLo = Op.getOperand(0);
4731 SDValue ShOpHi = Op.getOperand(1);
4732 SDValue ShAmt = Op.getOperand(2);
4733 SDValue Tmp1 = isSRA ?
Dale Johannesence0805b2009-02-03 19:33:06 +00004734 DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
4735 DAG.getConstant(VTBits - 1, MVT::i8)) :
Dan Gohman092014e2008-03-03 22:22:09 +00004736 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004737
Dan Gohman8181bd12008-07-27 21:46:04 +00004738 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004739 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004740 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4741 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004742 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00004743 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4744 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004745 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004746
Dale Johannesence0805b2009-02-03 19:33:06 +00004747 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004748 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesence0805b2009-02-03 19:33:06 +00004749 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004750 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004751
Dan Gohman8181bd12008-07-27 21:46:04 +00004752 SDValue Hi, Lo;
4753 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4754 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4755 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004756
Chris Lattner62814a32007-10-17 06:02:13 +00004757 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004758 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4759 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004760 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00004761 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4762 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004763 }
4764
Dan Gohman8181bd12008-07-27 21:46:04 +00004765 SDValue Ops[2] = { Lo, Hi };
Dale Johannesence0805b2009-02-03 19:33:06 +00004766 return DAG.getMergeValues(Ops, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004767}
4768
Dan Gohman8181bd12008-07-27 21:46:04 +00004769SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004770 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004771 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004772 "Unknown SINT_TO_FP to lower!");
4773
4774 // These are really Legal; caller falls through into that case.
4775 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004776 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004777 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4778 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004779 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004780
Dale Johannesence0805b2009-02-03 19:33:06 +00004781 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00004782 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004783 MachineFunction &MF = DAG.getMachineFunction();
4784 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004785 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00004786 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004787 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004788 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004789
4790 // Build the FILD
4791 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004792 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004793 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004794 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4795 else
4796 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004797 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004798 Ops.push_back(Chain);
4799 Ops.push_back(StackSlot);
4800 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004801 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004802 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004803
Dale Johannesen2fc20782007-09-14 22:26:36 +00004804 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004805 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004806 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004807
4808 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4809 // shouldn't be necessary except that RFP cannot be live across
4810 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4811 MachineFunction &MF = DAG.getMachineFunction();
4812 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004813 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004814 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004815 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004816 Ops.push_back(Chain);
4817 Ops.push_back(Result);
4818 Ops.push_back(StackSlot);
4819 Ops.push_back(DAG.getValueType(Op.getValueType()));
4820 Ops.push_back(InFlag);
Dale Johannesence0805b2009-02-03 19:33:06 +00004821 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4822 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004823 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004824 }
4825
4826 return Result;
4827}
4828
Bill Wendling14a30ef2009-01-17 03:56:04 +00004829// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4830SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4831 // This algorithm is not obvious. Here it is in C code, more or less:
4832 /*
4833 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4834 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4835 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesenfb019af2008-10-21 23:07:49 +00004836
Bill Wendling14a30ef2009-01-17 03:56:04 +00004837 // Copy ints to xmm registers.
4838 __m128i xh = _mm_cvtsi32_si128( hi );
4839 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004840
Bill Wendling14a30ef2009-01-17 03:56:04 +00004841 // Combine into low half of a single xmm register.
4842 __m128i x = _mm_unpacklo_epi32( xh, xl );
4843 __m128d d;
4844 double sd;
Dale Johannesenfb019af2008-10-21 23:07:49 +00004845
Bill Wendling14a30ef2009-01-17 03:56:04 +00004846 // Merge in appropriate exponents to give the integer bits the right
4847 // magnitude.
4848 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004849
Bill Wendling14a30ef2009-01-17 03:56:04 +00004850 // Subtract away the biases to deal with the IEEE-754 double precision
4851 // implicit 1.
4852 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004853
Bill Wendling14a30ef2009-01-17 03:56:04 +00004854 // All conversions up to here are exact. The correctly rounded result is
4855 // calculated using the current rounding mode using the following
4856 // horizontal add.
4857 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4858 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4859 // store doesn't really need to be here (except
4860 // maybe to zero the other double)
4861 return sd;
4862 }
4863 */
Dale Johannesenfb019af2008-10-21 23:07:49 +00004864
Dale Johannesence0805b2009-02-03 19:33:06 +00004865 DebugLoc dl = Op.getNode()->getDebugLoc();
4866
Dale Johannesena359b8b2008-10-21 20:50:01 +00004867 // Build some magic constants.
Bill Wendling14a30ef2009-01-17 03:56:04 +00004868 std::vector<Constant*> CV0;
Dale Johannesena359b8b2008-10-21 20:50:01 +00004869 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4870 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4871 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4872 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4873 Constant *C0 = ConstantVector::get(CV0);
4874 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 4);
4875
Bill Wendling14a30ef2009-01-17 03:56:04 +00004876 std::vector<Constant*> CV1;
Dale Johannesena359b8b2008-10-21 20:50:01 +00004877 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4878 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4879 Constant *C1 = ConstantVector::get(CV1);
4880 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 4);
4881
4882 SmallVector<SDValue, 4> MaskVec;
4883 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
4884 MaskVec.push_back(DAG.getConstant(4, MVT::i32));
4885 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
4886 MaskVec.push_back(DAG.getConstant(5, MVT::i32));
Dale Johannesence0805b2009-02-03 19:33:06 +00004887 SDValue UnpcklMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4888 &MaskVec[0], MaskVec.size());
Dale Johannesena359b8b2008-10-21 20:50:01 +00004889 SmallVector<SDValue, 4> MaskVec2;
Duncan Sandsca872ca2008-10-22 11:24:12 +00004890 MaskVec2.push_back(DAG.getConstant(1, MVT::i32));
4891 MaskVec2.push_back(DAG.getConstant(0, MVT::i32));
Dale Johannesence0805b2009-02-03 19:33:06 +00004892 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32,
4893 &MaskVec2[0], MaskVec2.size());
Dale Johannesena359b8b2008-10-21 20:50:01 +00004894
Dale Johannesence0805b2009-02-03 19:33:06 +00004895 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4896 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004897 Op.getOperand(0),
4898 DAG.getIntPtrConstant(1)));
Dale Johannesence0805b2009-02-03 19:33:06 +00004899 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4900 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004901 Op.getOperand(0),
4902 DAG.getIntPtrConstant(0)));
Dale Johannesence0805b2009-02-03 19:33:06 +00004903 SDValue Unpck1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v4i32,
Dale Johannesena359b8b2008-10-21 20:50:01 +00004904 XR1, XR2, UnpcklMask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004905 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004906 PseudoSourceValue::getConstantPool(), 0,
4907 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00004908 SDValue Unpck2 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v4i32,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004909 Unpck1, CLod0, UnpcklMask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004910 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4911 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004912 PseudoSourceValue::getConstantPool(), 0,
4913 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00004914 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling14a30ef2009-01-17 03:56:04 +00004915
Dale Johannesena359b8b2008-10-21 20:50:01 +00004916 // Add the halves; easiest way is to swap them into another reg first.
Dale Johannesence0805b2009-02-03 19:33:06 +00004917 SDValue Shuf = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v2f64,
Dale Johannesena359b8b2008-10-21 20:50:01 +00004918 Sub, Sub, ShufMask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004919 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4920 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesena359b8b2008-10-21 20:50:01 +00004921 DAG.getIntPtrConstant(0));
4922}
4923
Bill Wendling14a30ef2009-01-17 03:56:04 +00004924// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4925SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004926 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling14a30ef2009-01-17 03:56:04 +00004927 // FP constant to bias correct the final result.
4928 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4929 MVT::f64);
4930
4931 // Load the 32-bit value into an XMM register.
Dale Johannesence0805b2009-02-03 19:33:06 +00004932 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4933 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004934 Op.getOperand(0),
4935 DAG.getIntPtrConstant(0)));
4936
Dale Johannesence0805b2009-02-03 19:33:06 +00004937 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4938 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling14a30ef2009-01-17 03:56:04 +00004939 DAG.getIntPtrConstant(0));
4940
4941 // Or the load with the bias.
Dale Johannesence0805b2009-02-03 19:33:06 +00004942 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4943 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4944 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Chengc0ab5e52009-01-19 08:19:57 +00004945 MVT::v2f64, Load)),
Dale Johannesence0805b2009-02-03 19:33:06 +00004946 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4947 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Chengc0ab5e52009-01-19 08:19:57 +00004948 MVT::v2f64, Bias)));
Dale Johannesence0805b2009-02-03 19:33:06 +00004949 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4950 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling14a30ef2009-01-17 03:56:04 +00004951 DAG.getIntPtrConstant(0));
4952
4953 // Subtract the bias.
Dale Johannesence0805b2009-02-03 19:33:06 +00004954 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling14a30ef2009-01-17 03:56:04 +00004955
4956 // Handle final rounding.
Bill Wendlingdb547de2009-01-17 07:40:19 +00004957 MVT DestVT = Op.getValueType();
4958
4959 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004960 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendlingdb547de2009-01-17 07:40:19 +00004961 DAG.getIntPtrConstant(0));
4962 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004963 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendlingdb547de2009-01-17 07:40:19 +00004964 }
4965
4966 // Handle final rounding.
4967 return Sub;
Bill Wendling14a30ef2009-01-17 03:56:04 +00004968}
4969
4970SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Cheng44fd2392009-01-19 08:08:22 +00004971 SDValue N0 = Op.getOperand(0);
Dale Johannesence0805b2009-02-03 19:33:06 +00004972 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling14a30ef2009-01-17 03:56:04 +00004973
Evan Cheng44fd2392009-01-19 08:08:22 +00004974 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4975 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4976 // the optimization here.
4977 if (DAG.SignBitIsZero(N0))
Dale Johannesence0805b2009-02-03 19:33:06 +00004978 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Cheng44fd2392009-01-19 08:08:22 +00004979
4980 MVT SrcVT = N0.getValueType();
Bill Wendling14a30ef2009-01-17 03:56:04 +00004981 if (SrcVT == MVT::i64) {
4982 // We only handle SSE2 f64 target here; caller can handle the rest.
4983 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4984 return SDValue();
Bill Wendlingdb547de2009-01-17 07:40:19 +00004985
Bill Wendling14a30ef2009-01-17 03:56:04 +00004986 return LowerUINT_TO_FP_i64(Op, DAG);
4987 } else if (SrcVT == MVT::i32) {
Bill Wendling14a30ef2009-01-17 03:56:04 +00004988 return LowerUINT_TO_FP_i32(Op, DAG);
4989 }
4990
4991 assert(0 && "Unknown UINT_TO_FP to lower!");
4992 return SDValue();
4993}
4994
Dan Gohman8181bd12008-07-27 21:46:04 +00004995std::pair<SDValue,SDValue> X86TargetLowering::
4996FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00004997 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004998 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4999 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005000 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005001
Dale Johannesen2fc20782007-09-14 22:26:36 +00005002 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00005003 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005004 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00005005 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00005006 if (Subtarget->is64Bit() &&
5007 Op.getValueType() == MVT::i64 &&
5008 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00005009 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00005010
Evan Cheng05441e62007-10-15 20:11:21 +00005011 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5012 // stack slot.
5013 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00005014 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00005015 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00005016 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005017 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00005018 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005019 default: assert(0 && "Invalid FP_TO_SINT to lower!");
5020 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5021 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5022 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005023 }
5024
Dan Gohman8181bd12008-07-27 21:46:04 +00005025 SDValue Chain = DAG.getEntryNode();
5026 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00005027 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005028 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesence0805b2009-02-03 19:33:06 +00005029 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005030 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005031 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00005032 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005033 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5034 };
Dale Johannesence0805b2009-02-03 19:33:06 +00005035 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005036 Chain = Value.getValue(1);
5037 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
5038 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5039 }
5040
5041 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00005042 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesence0805b2009-02-03 19:33:06 +00005043 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005044
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005045 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005046}
5047
Dan Gohman8181bd12008-07-27 21:46:04 +00005048SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
5049 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
5050 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00005051 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005052
5053 // Load the result.
Dale Johannesence0805b2009-02-03 19:33:06 +00005054 return DAG.getLoad(Op.getValueType(), Op.getNode()->getDebugLoc(),
5055 FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005056}
5057
Dan Gohman8181bd12008-07-27 21:46:04 +00005058SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005059 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00005060 MVT VT = Op.getValueType();
5061 MVT EltVT = VT;
5062 if (VT.isVector())
5063 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005064 std::vector<Constant*> CV;
5065 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005066 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005067 CV.push_back(C);
5068 CV.push_back(C);
5069 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005070 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005071 CV.push_back(C);
5072 CV.push_back(C);
5073 CV.push_back(C);
5074 CV.push_back(C);
5075 }
Dan Gohman11821702007-07-27 17:16:43 +00005076 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005077 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00005078 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005079 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005080 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005081 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005082}
5083
Dan Gohman8181bd12008-07-27 21:46:04 +00005084SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005085 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00005086 MVT VT = Op.getValueType();
5087 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00005088 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00005089 if (VT.isVector()) {
5090 EltVT = VT.getVectorElementType();
5091 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00005092 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005093 std::vector<Constant*> CV;
5094 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005095 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005096 CV.push_back(C);
5097 CV.push_back(C);
5098 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005099 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005100 CV.push_back(C);
5101 CV.push_back(C);
5102 CV.push_back(C);
5103 CV.push_back(C);
5104 }
Dan Gohman11821702007-07-27 17:16:43 +00005105 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005106 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00005107 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005108 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005109 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00005110 if (VT.isVector()) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005111 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
5112 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5113 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
5114 Op.getOperand(0)),
5115 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Cheng92b8f782007-07-19 23:36:01 +00005116 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00005117 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng92b8f782007-07-19 23:36:01 +00005118 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005119}
5120
Dan Gohman8181bd12008-07-27 21:46:04 +00005121SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
5122 SDValue Op0 = Op.getOperand(0);
5123 SDValue Op1 = Op.getOperand(1);
Dale Johannesence0805b2009-02-03 19:33:06 +00005124 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00005125 MVT VT = Op.getValueType();
5126 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005127
5128 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005129 if (SrcVT.bitsLT(VT)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005130 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005131 SrcVT = VT;
5132 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005133 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005134 if (SrcVT.bitsGT(VT)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005135 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005136 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005137 }
5138
5139 // At this point the operands and the result should have the same
5140 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005141
5142 // First get the sign bit of second operand.
5143 std::vector<Constant*> CV;
5144 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005145 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
5146 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005147 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005148 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
5149 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5150 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5151 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005152 }
Dan Gohman11821702007-07-27 17:16:43 +00005153 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005154 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00005155 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005156 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005157 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005158 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005159
5160 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005161 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005162 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesence0805b2009-02-03 19:33:06 +00005163 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5164 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005165 DAG.getConstant(32, MVT::i32));
Dale Johannesence0805b2009-02-03 19:33:06 +00005166 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5167 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00005168 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005169 }
5170
5171 // Clear first operand sign bit.
5172 CV.clear();
5173 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005174 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
5175 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005176 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005177 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
5178 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5179 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5180 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005181 }
Dan Gohman11821702007-07-27 17:16:43 +00005182 C = ConstantVector::get(CV);
5183 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesence0805b2009-02-03 19:33:06 +00005184 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005185 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005186 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005187 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005188
5189 // Or the value with the sign bit.
Dale Johannesence0805b2009-02-03 19:33:06 +00005190 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005191}
5192
Dan Gohman8181bd12008-07-27 21:46:04 +00005193SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00005194 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00005195 SDValue Op0 = Op.getOperand(0);
5196 SDValue Op1 = Op.getOperand(1);
Dale Johannesence0805b2009-02-03 19:33:06 +00005197 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattner77a62312008-12-25 05:34:37 +00005198 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5199
Dan Gohman22cefb02009-01-29 01:59:02 +00005200 // Lower (X & (1 << N)) == 0 to BT(X, N).
5201 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5202 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman13dd9522009-01-13 23:25:30 +00005203 if (Op0.getOpcode() == ISD::AND &&
5204 Op0.hasOneUse() &&
5205 Op1.getOpcode() == ISD::Constant &&
Dan Gohman22cefb02009-01-29 01:59:02 +00005206 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattner77a62312008-12-25 05:34:37 +00005207 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohman22cefb02009-01-29 01:59:02 +00005208 SDValue LHS, RHS;
5209 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5210 if (ConstantSDNode *Op010C =
5211 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5212 if (Op010C->getZExtValue() == 1) {
5213 LHS = Op0.getOperand(0);
5214 RHS = Op0.getOperand(1).getOperand(1);
5215 }
5216 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5217 if (ConstantSDNode *Op000C =
5218 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5219 if (Op000C->getZExtValue() == 1) {
5220 LHS = Op0.getOperand(1);
5221 RHS = Op0.getOperand(0).getOperand(1);
5222 }
5223 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5224 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5225 SDValue AndLHS = Op0.getOperand(0);
5226 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5227 LHS = AndLHS.getOperand(0);
5228 RHS = AndLHS.getOperand(1);
5229 }
5230 }
Evan Cheng950aac02007-09-25 01:57:46 +00005231
Dan Gohman22cefb02009-01-29 01:59:02 +00005232 if (LHS.getNode()) {
Chris Lattner77a62312008-12-25 05:34:37 +00005233 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5234 // instruction. Since the shift amount is in-range-or-undefined, we know
5235 // that doing a bittest on the i16 value is ok. We extend to i32 because
5236 // the encoding for the i16 version is larger than the i32 version.
5237 if (LHS.getValueType() == MVT::i8)
Dale Johannesence0805b2009-02-03 19:33:06 +00005238 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattner77a62312008-12-25 05:34:37 +00005239
5240 // If the operand types disagree, extend the shift amount to match. Since
5241 // BT ignores high bits (like shifts) we can use anyextend.
5242 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesence0805b2009-02-03 19:33:06 +00005243 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohman22cefb02009-01-29 01:59:02 +00005244
Dale Johannesence0805b2009-02-03 19:33:06 +00005245 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005246 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesence0805b2009-02-03 19:33:06 +00005247 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner77a62312008-12-25 05:34:37 +00005248 DAG.getConstant(Cond, MVT::i8), BT);
5249 }
5250 }
5251
5252 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5253 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Chris Lattner60435922008-12-24 00:11:37 +00005254
Dale Johannesence0805b2009-02-03 19:33:06 +00005255 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5256 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner60435922008-12-24 00:11:37 +00005257 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005258}
5259
Dan Gohman8181bd12008-07-27 21:46:04 +00005260SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5261 SDValue Cond;
5262 SDValue Op0 = Op.getOperand(0);
5263 SDValue Op1 = Op.getOperand(1);
5264 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00005265 MVT VT = Op.getValueType();
5266 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5267 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesence0805b2009-02-03 19:33:06 +00005268 DebugLoc dl = Op.getNode()->getDebugLoc();
Nate Begeman03605a02008-07-17 16:51:19 +00005269
5270 if (isFP) {
5271 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00005272 MVT VT0 = Op0.getValueType();
5273 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5274 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00005275 bool Swap = false;
5276
5277 switch (SetCCOpcode) {
5278 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005279 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00005280 case ISD::SETEQ: SSECC = 0; break;
5281 case ISD::SETOGT:
5282 case ISD::SETGT: Swap = true; // Fallthrough
5283 case ISD::SETLT:
5284 case ISD::SETOLT: SSECC = 1; break;
5285 case ISD::SETOGE:
5286 case ISD::SETGE: Swap = true; // Fallthrough
5287 case ISD::SETLE:
5288 case ISD::SETOLE: SSECC = 2; break;
5289 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005290 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00005291 case ISD::SETNE: SSECC = 4; break;
5292 case ISD::SETULE: Swap = true;
5293 case ISD::SETUGE: SSECC = 5; break;
5294 case ISD::SETULT: Swap = true;
5295 case ISD::SETUGT: SSECC = 6; break;
5296 case ISD::SETO: SSECC = 7; break;
5297 }
5298 if (Swap)
5299 std::swap(Op0, Op1);
5300
Nate Begeman6357f9d2008-07-25 19:05:58 +00005301 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00005302 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00005303 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005304 SDValue UNORD, EQ;
Dale Johannesence0805b2009-02-03 19:33:06 +00005305 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5306 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5307 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begeman6357f9d2008-07-25 19:05:58 +00005308 }
5309 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005310 SDValue ORD, NEQ;
Dale Johannesence0805b2009-02-03 19:33:06 +00005311 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5312 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5313 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begeman6357f9d2008-07-25 19:05:58 +00005314 }
5315 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00005316 }
5317 // Handle all other FP comparisons here.
Dale Johannesence0805b2009-02-03 19:33:06 +00005318 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman03605a02008-07-17 16:51:19 +00005319 }
5320
5321 // We are handling one of the integer comparisons here. Since SSE only has
5322 // GT and EQ comparisons for integer, swapping operands and multiple
5323 // operations may be required for some comparisons.
5324 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5325 bool Swap = false, Invert = false, FlipSigns = false;
5326
5327 switch (VT.getSimpleVT()) {
5328 default: break;
5329 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5330 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5331 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5332 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5333 }
5334
5335 switch (SetCCOpcode) {
5336 default: break;
5337 case ISD::SETNE: Invert = true;
5338 case ISD::SETEQ: Opc = EQOpc; break;
5339 case ISD::SETLT: Swap = true;
5340 case ISD::SETGT: Opc = GTOpc; break;
5341 case ISD::SETGE: Swap = true;
5342 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5343 case ISD::SETULT: Swap = true;
5344 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5345 case ISD::SETUGE: Swap = true;
5346 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5347 }
5348 if (Swap)
5349 std::swap(Op0, Op1);
5350
5351 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5352 // bits of the inputs before performing those operations.
5353 if (FlipSigns) {
5354 MVT EltVT = VT.getVectorElementType();
Duncan Sands505ba942009-02-01 18:06:53 +00005355 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5356 EltVT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005357 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Dale Johannesence0805b2009-02-03 19:33:06 +00005358 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005359 SignBits.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00005360 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5361 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman03605a02008-07-17 16:51:19 +00005362 }
5363
Dale Johannesence0805b2009-02-03 19:33:06 +00005364 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005365
5366 // If the logical-not of the result is required, perform that now.
Bob Wilson81a42cf2009-01-22 17:39:32 +00005367 if (Invert)
Dale Johannesence0805b2009-02-03 19:33:06 +00005368 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson81a42cf2009-01-22 17:39:32 +00005369
Nate Begeman03605a02008-07-17 16:51:19 +00005370 return Result;
5371}
Evan Cheng950aac02007-09-25 01:57:46 +00005372
Evan Chengd580f022008-12-03 08:38:43 +00005373// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
5374static bool isX86LogicalCmp(unsigned Opc) {
5375 return Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI;
5376}
5377
Dan Gohman8181bd12008-07-27 21:46:04 +00005378SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005379 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005380 SDValue Cond = Op.getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005381 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00005382 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005383
5384 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005385 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005386
Evan Cheng50d37ab2007-10-08 22:16:29 +00005387 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5388 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005389 if (Cond.getOpcode() == X86ISD::SETCC) {
5390 CC = Cond.getOperand(0);
5391
Dan Gohman8181bd12008-07-27 21:46:04 +00005392 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005393 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005394 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005395
Evan Cheng50d37ab2007-10-08 22:16:29 +00005396 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005397 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005398 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005399 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005400
Dan Gohman22cefb02009-01-29 01:59:02 +00005401 if ((isX86LogicalCmp(Opc) && !IllegalFPCMov) || Opc == X86ISD::BT) { // FIXME
Evan Cheng50d37ab2007-10-08 22:16:29 +00005402 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005403 addTest = false;
5404 }
5405 }
5406
5407 if (addTest) {
5408 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005409 Cond= DAG.getNode(X86ISD::CMP, dl, MVT::i32, Cond,
5410 DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005411 }
5412
Duncan Sands92c43912008-06-06 12:08:01 +00005413 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005414 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005415 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005416 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5417 // condition is true.
5418 Ops.push_back(Op.getOperand(2));
5419 Ops.push_back(Op.getOperand(1));
5420 Ops.push_back(CC);
5421 Ops.push_back(Cond);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005422 return DAG.getNode(X86ISD::CMOV, dl, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005423}
5424
Evan Chengd580f022008-12-03 08:38:43 +00005425// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5426// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5427// from the AND / OR.
5428static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5429 Opc = Op.getOpcode();
5430 if (Opc != ISD::OR && Opc != ISD::AND)
5431 return false;
5432 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5433 Op.getOperand(0).hasOneUse() &&
5434 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5435 Op.getOperand(1).hasOneUse());
5436}
5437
Evan Cheng67f98b12009-02-02 08:19:07 +00005438// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5439// 1 and that the SETCC node has a single use.
Evan Cheng8c3af2c2009-02-02 08:07:36 +00005440static bool isXor1OfSetCC(SDValue Op) {
5441 if (Op.getOpcode() != ISD::XOR)
5442 return false;
5443 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5444 if (N1C && N1C->getAPIntValue() == 1) {
5445 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5446 Op.getOperand(0).hasOneUse();
5447 }
5448 return false;
5449}
5450
Dan Gohman8181bd12008-07-27 21:46:04 +00005451SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005452 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005453 SDValue Chain = Op.getOperand(0);
5454 SDValue Cond = Op.getOperand(1);
5455 SDValue Dest = Op.getOperand(2);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005456 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00005457 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005458
5459 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005460 Cond = LowerSETCC(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005461#if 0
5462 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingf5399032008-12-12 21:15:41 +00005463 else if (Cond.getOpcode() == X86ISD::ADD ||
5464 Cond.getOpcode() == X86ISD::SUB ||
5465 Cond.getOpcode() == X86ISD::SMUL ||
5466 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling7e04be62008-12-09 22:08:41 +00005467 Cond = LowerXALUO(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005468#endif
5469
Evan Cheng50d37ab2007-10-08 22:16:29 +00005470 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5471 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005472 if (Cond.getOpcode() == X86ISD::SETCC) {
5473 CC = Cond.getOperand(0);
5474
Dan Gohman8181bd12008-07-27 21:46:04 +00005475 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005476 unsigned Opc = Cmp.getOpcode();
Chris Lattner77a62312008-12-25 05:34:37 +00005477 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
5478 if (isX86LogicalCmp(Opc) || Opc == X86ISD::BT) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005479 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005480 addTest = false;
Bill Wendlingd3511522008-12-02 01:06:39 +00005481 } else {
Evan Chengd580f022008-12-03 08:38:43 +00005482 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling809e7bd2008-12-03 08:32:02 +00005483 default: break;
5484 case X86::COND_O:
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005485 case X86::COND_B:
Chris Lattner77a62312008-12-25 05:34:37 +00005486 // These can only come from an arithmetic instruction with overflow,
5487 // e.g. SADDO, UADDO.
Bill Wendling809e7bd2008-12-03 08:32:02 +00005488 Cond = Cond.getNode()->getOperand(1);
5489 addTest = false;
5490 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00005491 }
Evan Cheng950aac02007-09-25 01:57:46 +00005492 }
Evan Chengd580f022008-12-03 08:38:43 +00005493 } else {
5494 unsigned CondOpc;
5495 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5496 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5497 unsigned Opc = Cmp.getOpcode();
5498 if (CondOpc == ISD::OR) {
5499 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5500 // two branches instead of an explicit OR instruction with a
5501 // separate test.
5502 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5503 isX86LogicalCmp(Opc)) {
5504 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005505 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Chengd580f022008-12-03 08:38:43 +00005506 Chain, Dest, CC, Cmp);
5507 CC = Cond.getOperand(1).getOperand(0);
5508 Cond = Cmp;
5509 addTest = false;
5510 }
5511 } else { // ISD::AND
5512 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5513 // two branches instead of an explicit AND instruction with a
5514 // separate test. However, we only do this if this block doesn't
5515 // have a fall-through edge, because this requires an explicit
5516 // jmp when the condition is false.
5517 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5518 isX86LogicalCmp(Opc) &&
5519 Op.getNode()->hasOneUse()) {
5520 X86::CondCode CCode =
5521 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5522 CCode = X86::GetOppositeBranchCondition(CCode);
5523 CC = DAG.getConstant(CCode, MVT::i8);
5524 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5525 // Look for an unconditional branch following this conditional branch.
5526 // We need this because we need to reverse the successors in order
5527 // to implement FCMP_OEQ.
5528 if (User.getOpcode() == ISD::BR) {
5529 SDValue FalseBB = User.getOperand(1);
5530 SDValue NewBR =
5531 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5532 assert(NewBR == User);
5533 Dest = FalseBB;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005534
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005535 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Chengd580f022008-12-03 08:38:43 +00005536 Chain, Dest, CC, Cmp);
5537 X86::CondCode CCode =
5538 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5539 CCode = X86::GetOppositeBranchCondition(CCode);
5540 CC = DAG.getConstant(CCode, MVT::i8);
5541 Cond = Cmp;
5542 addTest = false;
5543 }
5544 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005545 }
Evan Cheng8c3af2c2009-02-02 08:07:36 +00005546 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5547 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5548 // It should be transformed during dag combiner except when the condition
5549 // is set by a arithmetics with overflow node.
5550 X86::CondCode CCode =
5551 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5552 CCode = X86::GetOppositeBranchCondition(CCode);
5553 CC = DAG.getConstant(CCode, MVT::i8);
5554 Cond = Cond.getOperand(0).getOperand(1);
5555 addTest = false;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005556 }
Evan Cheng950aac02007-09-25 01:57:46 +00005557 }
5558
5559 if (addTest) {
5560 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005561 Cond= DAG.getNode(X86ISD::CMP, dl, MVT::i32, Cond,
5562 DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005563 }
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005564 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005565 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005566}
5567
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005568
5569// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5570// Calls to _alloca is needed to probe the stack when allocating more than 4k
5571// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5572// that the guard pages used by the OS virtual memory manager are allocated in
5573// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005574SDValue
5575X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005576 SelectionDAG &DAG) {
5577 assert(Subtarget->isTargetCygMing() &&
5578 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005579 DebugLoc dl = Op.getNode()->getDebugLoc();
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005580
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005581 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005582 SDValue Chain = Op.getOperand(0);
5583 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005584 // FIXME: Ensure alignment here
5585
Dan Gohman8181bd12008-07-27 21:46:04 +00005586 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005587
Duncan Sands92c43912008-06-06 12:08:01 +00005588 MVT IntPtr = getPointerTy();
5589 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005590
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005591 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005592
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00005593 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005594 Flag = Chain.getValue(1);
5595
5596 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005597 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005598 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005599 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005600 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005601 Flag };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005602 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005603 Flag = Chain.getValue(1);
5604
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005605 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005606 DAG.getIntPtrConstant(0, true),
5607 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005608 Flag);
5609
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00005610 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005611
Dan Gohman8181bd12008-07-27 21:46:04 +00005612 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005613 return DAG.getMergeValues(Ops1, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005614}
5615
Dan Gohman8181bd12008-07-27 21:46:04 +00005616SDValue
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005617X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005618 SDValue Chain,
5619 SDValue Dst, SDValue Src,
5620 SDValue Size, unsigned Align,
5621 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005622 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005623 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005624
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005625 // If not DWORD aligned or size is more than the threshold, call the library.
5626 // The libc version is likely to be faster for these cases. It can use the
5627 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005628 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005629 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005630 ConstantSize->getZExtValue() >
5631 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005632 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005633
5634 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005635 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005636
Bill Wendling4b2e3782008-10-01 00:59:58 +00005637 if (const char *bzeroEntry = V &&
5638 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5639 MVT IntPtr = getPointerTy();
5640 const Type *IntPtrTy = TD->getIntPtrType();
5641 TargetLowering::ArgListTy Args;
5642 TargetLowering::ArgListEntry Entry;
5643 Entry.Node = Dst;
5644 Entry.Ty = IntPtrTy;
5645 Args.push_back(Entry);
5646 Entry.Node = Size;
5647 Args.push_back(Entry);
5648 std::pair<SDValue,SDValue> CallResult =
5649 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5650 CallingConv::C, false,
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005651 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling4b2e3782008-10-01 00:59:58 +00005652 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005653 }
5654
Dan Gohmane8b391e2008-04-12 04:36:06 +00005655 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005656 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005657 }
5658
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005659 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005660 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005661 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005662 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005663 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005664 unsigned BytesLeft = 0;
5665 bool TwoRepStos = false;
5666 if (ValC) {
5667 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005668 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005669
5670 // If the value is a constant, then we can potentially use larger sets.
5671 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005672 case 2: // WORD aligned
5673 AVT = MVT::i16;
5674 ValReg = X86::AX;
5675 Val = (Val << 8) | Val;
5676 break;
5677 case 0: // DWORD aligned
5678 AVT = MVT::i32;
5679 ValReg = X86::EAX;
5680 Val = (Val << 8) | Val;
5681 Val = (Val << 16) | Val;
5682 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5683 AVT = MVT::i64;
5684 ValReg = X86::RAX;
5685 Val = (Val << 32) | Val;
5686 }
5687 break;
5688 default: // Byte aligned
5689 AVT = MVT::i8;
5690 ValReg = X86::AL;
5691 Count = DAG.getIntPtrConstant(SizeVal);
5692 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005693 }
5694
Duncan Sandsec142ee2008-06-08 20:54:56 +00005695 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005696 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005697 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5698 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005699 }
5700
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005701 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005702 InFlag);
5703 InFlag = Chain.getValue(1);
5704 } else {
5705 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005706 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005707 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005708 InFlag = Chain.getValue(1);
5709 }
5710
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005711 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
5712 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005713 Count, InFlag);
5714 InFlag = Chain.getValue(1);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005715 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
5716 X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005717 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005718 InFlag = Chain.getValue(1);
5719
5720 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005721 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005722 Ops.push_back(Chain);
5723 Ops.push_back(DAG.getValueType(AVT));
5724 Ops.push_back(InFlag);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005725 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005726
5727 if (TwoRepStos) {
5728 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005729 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005730 MVT CVT = Count.getValueType();
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005731 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005732 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005733 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
5734 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005735 Left, InFlag);
5736 InFlag = Chain.getValue(1);
5737 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5738 Ops.clear();
5739 Ops.push_back(Chain);
5740 Ops.push_back(DAG.getValueType(MVT::i8));
5741 Ops.push_back(InFlag);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005742 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005743 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005744 // Handle the last 1 - 7 bytes.
5745 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005746 MVT AddrVT = Dst.getValueType();
5747 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005748
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005749 Chain = DAG.getMemset(Chain, dl,
5750 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005751 DAG.getConstant(Offset, AddrVT)),
5752 Src,
5753 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005754 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005755 }
5756
Dan Gohmane8b391e2008-04-12 04:36:06 +00005757 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005758 return Chain;
5759}
5760
Dan Gohman8181bd12008-07-27 21:46:04 +00005761SDValue
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005762X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005763 SDValue Chain, SDValue Dst, SDValue Src,
5764 SDValue Size, unsigned Align,
5765 bool AlwaysInline,
5766 const Value *DstSV, uint64_t DstSVOff,
5767 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005768 // This requires the copy size to be a constant, preferrably
5769 // within a subtarget-specific limit.
5770 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5771 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005772 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005773 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005774 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005775 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005776
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005777 /// If not DWORD aligned, call the library.
5778 if ((Align & 3) != 0)
5779 return SDValue();
5780
5781 // DWORD aligned
5782 MVT AVT = MVT::i32;
5783 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005784 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005785
Duncan Sands92c43912008-06-06 12:08:01 +00005786 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005787 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005788 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005789 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005790
Dan Gohman8181bd12008-07-27 21:46:04 +00005791 SDValue InFlag(0, 0);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005792 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
5793 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005794 Count, InFlag);
5795 InFlag = Chain.getValue(1);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005796 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
5797 X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005798 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005799 InFlag = Chain.getValue(1);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005800 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
5801 X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005802 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005803 InFlag = Chain.getValue(1);
5804
5805 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005806 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005807 Ops.push_back(Chain);
5808 Ops.push_back(DAG.getValueType(AVT));
5809 Ops.push_back(InFlag);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005810 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005811
Dan Gohman8181bd12008-07-27 21:46:04 +00005812 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005813 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005814 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005815 // Handle the last 1 - 7 bytes.
5816 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005817 MVT DstVT = Dst.getValueType();
5818 MVT SrcVT = Src.getValueType();
5819 MVT SizeVT = Size.getValueType();
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005820 Results.push_back(DAG.getMemcpy(Chain, dl,
5821 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005822 DAG.getConstant(Offset, DstVT)),
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005823 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005824 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005825 DAG.getConstant(BytesLeft, SizeVT),
5826 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005827 DstSV, DstSVOff + Offset,
5828 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005829 }
5830
Dale Johannesen7f2abf42009-02-03 22:26:09 +00005831 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
5832 &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005833}
5834
Dan Gohman8181bd12008-07-27 21:46:04 +00005835SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005836 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005837 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005838
5839 if (!Subtarget->is64Bit()) {
5840 // vastart just stores the address of the VarArgsFrameIndex slot into the
5841 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005842 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005843 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005844 }
5845
5846 // __va_list_tag:
5847 // gp_offset (0 - 6 * 8)
5848 // fp_offset (48 - 48 + 8 * 16)
5849 // overflow_arg_area (point to parameters coming in memory).
5850 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005851 SmallVector<SDValue, 8> MemOps;
5852 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005853 // Store gp_offset
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005854 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005855 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005856 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005857 MemOps.push_back(Store);
5858
5859 // Store fp_offset
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005860 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5861 FIN, DAG.getIntPtrConstant(4));
5862 Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005863 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005864 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005865 MemOps.push_back(Store);
5866
5867 // Store ptr to overflow_arg_area
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005868 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5869 FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005870 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005871 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005872 MemOps.push_back(Store);
5873
5874 // Store ptr to reg_save_area.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005875 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5876 FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005877 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005878 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005879 MemOps.push_back(Store);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005880 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
5881 &MemOps[0], MemOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005882}
5883
Dan Gohman8181bd12008-07-27 21:46:04 +00005884SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005885 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5886 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005887 SDValue Chain = Op.getOperand(0);
5888 SDValue SrcPtr = Op.getOperand(1);
5889 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005890
5891 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5892 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005893 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005894}
5895
Dan Gohman8181bd12008-07-27 21:46:04 +00005896SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005897 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005898 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005899 SDValue Chain = Op.getOperand(0);
5900 SDValue DstPtr = Op.getOperand(1);
5901 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005902 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5903 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00005904 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005905
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00005906 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman840ff5c2008-04-18 20:55:41 +00005907 DAG.getIntPtrConstant(24), 8, false,
5908 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005909}
5910
Dan Gohman8181bd12008-07-27 21:46:04 +00005911SDValue
5912X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00005913 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005914 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005915 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005916 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005917 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005918 case Intrinsic::x86_sse_comieq_ss:
5919 case Intrinsic::x86_sse_comilt_ss:
5920 case Intrinsic::x86_sse_comile_ss:
5921 case Intrinsic::x86_sse_comigt_ss:
5922 case Intrinsic::x86_sse_comige_ss:
5923 case Intrinsic::x86_sse_comineq_ss:
5924 case Intrinsic::x86_sse_ucomieq_ss:
5925 case Intrinsic::x86_sse_ucomilt_ss:
5926 case Intrinsic::x86_sse_ucomile_ss:
5927 case Intrinsic::x86_sse_ucomigt_ss:
5928 case Intrinsic::x86_sse_ucomige_ss:
5929 case Intrinsic::x86_sse_ucomineq_ss:
5930 case Intrinsic::x86_sse2_comieq_sd:
5931 case Intrinsic::x86_sse2_comilt_sd:
5932 case Intrinsic::x86_sse2_comile_sd:
5933 case Intrinsic::x86_sse2_comigt_sd:
5934 case Intrinsic::x86_sse2_comige_sd:
5935 case Intrinsic::x86_sse2_comineq_sd:
5936 case Intrinsic::x86_sse2_ucomieq_sd:
5937 case Intrinsic::x86_sse2_ucomilt_sd:
5938 case Intrinsic::x86_sse2_ucomile_sd:
5939 case Intrinsic::x86_sse2_ucomigt_sd:
5940 case Intrinsic::x86_sse2_ucomige_sd:
5941 case Intrinsic::x86_sse2_ucomineq_sd: {
5942 unsigned Opc = 0;
5943 ISD::CondCode CC = ISD::SETCC_INVALID;
5944 switch (IntNo) {
5945 default: break;
5946 case Intrinsic::x86_sse_comieq_ss:
5947 case Intrinsic::x86_sse2_comieq_sd:
5948 Opc = X86ISD::COMI;
5949 CC = ISD::SETEQ;
5950 break;
5951 case Intrinsic::x86_sse_comilt_ss:
5952 case Intrinsic::x86_sse2_comilt_sd:
5953 Opc = X86ISD::COMI;
5954 CC = ISD::SETLT;
5955 break;
5956 case Intrinsic::x86_sse_comile_ss:
5957 case Intrinsic::x86_sse2_comile_sd:
5958 Opc = X86ISD::COMI;
5959 CC = ISD::SETLE;
5960 break;
5961 case Intrinsic::x86_sse_comigt_ss:
5962 case Intrinsic::x86_sse2_comigt_sd:
5963 Opc = X86ISD::COMI;
5964 CC = ISD::SETGT;
5965 break;
5966 case Intrinsic::x86_sse_comige_ss:
5967 case Intrinsic::x86_sse2_comige_sd:
5968 Opc = X86ISD::COMI;
5969 CC = ISD::SETGE;
5970 break;
5971 case Intrinsic::x86_sse_comineq_ss:
5972 case Intrinsic::x86_sse2_comineq_sd:
5973 Opc = X86ISD::COMI;
5974 CC = ISD::SETNE;
5975 break;
5976 case Intrinsic::x86_sse_ucomieq_ss:
5977 case Intrinsic::x86_sse2_ucomieq_sd:
5978 Opc = X86ISD::UCOMI;
5979 CC = ISD::SETEQ;
5980 break;
5981 case Intrinsic::x86_sse_ucomilt_ss:
5982 case Intrinsic::x86_sse2_ucomilt_sd:
5983 Opc = X86ISD::UCOMI;
5984 CC = ISD::SETLT;
5985 break;
5986 case Intrinsic::x86_sse_ucomile_ss:
5987 case Intrinsic::x86_sse2_ucomile_sd:
5988 Opc = X86ISD::UCOMI;
5989 CC = ISD::SETLE;
5990 break;
5991 case Intrinsic::x86_sse_ucomigt_ss:
5992 case Intrinsic::x86_sse2_ucomigt_sd:
5993 Opc = X86ISD::UCOMI;
5994 CC = ISD::SETGT;
5995 break;
5996 case Intrinsic::x86_sse_ucomige_ss:
5997 case Intrinsic::x86_sse2_ucomige_sd:
5998 Opc = X86ISD::UCOMI;
5999 CC = ISD::SETGE;
6000 break;
6001 case Intrinsic::x86_sse_ucomineq_ss:
6002 case Intrinsic::x86_sse2_ucomineq_sd:
6003 Opc = X86ISD::UCOMI;
6004 CC = ISD::SETNE;
6005 break;
6006 }
6007
Dan Gohman8181bd12008-07-27 21:46:04 +00006008 SDValue LHS = Op.getOperand(1);
6009 SDValue RHS = Op.getOperand(2);
Chris Lattnerebb91142008-12-24 23:53:05 +00006010 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006011 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6012 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00006013 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006014 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006015 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006016
6017 // Fix vector shift instructions where the last operand is a non-immediate
6018 // i32 value.
6019 case Intrinsic::x86_sse2_pslli_w:
6020 case Intrinsic::x86_sse2_pslli_d:
6021 case Intrinsic::x86_sse2_pslli_q:
6022 case Intrinsic::x86_sse2_psrli_w:
6023 case Intrinsic::x86_sse2_psrli_d:
6024 case Intrinsic::x86_sse2_psrli_q:
6025 case Intrinsic::x86_sse2_psrai_w:
6026 case Intrinsic::x86_sse2_psrai_d:
6027 case Intrinsic::x86_mmx_pslli_w:
6028 case Intrinsic::x86_mmx_pslli_d:
6029 case Intrinsic::x86_mmx_pslli_q:
6030 case Intrinsic::x86_mmx_psrli_w:
6031 case Intrinsic::x86_mmx_psrli_d:
6032 case Intrinsic::x86_mmx_psrli_q:
6033 case Intrinsic::x86_mmx_psrai_w:
6034 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006035 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006036 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006037 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006038
6039 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006040 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006041 switch (IntNo) {
6042 case Intrinsic::x86_sse2_pslli_w:
6043 NewIntNo = Intrinsic::x86_sse2_psll_w;
6044 break;
6045 case Intrinsic::x86_sse2_pslli_d:
6046 NewIntNo = Intrinsic::x86_sse2_psll_d;
6047 break;
6048 case Intrinsic::x86_sse2_pslli_q:
6049 NewIntNo = Intrinsic::x86_sse2_psll_q;
6050 break;
6051 case Intrinsic::x86_sse2_psrli_w:
6052 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6053 break;
6054 case Intrinsic::x86_sse2_psrli_d:
6055 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6056 break;
6057 case Intrinsic::x86_sse2_psrli_q:
6058 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6059 break;
6060 case Intrinsic::x86_sse2_psrai_w:
6061 NewIntNo = Intrinsic::x86_sse2_psra_w;
6062 break;
6063 case Intrinsic::x86_sse2_psrai_d:
6064 NewIntNo = Intrinsic::x86_sse2_psra_d;
6065 break;
6066 default: {
6067 ShAmtVT = MVT::v2i32;
6068 switch (IntNo) {
6069 case Intrinsic::x86_mmx_pslli_w:
6070 NewIntNo = Intrinsic::x86_mmx_psll_w;
6071 break;
6072 case Intrinsic::x86_mmx_pslli_d:
6073 NewIntNo = Intrinsic::x86_mmx_psll_d;
6074 break;
6075 case Intrinsic::x86_mmx_pslli_q:
6076 NewIntNo = Intrinsic::x86_mmx_psll_q;
6077 break;
6078 case Intrinsic::x86_mmx_psrli_w:
6079 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6080 break;
6081 case Intrinsic::x86_mmx_psrli_d:
6082 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6083 break;
6084 case Intrinsic::x86_mmx_psrli_q:
6085 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6086 break;
6087 case Intrinsic::x86_mmx_psrai_w:
6088 NewIntNo = Intrinsic::x86_mmx_psra_w;
6089 break;
6090 case Intrinsic::x86_mmx_psrai_d:
6091 NewIntNo = Intrinsic::x86_mmx_psra_d;
6092 break;
6093 default: abort(); // Can't reach here.
6094 }
6095 break;
6096 }
6097 }
Duncan Sands92c43912008-06-06 12:08:01 +00006098 MVT VT = Op.getValueType();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006099 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6100 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6101 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006102 DAG.getConstant(NewIntNo, MVT::i32),
6103 Op.getOperand(1), ShAmt);
6104 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006105 }
6106}
6107
Dan Gohman8181bd12008-07-27 21:46:04 +00006108SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006109 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006110 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006111
6112 if (Depth > 0) {
6113 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6114 SDValue Offset =
6115 DAG.getConstant(TD->getPointerSize(),
6116 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006117 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
6118 DAG.getNode(ISD::ADD, dl, getPointerTy(),
6119 FrameAddr, Offset),
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006120 NULL, 0);
6121 }
6122
6123 // Just load the return address.
Dan Gohman8181bd12008-07-27 21:46:04 +00006124 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006125 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
6126 RetAddrFI, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006127}
6128
Dan Gohman8181bd12008-07-27 21:46:04 +00006129SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00006130 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6131 MFI->setFrameAddressIsTaken(true);
6132 MVT VT = Op.getValueType();
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006133 DebugLoc dl = Op.getNode()->getDebugLoc(); // FIXME probably not meaningful
Evan Cheng33633672008-09-27 01:56:22 +00006134 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6135 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006136 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng33633672008-09-27 01:56:22 +00006137 while (Depth--)
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006138 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng33633672008-09-27 01:56:22 +00006139 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006140}
6141
Dan Gohman8181bd12008-07-27 21:46:04 +00006142SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00006143 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006144 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006145}
6146
Dan Gohman8181bd12008-07-27 21:46:04 +00006147SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006148{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006149 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00006150 SDValue Chain = Op.getOperand(0);
6151 SDValue Offset = Op.getOperand(1);
6152 SDValue Handler = Op.getOperand(2);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006153 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006154
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006155 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6156 getPointerTy());
6157 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006158
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006159 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006160 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006161 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6162 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006163 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006164 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006165
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006166 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006167 MVT::Other,
6168 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006169}
6170
Dan Gohman8181bd12008-07-27 21:46:04 +00006171SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006172 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006173 SDValue Root = Op.getOperand(0);
6174 SDValue Trmp = Op.getOperand(1); // trampoline
6175 SDValue FPtr = Op.getOperand(2); // nested function
6176 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006177 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006178
Dan Gohman12a9c082008-02-06 22:27:42 +00006179 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006180
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006181 const X86InstrInfo *TII =
6182 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6183
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006184 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006185 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006186
6187 // Large code-model.
6188
6189 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6190 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6191
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006192 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6193 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006194
6195 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6196
6197 // Load the pointer to the nested function into R11.
6198 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00006199 SDValue Addr = Trmp;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006200 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6201 Addr, TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006202
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006203 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6204 DAG.getConstant(2, MVT::i64));
6205 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006206
6207 // Load the 'nest' parameter value into R10.
6208 // R10 is specified in X86CallingConv.td
6209 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006210 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6211 DAG.getConstant(10, MVT::i64));
6212 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6213 Addr, TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006214
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006215 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6216 DAG.getConstant(12, MVT::i64));
6217 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006218
6219 // Jump to the nested function.
6220 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006221 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6222 DAG.getConstant(20, MVT::i64));
6223 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6224 Addr, TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006225
6226 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006227 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6228 DAG.getConstant(22, MVT::i64));
6229 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006230 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006231
Dan Gohman8181bd12008-07-27 21:46:04 +00006232 SDValue Ops[] =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006233 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6234 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006235 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00006236 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006237 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6238 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00006239 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006240
6241 switch (CC) {
6242 default:
6243 assert(0 && "Unsupported calling convention");
6244 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006245 case CallingConv::X86_StdCall: {
6246 // Pass 'nest' parameter in ECX.
6247 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006248 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006249
6250 // Check that ECX wasn't needed by an 'inreg' parameter.
6251 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00006252 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006253
Chris Lattner1c8733e2008-03-12 17:45:29 +00006254 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006255 unsigned InRegCount = 0;
6256 unsigned Idx = 1;
6257
6258 for (FunctionType::param_iterator I = FTy->param_begin(),
6259 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00006260 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006261 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006262 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006263
6264 if (InRegCount > 2) {
6265 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6266 abort();
6267 }
6268 }
6269 break;
6270 }
6271 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00006272 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006273 // Pass 'nest' parameter in EAX.
6274 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006275 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006276 break;
6277 }
6278
Dan Gohman8181bd12008-07-27 21:46:04 +00006279 SDValue OutChains[4];
6280 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006281
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006282 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6283 DAG.getConstant(10, MVT::i32));
6284 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006285
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006286 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006287 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006288 OutChains[0] = DAG.getStore(Root, dl,
6289 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00006290 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006291
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006292 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6293 DAG.getConstant(1, MVT::i32));
6294 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006295
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006296 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006297 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6298 DAG.getConstant(5, MVT::i32));
6299 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006300 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006301
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006302 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6303 DAG.getConstant(6, MVT::i32));
6304 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006305
Dan Gohman8181bd12008-07-27 21:46:04 +00006306 SDValue Ops[] =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006307 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6308 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006309 }
6310}
6311
Dan Gohman8181bd12008-07-27 21:46:04 +00006312SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006313 /*
6314 The rounding mode is in bits 11:10 of FPSR, and has the following
6315 settings:
6316 00 Round to nearest
6317 01 Round to -inf
6318 10 Round to +inf
6319 11 Round to 0
6320
6321 FLT_ROUNDS, on the other hand, expects the following:
6322 -1 Undefined
6323 0 Round to 0
6324 1 Round to nearest
6325 2 Round to +inf
6326 3 Round to -inf
6327
6328 To perform the conversion, we do:
6329 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6330 */
6331
6332 MachineFunction &MF = DAG.getMachineFunction();
6333 const TargetMachine &TM = MF.getTarget();
6334 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6335 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00006336 MVT VT = Op.getValueType();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006337 DebugLoc dl = Op.getNode()->getDebugLoc();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006338
6339 // Save FP Control Word to stack slot
6340 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00006341 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006342
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006343 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00006344 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006345
6346 // Load FP Control Word from stack slot
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006347 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006348
6349 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00006350 SDValue CWD1 =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006351 DAG.getNode(ISD::SRL, dl, MVT::i16,
6352 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006353 CWD, DAG.getConstant(0x800, MVT::i16)),
6354 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006355 SDValue CWD2 =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006356 DAG.getNode(ISD::SRL, dl, MVT::i16,
6357 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006358 CWD, DAG.getConstant(0x400, MVT::i16)),
6359 DAG.getConstant(9, MVT::i8));
6360
Dan Gohman8181bd12008-07-27 21:46:04 +00006361 SDValue RetVal =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006362 DAG.getNode(ISD::AND, dl, MVT::i16,
6363 DAG.getNode(ISD::ADD, dl, MVT::i16,
6364 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006365 DAG.getConstant(1, MVT::i16)),
6366 DAG.getConstant(3, MVT::i16));
6367
6368
Duncan Sands92c43912008-06-06 12:08:01 +00006369 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006370 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6371}
6372
Dan Gohman8181bd12008-07-27 21:46:04 +00006373SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006374 MVT VT = Op.getValueType();
6375 MVT OpVT = VT;
6376 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006377 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng48679f42007-12-14 02:13:44 +00006378
6379 Op = Op.getOperand(0);
6380 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006381 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00006382 OpVT = MVT::i32;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006383 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006384 }
Evan Cheng48679f42007-12-14 02:13:44 +00006385
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006386 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6387 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006388 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006389
6390 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006391 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006392 Ops.push_back(Op);
6393 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6394 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6395 Ops.push_back(Op.getValue(1));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006396 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006397
6398 // Finally xor with NumBits-1.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006399 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006400
Evan Cheng48679f42007-12-14 02:13:44 +00006401 if (VT == MVT::i8)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006402 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006403 return Op;
6404}
6405
Dan Gohman8181bd12008-07-27 21:46:04 +00006406SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006407 MVT VT = Op.getValueType();
6408 MVT OpVT = VT;
6409 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006410 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng48679f42007-12-14 02:13:44 +00006411
6412 Op = Op.getOperand(0);
6413 if (VT == MVT::i8) {
6414 OpVT = MVT::i32;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006415 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006416 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006417
6418 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6419 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006420 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006421
6422 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006423 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006424 Ops.push_back(Op);
6425 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6426 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6427 Ops.push_back(Op.getValue(1));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006428 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006429
Evan Cheng48679f42007-12-14 02:13:44 +00006430 if (VT == MVT::i8)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006431 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00006432 return Op;
6433}
6434
Mon P Wang14edb092008-12-18 21:42:19 +00006435SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6436 MVT VT = Op.getValueType();
6437 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006438 DebugLoc dl = Op.getNode()->getDebugLoc();
Mon P Wang14edb092008-12-18 21:42:19 +00006439
6440 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6441 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6442 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6443 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6444 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6445 //
6446 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6447 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6448 // return AloBlo + AloBhi + AhiBlo;
6449
6450 SDValue A = Op.getOperand(0);
6451 SDValue B = Op.getOperand(1);
6452
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006453 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006454 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6455 A, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006456 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006457 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6458 B, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006459 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006460 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6461 A, B);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006462 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006463 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6464 A, Bhi);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006465 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006466 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6467 Ahi, B);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006468 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006469 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6470 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006471 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wang14edb092008-12-18 21:42:19 +00006472 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6473 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006474 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6475 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wang14edb092008-12-18 21:42:19 +00006476 return Res;
6477}
6478
6479
Bill Wendling7e04be62008-12-09 22:08:41 +00006480SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6481 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6482 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendlingd3511522008-12-02 01:06:39 +00006483 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6484 // has only one use.
Bill Wendlingd06b4202008-11-26 22:37:40 +00006485 SDNode *N = Op.getNode();
Bill Wendlingd3511522008-12-02 01:06:39 +00006486 SDValue LHS = N->getOperand(0);
6487 SDValue RHS = N->getOperand(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00006488 unsigned BaseOp = 0;
6489 unsigned Cond = 0;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006490 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling7e04be62008-12-09 22:08:41 +00006491
6492 switch (Op.getOpcode()) {
6493 default: assert(0 && "Unknown ovf instruction!");
6494 case ISD::SADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006495 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00006496 Cond = X86::COND_O;
6497 break;
6498 case ISD::UADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006499 BaseOp = X86ISD::ADD;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006500 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006501 break;
6502 case ISD::SSUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006503 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00006504 Cond = X86::COND_O;
6505 break;
6506 case ISD::USUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006507 BaseOp = X86ISD::SUB;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006508 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006509 break;
6510 case ISD::SMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006511 BaseOp = X86ISD::SMUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00006512 Cond = X86::COND_O;
6513 break;
6514 case ISD::UMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006515 BaseOp = X86ISD::UMUL;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006516 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006517 break;
6518 }
Bill Wendlingd06b4202008-11-26 22:37:40 +00006519
Bill Wendlingd3511522008-12-02 01:06:39 +00006520 // Also sets EFLAGS.
6521 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006522 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendlingd06b4202008-11-26 22:37:40 +00006523
Bill Wendlingd3511522008-12-02 01:06:39 +00006524 SDValue SetCC =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006525 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendling35f1a9d2008-12-10 02:01:32 +00006526 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendlingd06b4202008-11-26 22:37:40 +00006527
Bill Wendlingd3511522008-12-02 01:06:39 +00006528 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6529 return Sum;
Bill Wendling4c134df2008-11-24 19:21:46 +00006530}
6531
Dan Gohman8181bd12008-07-27 21:46:04 +00006532SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006533 MVT T = Op.getValueType();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006534 DebugLoc dl = Op.getNode()->getDebugLoc();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00006535 unsigned Reg = 0;
6536 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006537 switch(T.getSimpleVT()) {
6538 default:
6539 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006540 case MVT::i8: Reg = X86::AL; size = 1; break;
6541 case MVT::i16: Reg = X86::AX; size = 2; break;
6542 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00006543 case MVT::i64:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006544 assert(Subtarget->is64Bit() && "Node not type legal!");
6545 Reg = X86::RAX; size = 8;
Andrew Lenharth81580822008-03-05 01:15:49 +00006546 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00006547 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006548 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00006549 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00006550 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006551 Op.getOperand(1),
6552 Op.getOperand(3),
6553 DAG.getTargetConstant(size, MVT::i8),
6554 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006555 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006556 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Dan Gohman8181bd12008-07-27 21:46:04 +00006557 SDValue cpOut =
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006558 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006559 return cpOut;
6560}
6561
Duncan Sands7d9834b2008-12-01 11:39:25 +00006562SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif825aa892008-08-28 23:19:51 +00006563 SelectionDAG &DAG) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006564 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharth81580822008-03-05 01:15:49 +00006565 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006566 SDValue TheChain = Op.getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006567 DebugLoc dl = Op.getNode()->getDebugLoc();
6568 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006569 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6570 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006571 rax.getValue(2));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006572 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006573 DAG.getConstant(32, MVT::i8));
6574 SDValue Ops[] = {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006575 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006576 rdx.getValue(1)
6577 };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006578 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesenf160d802008-10-02 18:53:47 +00006579}
6580
Dale Johannesen9011d872008-09-29 22:25:26 +00006581SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6582 SDNode *Node = Op.getNode();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006583 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen9011d872008-09-29 22:25:26 +00006584 MVT T = Node->getValueType(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006585 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006586 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006587 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006588 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen9011d872008-09-29 22:25:26 +00006589 Node->getOperand(0),
6590 Node->getOperand(1), negOp,
6591 cast<AtomicSDNode>(Node)->getSrcValue(),
6592 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006593}
6594
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006595/// LowerOperation - Provide custom lowering hooks for some operations.
6596///
Dan Gohman8181bd12008-07-27 21:46:04 +00006597SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006598 switch (Op.getOpcode()) {
6599 default: assert(0 && "Should not custom lower this!");
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006600 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6601 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006602 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6603 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6604 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6605 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6606 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6607 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6608 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6609 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006610 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006611 case ISD::SHL_PARTS:
6612 case ISD::SRA_PARTS:
6613 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6614 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00006615 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006616 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6617 case ISD::FABS: return LowerFABS(Op, DAG);
6618 case ISD::FNEG: return LowerFNEG(Op, DAG);
6619 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006620 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006621 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006622 case ISD::SELECT: return LowerSELECT(Op, DAG);
6623 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006624 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6625 case ISD::CALL: return LowerCALL(Op, DAG);
6626 case ISD::RET: return LowerRET(Op, DAG);
6627 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006628 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006629 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006630 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6631 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6632 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6633 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6634 case ISD::FRAME_TO_ARGS_OFFSET:
6635 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6636 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6637 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006638 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006639 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006640 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6641 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wang14edb092008-12-18 21:42:19 +00006642 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling7e04be62008-12-09 22:08:41 +00006643 case ISD::SADDO:
6644 case ISD::UADDO:
6645 case ISD::SSUBO:
6646 case ISD::USUBO:
6647 case ISD::SMULO:
6648 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006649 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006650 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006651}
6652
Duncan Sands7d9834b2008-12-01 11:39:25 +00006653void X86TargetLowering::
6654ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6655 SelectionDAG &DAG, unsigned NewOp) {
6656 MVT T = Node->getValueType(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006657 DebugLoc dl = Node->getDebugLoc();
Duncan Sands7d9834b2008-12-01 11:39:25 +00006658 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6659
6660 SDValue Chain = Node->getOperand(0);
6661 SDValue In1 = Node->getOperand(1);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006662 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006663 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006664 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006665 Node->getOperand(2), DAG.getIntPtrConstant(1));
6666 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6667 // have a MemOperand. Pass the info through as a normal operand.
6668 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6669 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6670 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006671 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006672 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006673 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006674 Results.push_back(Result.getValue(2));
6675}
6676
Duncan Sandsac496a12008-07-04 11:47:58 +00006677/// ReplaceNodeResults - Replace a node with an illegal result type
6678/// with a new node built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00006679void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6680 SmallVectorImpl<SDValue>&Results,
6681 SelectionDAG &DAG) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006682 DebugLoc dl = N->getDebugLoc();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006683 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006684 default:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006685 assert(false && "Do not know how to custom type legalize this operation!");
6686 return;
6687 case ISD::FP_TO_SINT: {
6688 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
6689 SDValue FIST = Vals.first, StackSlot = Vals.second;
6690 if (FIST.getNode() != 0) {
6691 MVT VT = N->getValueType(0);
6692 // Return a load from the stack slot.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006693 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006694 }
6695 return;
6696 }
6697 case ISD::READCYCLECOUNTER: {
6698 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6699 SDValue TheChain = N->getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006700 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006701 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
6702 rd.getValue(1));
6703 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006704 eax.getValue(2));
6705 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6706 SDValue Ops[] = { eax, edx };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006707 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006708 Results.push_back(edx.getValue(1));
6709 return;
6710 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006711 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006712 MVT T = N->getValueType(0);
6713 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6714 SDValue cpInL, cpInH;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006715 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006716 DAG.getConstant(0, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006717 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006718 DAG.getConstant(1, MVT::i32));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006719 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6720 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006721 cpInL.getValue(1));
6722 SDValue swapInL, swapInH;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006723 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006724 DAG.getConstant(0, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006725 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands7d9834b2008-12-01 11:39:25 +00006726 DAG.getConstant(1, MVT::i32));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006727 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006728 cpInH.getValue(1));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006729 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands7d9834b2008-12-01 11:39:25 +00006730 swapInL.getValue(1));
6731 SDValue Ops[] = { swapInH.getValue(0),
6732 N->getOperand(1),
6733 swapInH.getValue(1) };
6734 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006735 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006736 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6737 MVT::i32, Result.getValue(1));
6738 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6739 MVT::i32, cpOutL.getValue(2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006740 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006741 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00006742 Results.push_back(cpOutH.getValue(1));
6743 return;
6744 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006745 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006746 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6747 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006748 case ISD::ATOMIC_LOAD_AND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006749 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6750 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006751 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006752 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6753 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006754 case ISD::ATOMIC_LOAD_OR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006755 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6756 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006757 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006758 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6759 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006760 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006761 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6762 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006763 case ISD::ATOMIC_SWAP:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006764 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6765 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006766 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006767}
6768
6769const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6770 switch (Opcode) {
6771 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006772 case X86ISD::BSF: return "X86ISD::BSF";
6773 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006774 case X86ISD::SHLD: return "X86ISD::SHLD";
6775 case X86ISD::SHRD: return "X86ISD::SHRD";
6776 case X86ISD::FAND: return "X86ISD::FAND";
6777 case X86ISD::FOR: return "X86ISD::FOR";
6778 case X86ISD::FXOR: return "X86ISD::FXOR";
6779 case X86ISD::FSRL: return "X86ISD::FSRL";
6780 case X86ISD::FILD: return "X86ISD::FILD";
6781 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6782 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6783 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6784 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6785 case X86ISD::FLD: return "X86ISD::FLD";
6786 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006787 case X86ISD::CALL: return "X86ISD::CALL";
6788 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6789 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohman7fe9b7f2008-12-23 22:45:23 +00006790 case X86ISD::BT: return "X86ISD::BT";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006791 case X86ISD::CMP: return "X86ISD::CMP";
6792 case X86ISD::COMI: return "X86ISD::COMI";
6793 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6794 case X86ISD::SETCC: return "X86ISD::SETCC";
6795 case X86ISD::CMOV: return "X86ISD::CMOV";
6796 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6797 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6798 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6799 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006800 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6801 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006802 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006803 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006804 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6805 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006806 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6807 case X86ISD::FMAX: return "X86ISD::FMAX";
6808 case X86ISD::FMIN: return "X86ISD::FMIN";
6809 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6810 case X86ISD::FRCP: return "X86ISD::FRCP";
6811 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6812 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6813 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006814 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006815 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006816 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6817 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006818 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6819 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6820 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6821 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6822 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6823 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006824 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6825 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006826 case X86ISD::VSHL: return "X86ISD::VSHL";
6827 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006828 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6829 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6830 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6831 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6832 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6833 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6834 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6835 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6836 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6837 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingae034ed2008-12-12 00:56:36 +00006838 case X86ISD::ADD: return "X86ISD::ADD";
6839 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingf5399032008-12-12 21:15:41 +00006840 case X86ISD::SMUL: return "X86ISD::SMUL";
6841 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006842 }
6843}
6844
6845// isLegalAddressingMode - Return true if the addressing mode represented
6846// by AM is legal for this target, for a load/store of the specified type.
6847bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6848 const Type *Ty) const {
6849 // X86 supports extremely general addressing modes.
6850
6851 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6852 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6853 return false;
6854
6855 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006856 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006857 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6858 return false;
Dale Johannesen64660e92008-12-05 21:47:27 +00006859 // If BaseGV requires a register, we cannot also have a BaseReg.
6860 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6861 AM.HasBaseReg)
6862 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006863
6864 // X86-64 only supports addr of globals in small code model.
6865 if (Subtarget->is64Bit()) {
6866 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6867 return false;
6868 // If lower 4G is not available, then we must use rip-relative addressing.
6869 if (AM.BaseOffs || AM.Scale > 1)
6870 return false;
6871 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006872 }
6873
6874 switch (AM.Scale) {
6875 case 0:
6876 case 1:
6877 case 2:
6878 case 4:
6879 case 8:
6880 // These scales always work.
6881 break;
6882 case 3:
6883 case 5:
6884 case 9:
6885 // These scales are formed with basereg+scalereg. Only accept if there is
6886 // no basereg yet.
6887 if (AM.HasBaseReg)
6888 return false;
6889 break;
6890 default: // Other stuff never works.
6891 return false;
6892 }
6893
6894 return true;
6895}
6896
6897
Evan Cheng27a820a2007-10-26 01:56:11 +00006898bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6899 if (!Ty1->isInteger() || !Ty2->isInteger())
6900 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006901 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6902 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006903 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006904 return false;
6905 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006906}
6907
Duncan Sands92c43912008-06-06 12:08:01 +00006908bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6909 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006910 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006911 unsigned NumBits1 = VT1.getSizeInBits();
6912 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006913 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006914 return false;
6915 return Subtarget->is64Bit() || NumBits1 < 64;
6916}
Evan Cheng27a820a2007-10-26 01:56:11 +00006917
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006918/// isShuffleMaskLegal - Targets can use this to indicate that they only
6919/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6920/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6921/// are assumed to be legal.
6922bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006923X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006924 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006925 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006926 return (Mask.getNode()->getNumOperands() <= 4 ||
6927 isIdentityMask(Mask.getNode()) ||
6928 isIdentityMask(Mask.getNode(), true) ||
6929 isSplatMask(Mask.getNode()) ||
6930 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6931 X86::isUNPCKLMask(Mask.getNode()) ||
6932 X86::isUNPCKHMask(Mask.getNode()) ||
6933 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6934 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006935}
6936
Dan Gohman48d5f062008-04-09 20:09:42 +00006937bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006938X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006939 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006940 unsigned NumElts = BVOps.size();
6941 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006942 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006943 if (NumElts == 2) return true;
6944 if (NumElts == 4) {
6945 return (isMOVLMask(&BVOps[0], 4) ||
6946 isCommutedMOVL(&BVOps[0], 4, true) ||
6947 isSHUFPMask(&BVOps[0], 4) ||
6948 isCommutedSHUFP(&BVOps[0], 4));
6949 }
6950 return false;
6951}
6952
6953//===----------------------------------------------------------------------===//
6954// X86 Scheduler Hooks
6955//===----------------------------------------------------------------------===//
6956
Mon P Wang078a62d2008-05-05 19:05:59 +00006957// private utility function
6958MachineBasicBlock *
6959X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6960 MachineBasicBlock *MBB,
6961 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006962 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006963 unsigned LoadOpc,
6964 unsigned CXchgOpc,
6965 unsigned copyOpc,
6966 unsigned notOpc,
6967 unsigned EAXreg,
6968 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006969 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006970 // For the atomic bitwise operator, we generate
6971 // thisMBB:
6972 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006973 // ld t1 = [bitinstr.addr]
6974 // op t2 = t1, [bitinstr.val]
6975 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006976 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6977 // bz newMBB
6978 // fallthrough -->nextMBB
6979 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6980 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006981 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006982 ++MBBIter;
6983
6984 /// First build the CFG
6985 MachineFunction *F = MBB->getParent();
6986 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006987 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6988 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6989 F->insert(MBBIter, newMBB);
6990 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006991
6992 // Move all successors to thisMBB to nextMBB
6993 nextMBB->transferSuccessors(thisMBB);
6994
6995 // Update thisMBB to fall through to newMBB
6996 thisMBB->addSuccessor(newMBB);
6997
6998 // newMBB jumps to itself and fall through to nextMBB
6999 newMBB->addSuccessor(nextMBB);
7000 newMBB->addSuccessor(newMBB);
7001
7002 // Insert instructions into newMBB based on incoming instruction
7003 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007004 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang078a62d2008-05-05 19:05:59 +00007005 MachineOperand& destOper = bInstr->getOperand(0);
7006 MachineOperand* argOpers[6];
7007 int numArgs = bInstr->getNumOperands() - 1;
7008 for (int i=0; i < numArgs; ++i)
7009 argOpers[i] = &bInstr->getOperand(i+1);
7010
7011 // x86 address has 4 operands: base, index, scale, and displacement
7012 int lastAddrIndx = 3; // [0,3]
7013 int valArgIndx = 4;
7014
Dale Johannesend20e4452008-08-19 18:47:28 +00007015 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007016 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00007017 for (int i=0; i <= lastAddrIndx; ++i)
7018 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007019
Dale Johannesend20e4452008-08-19 18:47:28 +00007020 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007021 if (invSrc) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007022 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007023 }
7024 else
7025 tt = t1;
7026
Dale Johannesend20e4452008-08-19 18:47:28 +00007027 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007028 assert((argOpers[valArgIndx]->isReg() ||
7029 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00007030 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007031 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007032 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00007033 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007034 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007035 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00007036 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007037
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007038 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00007039 MIB.addReg(t1);
7040
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007041 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00007042 for (int i=0; i <= lastAddrIndx; ++i)
7043 (*MIB).addOperand(*argOpers[i]);
7044 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00007045 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7046 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7047
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007048 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesend20e4452008-08-19 18:47:28 +00007049 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00007050
7051 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007052 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007053
Dan Gohman221a4372008-07-07 23:14:23 +00007054 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00007055 return nextMBB;
7056}
7057
Dale Johannesen44eb5372008-10-03 19:41:08 +00007058// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00007059MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00007060X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7061 MachineBasicBlock *MBB,
7062 unsigned regOpcL,
7063 unsigned regOpcH,
7064 unsigned immOpcL,
7065 unsigned immOpcH,
7066 bool invSrc) {
7067 // For the atomic bitwise operator, we generate
7068 // thisMBB (instructions are in pairs, except cmpxchg8b)
7069 // ld t1,t2 = [bitinstr.addr]
7070 // newMBB:
7071 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7072 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007073 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00007074 // mov ECX, EBX <- t5, t6
7075 // mov EAX, EDX <- t1, t2
7076 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7077 // mov t3, t4 <- EAX, EDX
7078 // bz newMBB
7079 // result in out1, out2
7080 // fallthrough -->nextMBB
7081
7082 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7083 const unsigned LoadOpc = X86::MOV32rm;
7084 const unsigned copyOpc = X86::MOV32rr;
7085 const unsigned NotOpc = X86::NOT32r;
7086 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7087 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7088 MachineFunction::iterator MBBIter = MBB;
7089 ++MBBIter;
7090
7091 /// First build the CFG
7092 MachineFunction *F = MBB->getParent();
7093 MachineBasicBlock *thisMBB = MBB;
7094 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7095 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7096 F->insert(MBBIter, newMBB);
7097 F->insert(MBBIter, nextMBB);
7098
7099 // Move all successors to thisMBB to nextMBB
7100 nextMBB->transferSuccessors(thisMBB);
7101
7102 // Update thisMBB to fall through to newMBB
7103 thisMBB->addSuccessor(newMBB);
7104
7105 // newMBB jumps to itself and fall through to nextMBB
7106 newMBB->addSuccessor(nextMBB);
7107 newMBB->addSuccessor(newMBB);
7108
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007109 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesenf160d802008-10-02 18:53:47 +00007110 // Insert instructions into newMBB based on incoming instruction
7111 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
7112 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
7113 MachineOperand& dest1Oper = bInstr->getOperand(0);
7114 MachineOperand& dest2Oper = bInstr->getOperand(1);
7115 MachineOperand* argOpers[6];
7116 for (int i=0; i < 6; ++i)
7117 argOpers[i] = &bInstr->getOperand(i+2);
7118
7119 // x86 address has 4 operands: base, index, scale, and displacement
7120 int lastAddrIndx = 3; // [0,3]
7121
7122 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007123 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesenf160d802008-10-02 18:53:47 +00007124 for (int i=0; i <= lastAddrIndx; ++i)
7125 (*MIB).addOperand(*argOpers[i]);
7126 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007127 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007128 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00007129 for (int i=0; i <= lastAddrIndx-1; ++i)
7130 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007131 MachineOperand newOp3 = *(argOpers[3]);
7132 if (newOp3.isImm())
7133 newOp3.setImm(newOp3.getImm()+4);
7134 else
7135 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00007136 (*MIB).addOperand(newOp3);
7137
7138 // t3/4 are defined later, at the bottom of the loop
7139 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7140 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007141 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007142 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007143 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007144 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7145
7146 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7147 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
7148 if (invSrc) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007149 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7150 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesenf160d802008-10-02 18:53:47 +00007151 } else {
7152 tt1 = t1;
7153 tt2 = t2;
7154 }
7155
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007156 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00007157 "invalid operand");
7158 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7159 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007160 if (argOpers[4]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007161 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesenf160d802008-10-02 18:53:47 +00007162 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007163 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007164 if (regOpcL != X86::MOV32rr)
7165 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00007166 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007167 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
7168 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
7169 if (argOpers[5]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007170 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesenf160d802008-10-02 18:53:47 +00007171 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007172 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007173 if (regOpcH != X86::MOV32rr)
7174 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00007175 (*MIB).addOperand(*argOpers[5]);
7176
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007177 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007178 MIB.addReg(t1);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007179 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007180 MIB.addReg(t2);
7181
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007182 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007183 MIB.addReg(t5);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007184 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007185 MIB.addReg(t6);
7186
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007187 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesenf160d802008-10-02 18:53:47 +00007188 for (int i=0; i <= lastAddrIndx; ++i)
7189 (*MIB).addOperand(*argOpers[i]);
7190
7191 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7192 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7193
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007194 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesenf160d802008-10-02 18:53:47 +00007195 MIB.addReg(X86::EAX);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007196 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesenf160d802008-10-02 18:53:47 +00007197 MIB.addReg(X86::EDX);
7198
7199 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007200 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesenf160d802008-10-02 18:53:47 +00007201
7202 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7203 return nextMBB;
7204}
7205
7206// private utility function
7207MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00007208X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7209 MachineBasicBlock *MBB,
7210 unsigned cmovOpc) {
7211 // For the atomic min/max operator, we generate
7212 // thisMBB:
7213 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00007214 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00007215 // mov t2 = [min/max.val]
7216 // cmp t1, t2
7217 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00007218 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00007219 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7220 // bz newMBB
7221 // fallthrough -->nextMBB
7222 //
7223 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7224 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007225 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00007226 ++MBBIter;
7227
7228 /// First build the CFG
7229 MachineFunction *F = MBB->getParent();
7230 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00007231 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7232 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7233 F->insert(MBBIter, newMBB);
7234 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007235
7236 // Move all successors to thisMBB to nextMBB
7237 nextMBB->transferSuccessors(thisMBB);
7238
7239 // Update thisMBB to fall through to newMBB
7240 thisMBB->addSuccessor(newMBB);
7241
7242 // newMBB jumps to newMBB and fall through to nextMBB
7243 newMBB->addSuccessor(nextMBB);
7244 newMBB->addSuccessor(newMBB);
7245
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007246 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang078a62d2008-05-05 19:05:59 +00007247 // Insert instructions into newMBB based on incoming instruction
7248 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
7249 MachineOperand& destOper = mInstr->getOperand(0);
7250 MachineOperand* argOpers[6];
7251 int numArgs = mInstr->getNumOperands() - 1;
7252 for (int i=0; i < numArgs; ++i)
7253 argOpers[i] = &mInstr->getOperand(i+1);
7254
7255 // x86 address has 4 operands: base, index, scale, and displacement
7256 int lastAddrIndx = 3; // [0,3]
7257 int valArgIndx = 4;
7258
Mon P Wang318b0372008-05-05 22:56:23 +00007259 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007260 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00007261 for (int i=0; i <= lastAddrIndx; ++i)
7262 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00007263
Mon P Wang078a62d2008-05-05 19:05:59 +00007264 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007265 assert((argOpers[valArgIndx]->isReg() ||
7266 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00007267 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00007268
7269 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007270 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007271 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00007272 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007273 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00007274 (*MIB).addOperand(*argOpers[valArgIndx]);
7275
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007276 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wang318b0372008-05-05 22:56:23 +00007277 MIB.addReg(t1);
7278
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007279 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang078a62d2008-05-05 19:05:59 +00007280 MIB.addReg(t1);
7281 MIB.addReg(t2);
7282
7283 // Generate movc
7284 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007285 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang078a62d2008-05-05 19:05:59 +00007286 MIB.addReg(t2);
7287 MIB.addReg(t1);
7288
7289 // Cmp and exchange if none has modified the memory location
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007290 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang078a62d2008-05-05 19:05:59 +00007291 for (int i=0; i <= lastAddrIndx; ++i)
7292 (*MIB).addOperand(*argOpers[i]);
7293 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00007294 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7295 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00007296
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007297 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang078a62d2008-05-05 19:05:59 +00007298 MIB.addReg(X86::EAX);
7299
7300 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007301 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007302
Dan Gohman221a4372008-07-07 23:14:23 +00007303 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00007304 return nextMBB;
7305}
7306
7307
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007308MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00007309X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
7310 MachineBasicBlock *BB) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007311 DebugLoc dl = MI->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007312 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7313 switch (MI->getOpcode()) {
7314 default: assert(false && "Unexpected instr type to insert");
Mon P Wang83edba52008-12-12 01:25:51 +00007315 case X86::CMOV_V1I64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007316 case X86::CMOV_FR32:
7317 case X86::CMOV_FR64:
7318 case X86::CMOV_V4F32:
7319 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00007320 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007321 // To "insert" a SELECT_CC instruction, we actually have to insert the
7322 // diamond control-flow pattern. The incoming instruction knows the
7323 // destination vreg to set, the condition code register to branch on, the
7324 // true/false values to select between, and a branch opcode to use.
7325 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007326 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007327 ++It;
7328
7329 // thisMBB:
7330 // ...
7331 // TrueVal = ...
7332 // cmpTY ccX, r1, r2
7333 // bCC copy1MBB
7334 // fallthrough --> copy0MBB
7335 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00007336 MachineFunction *F = BB->getParent();
7337 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7338 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007339 unsigned Opc =
7340 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007341 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00007342 F->insert(It, copy0MBB);
7343 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007344 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007345 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00007346 sinkMBB->transferSuccessors(BB);
7347
7348 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007349 BB->addSuccessor(copy0MBB);
7350 BB->addSuccessor(sinkMBB);
7351
7352 // copy0MBB:
7353 // %FalseValue = ...
7354 // # fallthrough to sinkMBB
7355 BB = copy0MBB;
7356
7357 // Update machine-CFG edges
7358 BB->addSuccessor(sinkMBB);
7359
7360 // sinkMBB:
7361 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7362 // ...
7363 BB = sinkMBB;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007364 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007365 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7366 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7367
Dan Gohman221a4372008-07-07 23:14:23 +00007368 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007369 return BB;
7370 }
7371
7372 case X86::FP32_TO_INT16_IN_MEM:
7373 case X86::FP32_TO_INT32_IN_MEM:
7374 case X86::FP32_TO_INT64_IN_MEM:
7375 case X86::FP64_TO_INT16_IN_MEM:
7376 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007377 case X86::FP64_TO_INT64_IN_MEM:
7378 case X86::FP80_TO_INT16_IN_MEM:
7379 case X86::FP80_TO_INT32_IN_MEM:
7380 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007381 // Change the floating point control register to use "round towards zero"
7382 // mode when truncating to an integer value.
7383 MachineFunction *F = BB->getParent();
7384 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007385 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007386
7387 // Load the old value of the high byte of the control word...
7388 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00007389 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007390 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
7391 CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007392
7393 // Set the high part to be round to zero...
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007394 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007395 .addImm(0xC7F);
7396
7397 // Reload the modified control word now...
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007398 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007399
7400 // Restore the memory image of control word to original value
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007401 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007402 .addReg(OldCW);
7403
7404 // Get the X86 opcode to use.
7405 unsigned Opc;
7406 switch (MI->getOpcode()) {
7407 default: assert(0 && "illegal opcode!");
7408 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7409 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7410 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7411 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7412 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7413 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007414 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7415 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7416 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007417 }
7418
7419 X86AddressMode AM;
7420 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007421 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007422 AM.BaseType = X86AddressMode::RegBase;
7423 AM.Base.Reg = Op.getReg();
7424 } else {
7425 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00007426 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007427 }
7428 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007429 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007430 AM.Scale = Op.getImm();
7431 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007432 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007433 AM.IndexReg = Op.getImm();
7434 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007435 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007436 AM.GV = Op.getGlobal();
7437 } else {
7438 AM.Disp = Op.getImm();
7439 }
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007440 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007441 .addReg(MI->getOperand(4).getReg());
7442
7443 // Reload the original control word now.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007444 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007445
Dan Gohman221a4372008-07-07 23:14:23 +00007446 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007447 return BB;
7448 }
Mon P Wang078a62d2008-05-05 19:05:59 +00007449 case X86::ATOMAND32:
7450 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007451 X86::AND32ri, X86::MOV32rm,
7452 X86::LCMPXCHG32, X86::MOV32rr,
7453 X86::NOT32r, X86::EAX,
7454 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007455 case X86::ATOMOR32:
7456 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007457 X86::OR32ri, X86::MOV32rm,
7458 X86::LCMPXCHG32, X86::MOV32rr,
7459 X86::NOT32r, X86::EAX,
7460 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007461 case X86::ATOMXOR32:
7462 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007463 X86::XOR32ri, X86::MOV32rm,
7464 X86::LCMPXCHG32, X86::MOV32rr,
7465 X86::NOT32r, X86::EAX,
7466 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007467 case X86::ATOMNAND32:
7468 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007469 X86::AND32ri, X86::MOV32rm,
7470 X86::LCMPXCHG32, X86::MOV32rr,
7471 X86::NOT32r, X86::EAX,
7472 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00007473 case X86::ATOMMIN32:
7474 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7475 case X86::ATOMMAX32:
7476 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7477 case X86::ATOMUMIN32:
7478 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7479 case X86::ATOMUMAX32:
7480 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00007481
7482 case X86::ATOMAND16:
7483 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7484 X86::AND16ri, X86::MOV16rm,
7485 X86::LCMPXCHG16, X86::MOV16rr,
7486 X86::NOT16r, X86::AX,
7487 X86::GR16RegisterClass);
7488 case X86::ATOMOR16:
7489 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7490 X86::OR16ri, X86::MOV16rm,
7491 X86::LCMPXCHG16, X86::MOV16rr,
7492 X86::NOT16r, X86::AX,
7493 X86::GR16RegisterClass);
7494 case X86::ATOMXOR16:
7495 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7496 X86::XOR16ri, X86::MOV16rm,
7497 X86::LCMPXCHG16, X86::MOV16rr,
7498 X86::NOT16r, X86::AX,
7499 X86::GR16RegisterClass);
7500 case X86::ATOMNAND16:
7501 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7502 X86::AND16ri, X86::MOV16rm,
7503 X86::LCMPXCHG16, X86::MOV16rr,
7504 X86::NOT16r, X86::AX,
7505 X86::GR16RegisterClass, true);
7506 case X86::ATOMMIN16:
7507 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7508 case X86::ATOMMAX16:
7509 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7510 case X86::ATOMUMIN16:
7511 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7512 case X86::ATOMUMAX16:
7513 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7514
7515 case X86::ATOMAND8:
7516 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7517 X86::AND8ri, X86::MOV8rm,
7518 X86::LCMPXCHG8, X86::MOV8rr,
7519 X86::NOT8r, X86::AL,
7520 X86::GR8RegisterClass);
7521 case X86::ATOMOR8:
7522 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7523 X86::OR8ri, X86::MOV8rm,
7524 X86::LCMPXCHG8, X86::MOV8rr,
7525 X86::NOT8r, X86::AL,
7526 X86::GR8RegisterClass);
7527 case X86::ATOMXOR8:
7528 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7529 X86::XOR8ri, X86::MOV8rm,
7530 X86::LCMPXCHG8, X86::MOV8rr,
7531 X86::NOT8r, X86::AL,
7532 X86::GR8RegisterClass);
7533 case X86::ATOMNAND8:
7534 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7535 X86::AND8ri, X86::MOV8rm,
7536 X86::LCMPXCHG8, X86::MOV8rr,
7537 X86::NOT8r, X86::AL,
7538 X86::GR8RegisterClass, true);
7539 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00007540 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007541 case X86::ATOMAND64:
7542 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7543 X86::AND64ri32, X86::MOV64rm,
7544 X86::LCMPXCHG64, X86::MOV64rr,
7545 X86::NOT64r, X86::RAX,
7546 X86::GR64RegisterClass);
7547 case X86::ATOMOR64:
7548 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7549 X86::OR64ri32, X86::MOV64rm,
7550 X86::LCMPXCHG64, X86::MOV64rr,
7551 X86::NOT64r, X86::RAX,
7552 X86::GR64RegisterClass);
7553 case X86::ATOMXOR64:
7554 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7555 X86::XOR64ri32, X86::MOV64rm,
7556 X86::LCMPXCHG64, X86::MOV64rr,
7557 X86::NOT64r, X86::RAX,
7558 X86::GR64RegisterClass);
7559 case X86::ATOMNAND64:
7560 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7561 X86::AND64ri32, X86::MOV64rm,
7562 X86::LCMPXCHG64, X86::MOV64rr,
7563 X86::NOT64r, X86::RAX,
7564 X86::GR64RegisterClass, true);
7565 case X86::ATOMMIN64:
7566 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7567 case X86::ATOMMAX64:
7568 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7569 case X86::ATOMUMIN64:
7570 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7571 case X86::ATOMUMAX64:
7572 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00007573
7574 // This group does 64-bit operations on a 32-bit host.
7575 case X86::ATOMAND6432:
7576 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7577 X86::AND32rr, X86::AND32rr,
7578 X86::AND32ri, X86::AND32ri,
7579 false);
7580 case X86::ATOMOR6432:
7581 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7582 X86::OR32rr, X86::OR32rr,
7583 X86::OR32ri, X86::OR32ri,
7584 false);
7585 case X86::ATOMXOR6432:
7586 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7587 X86::XOR32rr, X86::XOR32rr,
7588 X86::XOR32ri, X86::XOR32ri,
7589 false);
7590 case X86::ATOMNAND6432:
7591 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7592 X86::AND32rr, X86::AND32rr,
7593 X86::AND32ri, X86::AND32ri,
7594 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00007595 case X86::ATOMADD6432:
7596 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7597 X86::ADD32rr, X86::ADC32rr,
7598 X86::ADD32ri, X86::ADC32ri,
7599 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00007600 case X86::ATOMSUB6432:
7601 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7602 X86::SUB32rr, X86::SBB32rr,
7603 X86::SUB32ri, X86::SBB32ri,
7604 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007605 case X86::ATOMSWAP6432:
7606 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7607 X86::MOV32rr, X86::MOV32rr,
7608 X86::MOV32ri, X86::MOV32ri,
7609 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007610 }
7611}
7612
7613//===----------------------------------------------------------------------===//
7614// X86 Optimization Hooks
7615//===----------------------------------------------------------------------===//
7616
Dan Gohman8181bd12008-07-27 21:46:04 +00007617void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00007618 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00007619 APInt &KnownZero,
7620 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007621 const SelectionDAG &DAG,
7622 unsigned Depth) const {
7623 unsigned Opc = Op.getOpcode();
7624 assert((Opc >= ISD::BUILTIN_OP_END ||
7625 Opc == ISD::INTRINSIC_WO_CHAIN ||
7626 Opc == ISD::INTRINSIC_W_CHAIN ||
7627 Opc == ISD::INTRINSIC_VOID) &&
7628 "Should use MaskedValueIsZero if you don't know whether Op"
7629 " is a target node!");
7630
Dan Gohman1d79e432008-02-13 23:07:24 +00007631 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007632 switch (Opc) {
7633 default: break;
Evan Cheng8e9b21c2009-02-02 09:15:04 +00007634 case X86ISD::ADD:
7635 case X86ISD::SUB:
7636 case X86ISD::SMUL:
7637 case X86ISD::UMUL:
7638 // These nodes' second result is a boolean.
7639 if (Op.getResNo() == 0)
7640 break;
7641 // Fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007642 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007643 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7644 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007645 break;
7646 }
7647}
7648
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007649/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007650/// node is a GlobalAddress + offset.
7651bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7652 GlobalValue* &GA, int64_t &Offset) const{
7653 if (N->getOpcode() == X86ISD::Wrapper) {
7654 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007655 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00007656 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007657 return true;
7658 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007659 }
Evan Chengef7be082008-05-12 19:56:52 +00007660 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007661}
7662
Evan Chengef7be082008-05-12 19:56:52 +00007663static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7664 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007665 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007666 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007667 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007668 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007669 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007670 return false;
7671}
7672
Dan Gohman8181bd12008-07-27 21:46:04 +00007673static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007674 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007675 SDNode *&Base,
7676 SelectionDAG &DAG, MachineFrameInfo *MFI,
7677 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007678 Base = NULL;
7679 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007680 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007681 if (Idx.getOpcode() == ISD::UNDEF) {
7682 if (!Base)
7683 return false;
7684 continue;
7685 }
7686
Dan Gohman8181bd12008-07-27 21:46:04 +00007687 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007688 if (!Elt.getNode() ||
7689 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007690 return false;
7691 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007692 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007693 if (Base->getOpcode() == ISD::UNDEF)
7694 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007695 continue;
7696 }
7697 if (Elt.getOpcode() == ISD::UNDEF)
7698 continue;
7699
Gabor Greif1c80d112008-08-28 21:40:38 +00007700 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007701 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007702 return false;
7703 }
7704 return true;
7705}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007706
7707/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7708/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7709/// if the load addresses are consecutive, non-overlapping, and in the right
7710/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007711static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007712 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007713 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007714 DebugLoc dl = N->getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00007715 MVT VT = N->getValueType(0);
7716 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007717 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007718 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007719 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007720 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7721 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007722 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007723
Dan Gohman11821702007-07-27 17:16:43 +00007724 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007725 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007726 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7727 LD->getSrcValue(), LD->getSrcValueOffset(),
7728 LD->isVolatile());
7729 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7730 LD->getSrcValue(), LD->getSrcValueOffset(),
7731 LD->isVolatile(), LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007732}
7733
Evan Chengb6290462008-05-12 23:04:07 +00007734/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007735static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohman22cefb02009-01-29 01:59:02 +00007736 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng6617eed2008-09-24 23:26:36 +00007737 const X86Subtarget *Subtarget,
7738 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007739 unsigned NumOps = N->getNumOperands();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007740 DebugLoc dl = N->getDebugLoc();
Evan Chengdea99362008-05-29 08:22:04 +00007741
Evan Chenge9b9c672008-05-09 21:53:03 +00007742 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007743 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007744 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007745
Duncan Sands92c43912008-06-06 12:08:01 +00007746 MVT VT = N->getValueType(0);
7747 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007748 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7749 // We are looking for load i64 and zero extend. We want to transform
7750 // it before legalizer has a chance to expand it. Also look for i64
7751 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007752 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007753 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007754 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007755 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007756 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007757
7758 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007759 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007760 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007761 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007762 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007763 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007764 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007765 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007766 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007767
7768 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007769 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007770
7771 // Load must not be an extload.
7772 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007773 return SDValue();
Mon P Wang28b36412009-01-30 07:07:40 +00007774
7775 // Load type should legal type so we don't have to legalize it.
7776 if (!TLI.isTypeLegal(VT))
7777 return SDValue();
7778
Evan Cheng6617eed2008-09-24 23:26:36 +00007779 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7780 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007781 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Dan Gohman22cefb02009-01-29 01:59:02 +00007782 TargetLowering::TargetLoweringOpt TLO(DAG);
7783 TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1));
7784 DCI.CommitTargetLoweringOpt(TLO);
Evan Cheng6617eed2008-09-24 23:26:36 +00007785 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007786}
7787
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007788/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007789static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007790 const X86Subtarget *Subtarget) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007791 DebugLoc dl = N->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00007792 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007793
7794 // If we have SSE[12] support, try to form min/max nodes.
7795 if (Subtarget->hasSSE2() &&
7796 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7797 if (Cond.getOpcode() == ISD::SETCC) {
7798 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007799 SDValue LHS = N->getOperand(1);
7800 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007801 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7802
7803 unsigned Opcode = 0;
7804 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7805 switch (CC) {
7806 default: break;
7807 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7808 case ISD::SETULE:
7809 case ISD::SETLE:
7810 if (!UnsafeFPMath) break;
7811 // FALL THROUGH.
7812 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7813 case ISD::SETLT:
7814 Opcode = X86ISD::FMIN;
7815 break;
7816
7817 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7818 case ISD::SETUGT:
7819 case ISD::SETGT:
7820 if (!UnsafeFPMath) break;
7821 // FALL THROUGH.
7822 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7823 case ISD::SETGE:
7824 Opcode = X86ISD::FMAX;
7825 break;
7826 }
7827 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7828 switch (CC) {
7829 default: break;
7830 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7831 case ISD::SETUGT:
7832 case ISD::SETGT:
7833 if (!UnsafeFPMath) break;
7834 // FALL THROUGH.
7835 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7836 case ISD::SETGE:
7837 Opcode = X86ISD::FMIN;
7838 break;
7839
7840 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7841 case ISD::SETULE:
7842 case ISD::SETLE:
7843 if (!UnsafeFPMath) break;
7844 // FALL THROUGH.
7845 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7846 case ISD::SETLT:
7847 Opcode = X86ISD::FMAX;
7848 break;
7849 }
7850 }
7851
7852 if (Opcode)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007853 return DAG.getNode(Opcode, dl, N->getValueType(0), LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007854 }
7855
7856 }
7857
Dan Gohman8181bd12008-07-27 21:46:04 +00007858 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007859}
7860
sampo025b75c2009-01-26 00:52:55 +00007861/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
7862/// when possible.
7863static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
7864 const X86Subtarget *Subtarget) {
7865 // On X86 with SSE2 support, we can transform this to a vector shift if
7866 // all elements are shifted by the same amount. We can't do this in legalize
7867 // because the a constant vector is typically transformed to a constant pool
7868 // so we have no knowledge of the shift amount.
sampo087d53c2009-01-26 03:15:31 +00007869 if (!Subtarget->hasSSE2())
7870 return SDValue();
7871
sampo025b75c2009-01-26 00:52:55 +00007872 MVT VT = N->getValueType(0);
sampo087d53c2009-01-26 03:15:31 +00007873 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
7874 return SDValue();
7875
Mon P Wanga91e9642009-01-28 08:12:05 +00007876 SDValue ShAmtOp = N->getOperand(1);
7877 MVT EltVT = VT.getVectorElementType();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007878 DebugLoc dl = N->getDebugLoc();
Mon P Wanga91e9642009-01-28 08:12:05 +00007879 SDValue BaseShAmt;
7880 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
7881 unsigned NumElts = VT.getVectorNumElements();
7882 unsigned i = 0;
7883 for (; i != NumElts; ++i) {
7884 SDValue Arg = ShAmtOp.getOperand(i);
7885 if (Arg.getOpcode() == ISD::UNDEF) continue;
7886 BaseShAmt = Arg;
7887 break;
7888 }
7889 for (; i != NumElts; ++i) {
7890 SDValue Arg = ShAmtOp.getOperand(i);
7891 if (Arg.getOpcode() == ISD::UNDEF) continue;
7892 if (Arg != BaseShAmt) {
7893 return SDValue();
7894 }
7895 }
7896 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
7897 isSplatMask(ShAmtOp.getOperand(2).getNode())) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007898 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, ShAmtOp,
Mon P Wanga91e9642009-01-28 08:12:05 +00007899 DAG.getIntPtrConstant(0));
7900 } else
sampo087d53c2009-01-26 03:15:31 +00007901 return SDValue();
sampo025b75c2009-01-26 00:52:55 +00007902
sampo087d53c2009-01-26 03:15:31 +00007903 if (EltVT.bitsGT(MVT::i32))
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007904 BaseShAmt = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007905 else if (EltVT.bitsLT(MVT::i32))
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007906 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BaseShAmt);
sampo025b75c2009-01-26 00:52:55 +00007907
sampo087d53c2009-01-26 03:15:31 +00007908 // The shift amount is identical so we can do a vector shift.
7909 SDValue ValOp = N->getOperand(0);
7910 switch (N->getOpcode()) {
7911 default:
7912 assert(0 && "Unknown shift opcode!");
7913 break;
7914 case ISD::SHL:
7915 if (VT == MVT::v2i64)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007916 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007917 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7918 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007919 if (VT == MVT::v4i32)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007920 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007921 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
7922 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007923 if (VT == MVT::v8i16)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007924 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007925 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
7926 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007927 break;
7928 case ISD::SRA:
7929 if (VT == MVT::v4i32)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007930 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007931 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
7932 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007933 if (VT == MVT::v8i16)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007934 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007935 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
7936 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007937 break;
7938 case ISD::SRL:
7939 if (VT == MVT::v2i64)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007940 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007941 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7942 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007943 if (VT == MVT::v4i32)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007944 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007945 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
7946 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007947 if (VT == MVT::v8i16)
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007948 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
sampo025b75c2009-01-26 00:52:55 +00007949 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
7950 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007951 break;
sampo025b75c2009-01-26 00:52:55 +00007952 }
7953 return SDValue();
7954}
7955
Chris Lattnerce84ae42008-02-22 02:09:43 +00007956/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007957static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007958 const X86Subtarget *Subtarget) {
7959 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7960 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007961 // A preferable solution to the general problem is to figure out the right
7962 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007963 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007964 if (St->getValue().getValueType().isVector() &&
7965 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007966 isa<LoadSDNode>(St->getValue()) &&
7967 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7968 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007969 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007970 LoadSDNode *Ld = 0;
7971 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007972 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007973 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007974 // Must be a store of a load. We currently handle two cases: the load
7975 // is a direct child, and it's under an intervening TokenFactor. It is
7976 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007977 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007978 Ld = cast<LoadSDNode>(St->getChain());
7979 else if (St->getValue().hasOneUse() &&
7980 ChainVal->getOpcode() == ISD::TokenFactor) {
7981 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007982 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007983 TokenFactorIndex = i;
7984 Ld = cast<LoadSDNode>(St->getValue());
7985 } else
7986 Ops.push_back(ChainVal->getOperand(i));
7987 }
7988 }
7989 if (Ld) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007990 DebugLoc dl = N->getDebugLoc();
Dale Johannesend112b802008-02-25 19:20:14 +00007991 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7992 if (Subtarget->is64Bit()) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007993 SDValue NewLd = DAG.getLoad(MVT::i64, dl, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007994 Ld->getBasePtr(), Ld->getSrcValue(),
7995 Ld->getSrcValueOffset(), Ld->isVolatile(),
7996 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007997 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007998 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00007999 Ops.push_back(NewChain);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008000 NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Ops[0],
Dale Johannesend112b802008-02-25 19:20:14 +00008001 Ops.size());
8002 }
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008003 return DAG.getStore(NewChain, dl, NewLd, St->getBasePtr(),
Dale Johannesend112b802008-02-25 19:20:14 +00008004 St->getSrcValue(), St->getSrcValueOffset(),
8005 St->isVolatile(), St->getAlignment());
8006 }
8007
8008 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00008009 SDValue LoAddr = Ld->getBasePtr();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008010 SDValue HiAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00008011 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00008012
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008013 SDValue LoLd = DAG.getLoad(MVT::i32, dl, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00008014 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8015 Ld->isVolatile(), Ld->getAlignment());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008016 SDValue HiLd = DAG.getLoad(MVT::i32, dl, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00008017 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8018 Ld->isVolatile(),
8019 MinAlign(Ld->getAlignment(), 4));
8020
Dan Gohman8181bd12008-07-27 21:46:04 +00008021 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00008022 if (TokenFactorIndex != -1) {
8023 Ops.push_back(LoLd);
8024 Ops.push_back(HiLd);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008025 NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Ops[0],
Dale Johannesend112b802008-02-25 19:20:14 +00008026 Ops.size());
8027 }
8028
8029 LoAddr = St->getBasePtr();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008030 HiAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00008031 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00008032
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008033 SDValue LoSt = DAG.getStore(NewChain, dl, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00008034 St->getSrcValue(), St->getSrcValueOffset(),
8035 St->isVolatile(), St->getAlignment());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008036 SDValue HiSt = DAG.getStore(NewChain, dl, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00008037 St->getSrcValue(),
8038 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00008039 St->isVolatile(),
8040 MinAlign(St->getAlignment(), 4));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008041 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00008042 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00008043 }
Dan Gohman8181bd12008-07-27 21:46:04 +00008044 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00008045}
8046
Chris Lattner470d5dc2008-01-25 06:14:17 +00008047/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8048/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00008049static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00008050 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8051 // F[X]OR(0.0, x) -> x
8052 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00008053 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8054 if (C->getValueAPF().isPosZero())
8055 return N->getOperand(1);
8056 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8057 if (C->getValueAPF().isPosZero())
8058 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00008059 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00008060}
8061
8062/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00008063static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00008064 // FAND(0.0, x) -> 0.0
8065 // FAND(x, 0.0) -> 0.0
8066 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8067 if (C->getValueAPF().isPosZero())
8068 return N->getOperand(0);
8069 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8070 if (C->getValueAPF().isPosZero())
8071 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00008072 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00008073}
8074
Dan Gohman22cefb02009-01-29 01:59:02 +00008075static SDValue PerformBTCombine(SDNode *N,
8076 SelectionDAG &DAG,
8077 TargetLowering::DAGCombinerInfo &DCI) {
8078 // BT ignores high bits in the bit index operand.
8079 SDValue Op1 = N->getOperand(1);
8080 if (Op1.hasOneUse()) {
8081 unsigned BitWidth = Op1.getValueSizeInBits();
8082 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8083 APInt KnownZero, KnownOne;
8084 TargetLowering::TargetLoweringOpt TLO(DAG);
8085 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8086 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8087 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8088 DCI.CommitTargetLoweringOpt(TLO);
8089 }
8090 return SDValue();
8091}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008092
Dan Gohman8181bd12008-07-27 21:46:04 +00008093SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00008094 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008095 SelectionDAG &DAG = DCI.DAG;
8096 switch (N->getOpcode()) {
8097 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00008098 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
8099 case ISD::BUILD_VECTOR:
Dan Gohman22cefb02009-01-29 01:59:02 +00008100 return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00008101 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
sampo025b75c2009-01-26 00:52:55 +00008102 case ISD::SHL:
8103 case ISD::SRA:
8104 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00008105 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00008106 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00008107 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8108 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohman22cefb02009-01-29 01:59:02 +00008109 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008110 }
8111
Dan Gohman8181bd12008-07-27 21:46:04 +00008112 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008113}
8114
8115//===----------------------------------------------------------------------===//
8116// X86 Inline Assembly Support
8117//===----------------------------------------------------------------------===//
8118
8119/// getConstraintType - Given a constraint letter, return the type of
8120/// constraint it is for this target.
8121X86TargetLowering::ConstraintType
8122X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8123 if (Constraint.size() == 1) {
8124 switch (Constraint[0]) {
8125 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +00008126 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +00008127 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008128 case 'r':
8129 case 'R':
8130 case 'l':
8131 case 'q':
8132 case 'Q':
8133 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00008134 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008135 case 'Y':
8136 return C_RegisterClass;
8137 default:
8138 break;
8139 }
8140 }
8141 return TargetLowering::getConstraintType(Constraint);
8142}
8143
Dale Johannesene99fc902008-01-29 02:21:21 +00008144/// LowerXConstraint - try to replace an X constraint, which matches anything,
8145/// with another that has more specific requirements based on the type of the
8146/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00008147const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00008148LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00008149 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8150 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00008151 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00008152 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00008153 return "Y";
8154 if (Subtarget->hasSSE1())
8155 return "x";
8156 }
8157
8158 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00008159}
8160
Chris Lattnera531abc2007-08-25 00:47:38 +00008161/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8162/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00008163void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00008164 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00008165 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00008166 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00008167 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00008168 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00008169
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008170 switch (Constraint) {
8171 default: break;
8172 case 'I':
8173 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008174 if (C->getZExtValue() <= 31) {
8175 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008176 break;
8177 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008178 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008179 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00008180 case 'J':
8181 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8182 if (C->getZExtValue() <= 63) {
8183 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8184 break;
8185 }
8186 }
8187 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008188 case 'N':
8189 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008190 if (C->getZExtValue() <= 255) {
8191 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008192 break;
8193 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008194 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008195 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008196 case 'i': {
8197 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00008198 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008199 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008200 break;
8201 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008202
8203 // If we are in non-pic codegen mode, we allow the address of a global (with
8204 // an optional displacement) to be used with 'i'.
8205 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
8206 int64_t Offset = 0;
8207
8208 // Match either (GA) or (GA+C)
8209 if (GA) {
8210 Offset = GA->getOffset();
8211 } else if (Op.getOpcode() == ISD::ADD) {
8212 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8213 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8214 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008215 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008216 } else {
8217 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8218 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8219 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008220 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008221 else
8222 C = 0, GA = 0;
8223 }
8224 }
8225
8226 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00008227 if (hasMemory)
Dan Gohman36322c72008-10-18 02:06:02 +00008228 Op = LowerGlobalAddress(GA->getGlobal(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00008229 else
8230 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8231 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00008232 Result = Op;
8233 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008234 }
8235
8236 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00008237 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008238 }
8239 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008240
Gabor Greif1c80d112008-08-28 21:40:38 +00008241 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00008242 Ops.push_back(Result);
8243 return;
8244 }
Evan Cheng7f250d62008-09-24 00:05:32 +00008245 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8246 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008247}
8248
8249std::vector<unsigned> X86TargetLowering::
8250getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00008251 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008252 if (Constraint.size() == 1) {
8253 // FIXME: not handling fp-stack yet!
8254 switch (Constraint[0]) { // GCC X86 Constraint Letters
8255 default: break; // Unknown constraint letter
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008256 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8257 case 'Q': // Q_REGS
8258 if (VT == MVT::i32)
8259 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8260 else if (VT == MVT::i16)
8261 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8262 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00008263 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00008264 else if (VT == MVT::i64)
8265 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8266 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008267 }
8268 }
8269
8270 return std::vector<unsigned>();
8271}
8272
8273std::pair<unsigned, const TargetRegisterClass*>
8274X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00008275 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008276 // First, see if this is a constraint that directly corresponds to an LLVM
8277 // register class.
8278 if (Constraint.size() == 1) {
8279 // GCC Constraint Letters
8280 switch (Constraint[0]) {
8281 default: break;
8282 case 'r': // GENERAL_REGS
8283 case 'R': // LEGACY_REGS
8284 case 'l': // INDEX_REGS
Chris Lattnerbbfea052008-10-17 18:15:05 +00008285 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008286 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00008287 if (VT == MVT::i16)
8288 return std::make_pair(0U, X86::GR16RegisterClass);
8289 if (VT == MVT::i32 || !Subtarget->is64Bit())
8290 return std::make_pair(0U, X86::GR32RegisterClass);
8291 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00008292 case 'f': // FP Stack registers.
8293 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8294 // value to the correct fpstack register class.
8295 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8296 return std::make_pair(0U, X86::RFP32RegisterClass);
8297 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8298 return std::make_pair(0U, X86::RFP64RegisterClass);
8299 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008300 case 'y': // MMX_REGS if MMX allowed.
8301 if (!Subtarget->hasMMX()) break;
8302 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008303 case 'Y': // SSE_REGS if SSE2 allowed
8304 if (!Subtarget->hasSSE2()) break;
8305 // FALL THROUGH.
8306 case 'x': // SSE_REGS if SSE1 allowed
8307 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00008308
8309 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008310 default: break;
8311 // Scalar SSE types.
8312 case MVT::f32:
8313 case MVT::i32:
8314 return std::make_pair(0U, X86::FR32RegisterClass);
8315 case MVT::f64:
8316 case MVT::i64:
8317 return std::make_pair(0U, X86::FR64RegisterClass);
8318 // Vector types.
8319 case MVT::v16i8:
8320 case MVT::v8i16:
8321 case MVT::v4i32:
8322 case MVT::v2i64:
8323 case MVT::v4f32:
8324 case MVT::v2f64:
8325 return std::make_pair(0U, X86::VR128RegisterClass);
8326 }
8327 break;
8328 }
8329 }
8330
8331 // Use the default implementation in TargetLowering to convert the register
8332 // constraint into a member of a register class.
8333 std::pair<unsigned, const TargetRegisterClass*> Res;
8334 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8335
8336 // Not found as a standard register?
8337 if (Res.second == 0) {
8338 // GCC calls "st(0)" just plain "st".
8339 if (StringsEqualNoCase("{st}", Constraint)) {
8340 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00008341 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008342 }
Dale Johannesen73920c02008-11-13 21:52:36 +00008343 // 'A' means EAX + EDX.
8344 if (Constraint == "A") {
8345 Res.first = X86::EAX;
8346 Res.second = X86::GRADRegisterClass;
8347 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008348 return Res;
8349 }
8350
8351 // Otherwise, check to see if this is a register class of the wrong value
8352 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8353 // turn into {ax},{dx}.
8354 if (Res.second->hasType(VT))
8355 return Res; // Correct type already, nothing to do.
8356
8357 // All of the single-register GCC register classes map their values onto
8358 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8359 // really want an 8-bit or 32-bit register, map to the appropriate register
8360 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00008361 if (Res.second == X86::GR16RegisterClass) {
8362 if (VT == MVT::i8) {
8363 unsigned DestReg = 0;
8364 switch (Res.first) {
8365 default: break;
8366 case X86::AX: DestReg = X86::AL; break;
8367 case X86::DX: DestReg = X86::DL; break;
8368 case X86::CX: DestReg = X86::CL; break;
8369 case X86::BX: DestReg = X86::BL; break;
8370 }
8371 if (DestReg) {
8372 Res.first = DestReg;
8373 Res.second = Res.second = X86::GR8RegisterClass;
8374 }
8375 } else if (VT == MVT::i32) {
8376 unsigned DestReg = 0;
8377 switch (Res.first) {
8378 default: break;
8379 case X86::AX: DestReg = X86::EAX; break;
8380 case X86::DX: DestReg = X86::EDX; break;
8381 case X86::CX: DestReg = X86::ECX; break;
8382 case X86::BX: DestReg = X86::EBX; break;
8383 case X86::SI: DestReg = X86::ESI; break;
8384 case X86::DI: DestReg = X86::EDI; break;
8385 case X86::BP: DestReg = X86::EBP; break;
8386 case X86::SP: DestReg = X86::ESP; break;
8387 }
8388 if (DestReg) {
8389 Res.first = DestReg;
8390 Res.second = Res.second = X86::GR32RegisterClass;
8391 }
8392 } else if (VT == MVT::i64) {
8393 unsigned DestReg = 0;
8394 switch (Res.first) {
8395 default: break;
8396 case X86::AX: DestReg = X86::RAX; break;
8397 case X86::DX: DestReg = X86::RDX; break;
8398 case X86::CX: DestReg = X86::RCX; break;
8399 case X86::BX: DestReg = X86::RBX; break;
8400 case X86::SI: DestReg = X86::RSI; break;
8401 case X86::DI: DestReg = X86::RDI; break;
8402 case X86::BP: DestReg = X86::RBP; break;
8403 case X86::SP: DestReg = X86::RSP; break;
8404 }
8405 if (DestReg) {
8406 Res.first = DestReg;
8407 Res.second = Res.second = X86::GR64RegisterClass;
8408 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008409 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00008410 } else if (Res.second == X86::FR32RegisterClass ||
8411 Res.second == X86::FR64RegisterClass ||
8412 Res.second == X86::VR128RegisterClass) {
8413 // Handle references to XMM physical registers that got mapped into the
8414 // wrong class. This can happen with constraints like {xmm0} where the
8415 // target independent register mapper will just pick the first match it can
8416 // find, ignoring the required type.
8417 if (VT == MVT::f32)
8418 Res.second = X86::FR32RegisterClass;
8419 else if (VT == MVT::f64)
8420 Res.second = X86::FR64RegisterClass;
8421 else if (X86::VR128RegisterClass->hasType(VT))
8422 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008423 }
8424
8425 return Res;
8426}
Mon P Wang1448aad2008-10-30 08:01:45 +00008427
8428//===----------------------------------------------------------------------===//
8429// X86 Widen vector type
8430//===----------------------------------------------------------------------===//
8431
8432/// getWidenVectorType: given a vector type, returns the type to widen
8433/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8434/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +00008435/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +00008436/// scalarizing vs using the wider vector type.
8437
Dan Gohman0fe66c92009-01-15 17:34:08 +00008438MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang1448aad2008-10-30 08:01:45 +00008439 assert(VT.isVector());
8440 if (isTypeLegal(VT))
8441 return VT;
8442
8443 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8444 // type based on element type. This would speed up our search (though
8445 // it may not be worth it since the size of the list is relatively
8446 // small).
8447 MVT EltVT = VT.getVectorElementType();
8448 unsigned NElts = VT.getVectorNumElements();
8449
8450 // On X86, it make sense to widen any vector wider than 1
8451 if (NElts <= 1)
8452 return MVT::Other;
8453
8454 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
8455 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8456 MVT SVT = (MVT::SimpleValueType)nVT;
8457
8458 if (isTypeLegal(SVT) &&
8459 SVT.getVectorElementType() == EltVT &&
8460 SVT.getVectorNumElements() > NElts)
8461 return SVT;
8462 }
8463 return MVT::Other;
8464}