blob: 19d5aedf693687d87050d80541d1abfd681ec750 [file] [log] [blame]
Arnold Schwaighofera70fe792007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86InstrBuilder.h"
17#include "X86ISelLowering.h"
18#include "X86MachineFunctionInfo.h"
19#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
21#include "llvm/Constants.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/Function.h"
25#include "llvm/Intrinsics.h"
Evan Cheng75184a92007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/ADT/VectorExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/CodeGen/CallingConvLower.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000034#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Support/MathExtras.h"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000037#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Target/TargetOptions.h"
Evan Cheng75184a92007-12-11 01:46:18 +000039#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include "llvm/ADT/StringExtras.h"
Mon P Wang1f292322008-11-23 04:37:22 +000041#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042using namespace llvm;
43
Mon P Wang1f292322008-11-23 04:37:22 +000044static cl::opt<bool>
Mon P Wangba7e48e2008-11-24 02:10:43 +000045DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang1f292322008-11-23 04:37:22 +000046
Evan Cheng2aea0b42008-04-25 19:11:04 +000047// Forward declarations.
Dan Gohman8181bd12008-07-27 21:46:04 +000048static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG);
Evan Cheng2aea0b42008-04-25 19:11:04 +000049
Dan Gohmanb41dfba2008-05-14 01:58:56 +000050X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 : TargetLowering(TM) {
52 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000053 X86ScalarSSEf64 = Subtarget->hasSSE2();
54 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000056
Chris Lattnerdec9cb52008-01-24 08:07:48 +000057 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
59 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000060 TD = getTargetData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061
62 // Set up the TargetLowering object.
63
64 // X86 is weird, it always uses i8 for shift amounts and setcc results.
65 setShiftAmountType(MVT::i8);
Duncan Sands8cf4a822008-11-23 15:47:28 +000066 setBooleanContents(ZeroOrOneBooleanContent);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 setSchedulingPreference(SchedulingForRegPressure);
68 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
69 setStackPointerRegisterToSaveRestore(X86StackPtr);
70
71 if (Subtarget->isTargetDarwin()) {
72 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
73 setUseUnderscoreSetJmp(false);
74 setUseUnderscoreLongJmp(false);
75 } else if (Subtarget->isTargetMingw()) {
76 // MS runtime is weird: it exports _setjmp, but longjmp!
77 setUseUnderscoreSetJmp(true);
78 setUseUnderscoreLongJmp(false);
79 } else {
80 setUseUnderscoreSetJmp(true);
81 setUseUnderscoreLongJmp(true);
82 }
83
84 // Set up the register classes.
85 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
86 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
87 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
88 if (Subtarget->is64Bit())
89 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
90
Evan Cheng08c171a2008-10-14 21:26:46 +000091 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
Chris Lattner3bc08502008-01-17 19:59:44 +000093 // We don't accept any truncstore of integer registers.
94 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
95 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
96 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
97 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
98 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
Evan Cheng71343822008-10-15 02:05:31 +000099 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
100
101 // SETOEQ and SETUNE require checking two conditions.
102 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
103 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
104 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
105 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
106 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
107 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattner3bc08502008-01-17 19:59:44 +0000108
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
110 // operation.
111 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
112 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
113 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
114
115 if (Subtarget->is64Bit()) {
116 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
117 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
118 } else {
Dale Johannesena359b8b2008-10-21 20:50:01 +0000119 if (X86ScalarSSEf64) {
120 // We have an impenetrably clever algorithm for ui64->double only.
121 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling14a30ef2009-01-17 03:56:04 +0000122
123 // We have faster algorithm for ui32->single only.
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) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
918
919 SmallVector<CCValAssign, 16> RVLocs;
920 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
921 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
922 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000923 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000924
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000925 // If this is the first return lowered for this function, add the regs to the
926 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000927 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000928 for (unsigned i = 0; i != RVLocs.size(); ++i)
929 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000930 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000931 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000932 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000933
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000934 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000935 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000936 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000937 SDValue TailCall = Chain;
938 SDValue TargetAddress = TailCall.getOperand(1);
939 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000940 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000941 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000942 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000943 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000944 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
945 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000946 assert(StackAdjustment.getOpcode() == ISD::Constant &&
947 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000948
Dan Gohman8181bd12008-07-27 21:46:04 +0000949 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000950 Operands.push_back(Chain.getOperand(0));
951 Operands.push_back(TargetAddress);
952 Operands.push_back(StackAdjustment);
953 // Copy registers used by the call. Last operand is a flag so it is not
954 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000955 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000956 Operands.push_back(Chain.getOperand(i));
957 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000958 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
959 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000960 }
961
962 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000963 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000964
Dan Gohman8181bd12008-07-27 21:46:04 +0000965 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000966 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
967 // Operand #1 = Bytes To Pop
968 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
969
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000970 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000971 for (unsigned i = 0; i != RVLocs.size(); ++i) {
972 CCValAssign &VA = RVLocs[i];
973 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000974 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000975
Chris Lattnerb56cc342008-03-11 03:23:40 +0000976 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
977 // the RET instruction and handled by the FP Stackifier.
978 if (RVLocs[i].getLocReg() == X86::ST0 ||
979 RVLocs[i].getLocReg() == X86::ST1) {
980 // If this is a copy from an xmm register to ST(0), use an FPExtend to
981 // change the value to the FP stack register class.
982 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
983 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
984 RetOps.push_back(ValToCopy);
985 // Don't emit a copytoreg.
986 continue;
987 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000988
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000989 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000990 Flag = Chain.getValue(1);
991 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000992
993 // The x86-64 ABI for returning structs by value requires that we copy
994 // the sret argument into %rax for the return. We saved the argument into
995 // a virtual register in the entry block, so now we copy the value out
996 // and into %rax.
997 if (Subtarget->is64Bit() &&
998 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
999 MachineFunction &MF = DAG.getMachineFunction();
1000 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1001 unsigned Reg = FuncInfo->getSRetReturnReg();
1002 if (!Reg) {
1003 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1004 FuncInfo->setSRetReturnReg(Reg);
1005 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001006 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001007
1008 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
1009 Flag = Chain.getValue(1);
1010 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001011
Chris Lattnerb56cc342008-03-11 03:23:40 +00001012 RetOps[0] = Chain; // Update chain.
1013
1014 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +00001015 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +00001016 RetOps.push_back(Flag);
1017
1018 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001019}
1020
1021
1022/// LowerCallResult - Lower the result values of an ISD::CALL into the
1023/// appropriate copies out of appropriate physical registers. This assumes that
1024/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1025/// being lowered. The returns a SDNode with the same number of values as the
1026/// ISD::CALL.
1027SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +00001028LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029 unsigned CallingConv, SelectionDAG &DAG) {
1030
1031 // Assign locations to each value returned by this call.
1032 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +00001033 bool isVarArg = TheCall->isVarArg();
Edwin Törökaf8e1332009-02-01 18:15:56 +00001034 bool Is64Bit = Subtarget->is64Bit();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
1036 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1037
Dan Gohman8181bd12008-07-27 21:46:04 +00001038 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039
1040 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001041 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00001042 MVT CopyVT = RVLocs[i].getValVT();
Edwin Törökaf8e1332009-02-01 18:15:56 +00001043
1044 // If this is x86-64, and we disabled SSE, we can't return FP values
1045 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
1046 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
1047 cerr << "SSE register return with SSE disabled\n";
1048 exit(1);
1049 }
1050
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001051 // If this is a call to a function that returns an fp value on the floating
1052 // point stack, but where we prefer to use the value in xmm registers, copy
1053 // 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 +00001054 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1055 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001056 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1057 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001059
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001060 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1061 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001062 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001063 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001064
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001065 if (CopyVT != RVLocs[i].getValVT()) {
1066 // Round the F80 the right size, which also moves to the appropriate xmm
1067 // register.
1068 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1069 // This truncation won't change the value.
1070 DAG.getIntPtrConstant(1));
1071 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001072
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001073 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 }
Duncan Sands698842f2008-07-02 17:40:58 +00001075
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001076 // Merge everything together with a MERGE_VALUES node.
1077 ResultVals.push_back(Chain);
Duncan Sands42d7bb82008-12-01 11:41:29 +00001078 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(), &ResultVals[0],
1079 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001080}
1081
1082
1083//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001084// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001085//===----------------------------------------------------------------------===//
1086// StdCall calling convention seems to be standard for many Windows' API
1087// routines and around. It differs from C calling convention just a little:
1088// callee should clean up the stack, not caller. Symbols should be also
1089// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001090// For info on fast calling convention see Fast Calling Convention (tail call)
1091// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001092
1093/// AddLiveIn - This helper function adds the specified physical register to the
1094/// MachineFunction as a live in value. It also creates a corresponding virtual
1095/// register for it.
1096static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1097 const TargetRegisterClass *RC) {
1098 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001099 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1100 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001101 return VReg;
1102}
1103
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001104/// CallIsStructReturn - Determines whether a CALL node uses struct return
1105/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001106static bool CallIsStructReturn(CallSDNode *TheCall) {
1107 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001108 if (!NumOps)
1109 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001110
Dan Gohman705e3f72008-09-13 01:54:27 +00001111 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001112}
1113
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001114/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1115/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001116static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001117 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001118 if (!NumArgs)
1119 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001120
1121 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001122}
1123
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001124/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1125/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001126/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001127bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001128 if (IsVarArg)
1129 return false;
1130
Dan Gohman705e3f72008-09-13 01:54:27 +00001131 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001132 default:
1133 return false;
1134 case CallingConv::X86_StdCall:
1135 return !Subtarget->is64Bit();
1136 case CallingConv::X86_FastCall:
1137 return !Subtarget->is64Bit();
1138 case CallingConv::Fast:
1139 return PerformTailCallOpt;
1140 }
1141}
1142
Dan Gohman705e3f72008-09-13 01:54:27 +00001143/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1144/// given CallingConvention value.
1145CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001146 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001147 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001148 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001149 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1150 return CC_X86_64_TailCall;
1151 else
1152 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001153 }
1154
Gordon Henriksen18ace102008-01-05 16:56:59 +00001155 if (CC == CallingConv::X86_FastCall)
1156 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001157 else if (CC == CallingConv::Fast)
1158 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001159 else
1160 return CC_X86_32_C;
1161}
1162
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001163/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1164/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001165NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001166X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001167 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001168 if (CC == CallingConv::X86_FastCall)
1169 return FastCall;
1170 else if (CC == CallingConv::X86_StdCall)
1171 return StdCall;
1172 return None;
1173}
1174
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001175
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001176/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1177/// in a register before calling.
1178bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1179 return !IsTailCall && !Is64Bit &&
1180 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1181 Subtarget->isPICStyleGOT();
1182}
1183
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001184/// CallRequiresFnAddressInReg - Check whether the call requires the function
1185/// address to be loaded in a register.
1186bool
1187X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1188 return !Is64Bit && IsTailCall &&
1189 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1190 Subtarget->isPICStyleGOT();
1191}
1192
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001193/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1194/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001195/// the specific parameter attribute. The copy will be passed as a byval
1196/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001197static SDValue
1198CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001199 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001200 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001201 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001202 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001203}
1204
Dan Gohman8181bd12008-07-27 21:46:04 +00001205SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001206 const CCValAssign &VA,
1207 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001208 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001209 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001210 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001211 ISD::ArgFlagsTy Flags =
1212 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001213 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001214 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001215
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001216 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1217 // changed with more analysis.
1218 // In case of tail call optimization mark all arguments mutable. Since they
1219 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001220 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001221 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001222 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001223 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001224 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001225 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001226 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001227}
1228
Dan Gohman8181bd12008-07-27 21:46:04 +00001229SDValue
1230X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001231 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001232 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1233
1234 const Function* Fn = MF.getFunction();
1235 if (Fn->hasExternalLinkage() &&
1236 Subtarget->isTargetCygMing() &&
1237 Fn->getName() == "main")
1238 FuncInfo->setForceFramePointer(true);
1239
1240 // Decorate the function name.
1241 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1242
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001243 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001244 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001245 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001246 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001247 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001248 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001249
1250 assert(!(isVarArg && CC == CallingConv::Fast) &&
1251 "Var args not supported with calling convention fastcc");
1252
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001253 // Assign locations to all of the incoming arguments.
1254 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001255 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001256 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001257
Dan Gohman8181bd12008-07-27 21:46:04 +00001258 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 unsigned LastVal = ~0U;
1260 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1261 CCValAssign &VA = ArgLocs[i];
1262 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1263 // places.
1264 assert(VA.getValNo() != LastVal &&
1265 "Don't support value assigned to multiple locs yet");
1266 LastVal = VA.getValNo();
1267
1268 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001269 MVT RegVT = VA.getLocVT();
Devang Patelf3707e82009-01-05 17:31:22 +00001270 TargetRegisterClass *RC = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001271 if (RegVT == MVT::i32)
1272 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001273 else if (Is64Bit && RegVT == MVT::i64)
1274 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001275 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001276 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001277 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001278 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001279 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001280 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001281 else if (RegVT.isVector()) {
1282 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001283 if (!Is64Bit)
1284 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1285 else {
1286 // Darwin calling convention passes MMX values in either GPRs or
1287 // XMMs in x86-64. Other targets pass them in memory.
1288 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1289 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1290 RegVT = MVT::v2i64;
1291 } else {
1292 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1293 RegVT = MVT::i64;
1294 }
1295 }
1296 } else {
1297 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001299
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001300 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001301 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302
1303 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1304 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1305 // right size.
1306 if (VA.getLocInfo() == CCValAssign::SExt)
1307 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1308 DAG.getValueType(VA.getValVT()));
1309 else if (VA.getLocInfo() == CCValAssign::ZExt)
1310 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1311 DAG.getValueType(VA.getValVT()));
1312
1313 if (VA.getLocInfo() != CCValAssign::Full)
1314 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1315
Gordon Henriksen18ace102008-01-05 16:56:59 +00001316 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001317 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001318 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001319 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1320 else if (RC == X86::VR128RegisterClass) {
1321 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1322 DAG.getConstant(0, MVT::i64));
1323 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1324 }
1325 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001326
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001327 ArgValues.push_back(ArgValue);
1328 } else {
1329 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001330 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001331 }
1332 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001333
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001334 // The x86-64 ABI for returning structs by value requires that we copy
1335 // the sret argument into %rax for the return. Save the argument into
1336 // a virtual register so that we can access it from the return points.
1337 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1338 MachineFunction &MF = DAG.getMachineFunction();
1339 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1340 unsigned Reg = FuncInfo->getSRetReturnReg();
1341 if (!Reg) {
1342 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1343 FuncInfo->setSRetReturnReg(Reg);
1344 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001345 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001346 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1347 }
1348
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001349 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001350 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001351 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001352 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353
1354 // If the function takes variable number of arguments, make a frame index for
1355 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001356 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001357 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1358 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1359 }
1360 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001361 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1362
1363 // FIXME: We should really autogenerate these arrays
1364 static const unsigned GPR64ArgRegsWin64[] = {
1365 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001366 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001367 static const unsigned XMMArgRegsWin64[] = {
1368 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1369 };
1370 static const unsigned GPR64ArgRegs64Bit[] = {
1371 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1372 };
1373 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001374 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1375 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1376 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001377 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1378
1379 if (IsWin64) {
1380 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1381 GPR64ArgRegs = GPR64ArgRegsWin64;
1382 XMMArgRegs = XMMArgRegsWin64;
1383 } else {
1384 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1385 GPR64ArgRegs = GPR64ArgRegs64Bit;
1386 XMMArgRegs = XMMArgRegs64Bit;
1387 }
1388 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1389 TotalNumIntRegs);
1390 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1391 TotalNumXMMRegs);
1392
Edwin Törökaf8e1332009-02-01 18:15:56 +00001393 assert((Subtarget->hasSSE1() || !NumXMMRegs) &&
1394 "SSE register cannot be used when SSE is disabled!");
1395 if (!Subtarget->hasSSE1()) {
1396 // Kernel mode asks for SSE to be disabled, so don't push them
1397 // on the stack.
1398 TotalNumXMMRegs = 0;
1399 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001400 // For X86-64, if there are vararg parameters that are passed via
1401 // registers, then we must store them to their spots on the stack so they
1402 // may be loaded by deferencing the result of va_next.
1403 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001404 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1405 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1406 TotalNumXMMRegs * 16, 16);
1407
Gordon Henriksen18ace102008-01-05 16:56:59 +00001408 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001409 SmallVector<SDValue, 8> MemOps;
1410 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1411 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001412 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001413 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001414 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1415 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001416 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1417 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001418 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001419 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001420 MemOps.push_back(Store);
1421 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001422 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001423 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001424
Gordon Henriksen18ace102008-01-05 16:56:59 +00001425 // Now store the XMM (fp + vector) parameter registers.
1426 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001427 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001428 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001429 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1430 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001431 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1432 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001433 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001434 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001435 MemOps.push_back(Store);
1436 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001437 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001438 }
1439 if (!MemOps.empty())
1440 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1441 &MemOps[0], MemOps.size());
1442 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001443 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001444
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001445 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001446
Gordon Henriksen18ace102008-01-05 16:56:59 +00001447 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001448 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001449 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001450 BytesCallerReserves = 0;
1451 } else {
1452 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001453 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001454 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001455 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001456 BytesCallerReserves = StackSize;
1457 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001458
Gordon Henriksen18ace102008-01-05 16:56:59 +00001459 if (!Is64Bit) {
1460 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1461 if (CC == CallingConv::X86_FastCall)
1462 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1463 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001464
Anton Korobeynikove844e472007-08-15 17:12:32 +00001465 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001466
1467 // Return the new list of results.
Duncan Sands42d7bb82008-12-01 11:41:29 +00001468 return DAG.getNode(ISD::MERGE_VALUES, Op.getNode()->getVTList(),
1469 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001470}
1471
Dan Gohman8181bd12008-07-27 21:46:04 +00001472SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001473X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001474 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001475 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001476 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001477 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001478 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001479 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001480 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001481 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001482 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001483 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001484 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001485 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001486}
1487
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001488/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001489/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001490SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001491X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001492 SDValue &OutRetAddr,
1493 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001494 bool IsTailCall,
1495 bool Is64Bit,
1496 int FPDiff) {
1497 if (!IsTailCall || FPDiff==0) return Chain;
1498
1499 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001500 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001501 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001502
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001503 // Load the "old" Return address.
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001504 OutRetAddr = DAG.getLoad(VT, Chain, OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001505 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001506}
1507
1508/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1509/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001510static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001511EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001512 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001513 bool Is64Bit, int FPDiff) {
1514 // Store the return address to the appropriate stack slot.
1515 if (!FPDiff) return Chain;
1516 // Calculate the new stack slot for the return address.
1517 int SlotSize = Is64Bit ? 8 : 4;
1518 int NewReturnAddrFI =
1519 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001520 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001521 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001522 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001523 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001524 return Chain;
1525}
1526
Dan Gohman8181bd12008-07-27 21:46:04 +00001527SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001528 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001529 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1530 SDValue Chain = TheCall->getChain();
1531 unsigned CC = TheCall->getCallingConv();
1532 bool isVarArg = TheCall->isVarArg();
1533 bool IsTailCall = TheCall->isTailCall() &&
1534 CC == CallingConv::Fast && PerformTailCallOpt;
1535 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001536 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001537 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001538
1539 assert(!(isVarArg && CC == CallingConv::Fast) &&
1540 "Var args not supported with calling convention fastcc");
1541
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001542 // Analyze operands of the call, assigning locations to each operand.
1543 SmallVector<CCValAssign, 16> ArgLocs;
1544 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001545 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001546
1547 // Get a count of how many bytes are to be pushed on the stack.
1548 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001549 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001550 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001551
Gordon Henriksen18ace102008-01-05 16:56:59 +00001552 int FPDiff = 0;
1553 if (IsTailCall) {
1554 // Lower arguments at fp - stackoffset + fpdiff.
1555 unsigned NumBytesCallerPushed =
1556 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1557 FPDiff = NumBytesCallerPushed - NumBytes;
1558
1559 // Set the delta of movement of the returnaddr stackslot.
1560 // But only set if delta is greater than previous delta.
1561 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1562 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1563 }
1564
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001565 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001566
Dan Gohman8181bd12008-07-27 21:46:04 +00001567 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001568 // Load return adress for tail calls.
1569 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1570 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001571
Dan Gohman8181bd12008-07-27 21:46:04 +00001572 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1573 SmallVector<SDValue, 8> MemOpChains;
1574 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001575
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001576 // Walk the register/memloc assignments, inserting copies/loads. In the case
1577 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001578 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1579 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001580 SDValue Arg = TheCall->getArg(i);
1581 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1582 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001583
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001584 // Promote the value if needed.
1585 switch (VA.getLocInfo()) {
1586 default: assert(0 && "Unknown loc info!");
1587 case CCValAssign::Full: break;
1588 case CCValAssign::SExt:
1589 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1590 break;
1591 case CCValAssign::ZExt:
1592 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1593 break;
1594 case CCValAssign::AExt:
1595 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1596 break;
1597 }
1598
1599 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001600 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001601 MVT RegVT = VA.getLocVT();
1602 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001603 switch (VA.getLocReg()) {
1604 default:
1605 break;
1606 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1607 case X86::R8: {
1608 // Special case: passing MMX values in GPR registers.
1609 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1610 break;
1611 }
1612 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1613 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1614 // Special case: passing MMX values in XMM registers.
1615 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1616 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1617 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1618 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1619 getMOVLMask(2, DAG));
1620 break;
1621 }
1622 }
1623 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001624 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1625 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001626 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001627 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001628 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001629 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1630
Dan Gohman705e3f72008-09-13 01:54:27 +00001631 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1632 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001633 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001634 }
1635 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001636
1637 if (!MemOpChains.empty())
1638 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1639 &MemOpChains[0], MemOpChains.size());
1640
1641 // Build a sequence of copy-to-reg nodes chained together with token chain
1642 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001643 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001644 // Tail call byval lowering might overwrite argument registers so in case of
1645 // tail call optimization the copies to registers are lowered later.
1646 if (!IsTailCall)
1647 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1648 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1649 InFlag);
1650 InFlag = Chain.getValue(1);
1651 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001652
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001653 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001654 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001655 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1656 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1657 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1658 InFlag);
1659 InFlag = Chain.getValue(1);
1660 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001661 // If we are tail calling and generating PIC/GOT style code load the address
1662 // of the callee into ecx. The value in ecx is used as target of the tail
1663 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1664 // calls on PIC/GOT architectures. Normally we would just put the address of
1665 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1666 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001667 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001668 // Note: The actual moving to ecx is done further down.
1669 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001670 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001671 !G->getGlobal()->hasProtectedVisibility())
1672 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001673 else if (isa<ExternalSymbolSDNode>(Callee))
1674 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001675 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001676
Gordon Henriksen18ace102008-01-05 16:56:59 +00001677 if (Is64Bit && isVarArg) {
1678 // From AMD64 ABI document:
1679 // For calls that may call functions that use varargs or stdargs
1680 // (prototype-less calls or calls to functions containing ellipsis (...) in
1681 // the declaration) %al is used as hidden argument to specify the number
1682 // of SSE registers used. The contents of %al do not need to match exactly
1683 // the number of registers, but must be an ubound on the number of SSE
1684 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001685
1686 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001687 // Count the number of XMM registers allocated.
1688 static const unsigned XMMArgRegs[] = {
1689 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1690 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1691 };
1692 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Edwin Törökaf8e1332009-02-01 18:15:56 +00001693 assert((Subtarget->hasSSE1() || !NumXMMRegs)
1694 && "SSE registers cannot be used when SSE is disabled");
Gordon Henriksen18ace102008-01-05 16:56:59 +00001695
1696 Chain = DAG.getCopyToReg(Chain, X86::AL,
1697 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1698 InFlag = Chain.getValue(1);
1699 }
1700
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001701
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001702 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001703 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001704 SmallVector<SDValue, 8> MemOpChains2;
1705 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001706 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001707 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001708 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001709 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1710 CCValAssign &VA = ArgLocs[i];
1711 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001712 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001713 SDValue Arg = TheCall->getArg(i);
1714 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001715 // Create frame index.
1716 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001717 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001718 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001719 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001720
Duncan Sandsc93fae32008-03-21 09:14:45 +00001721 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001722 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001723 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001724 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001725 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1726 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1727
1728 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001729 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001730 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001731 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001732 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001733 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001734 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001735 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001736 }
1737 }
1738
1739 if (!MemOpChains2.empty())
1740 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001741 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001742
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001743 // Copy arguments to their registers.
1744 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1745 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1746 InFlag);
1747 InFlag = Chain.getValue(1);
1748 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001749 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001750
Gordon Henriksen18ace102008-01-05 16:56:59 +00001751 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001752 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1753 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001754 }
1755
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001756 // If the callee is a GlobalAddress node (quite common, every direct call is)
1757 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1758 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1759 // We should use extra load for direct calls to dllimported functions in
1760 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001761 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1762 getTargetMachine(), true))
Dan Gohman36322c72008-10-18 02:06:02 +00001763 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1764 G->getOffset());
Bill Wendlingfef06052008-09-16 21:48:12 +00001765 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1766 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001767 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001768 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001769
1770 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001771 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001772 Callee,InFlag);
1773 Callee = DAG.getRegister(Opc, getPointerTy());
1774 // Add register as live out.
1775 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001776 }
1777
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001778 // Returns a chain & a flag for retval copy to use.
1779 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001780 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001781
1782 if (IsTailCall) {
1783 Ops.push_back(Chain);
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001784 Ops.push_back(DAG.getIntPtrConstant(NumBytes, true));
1785 Ops.push_back(DAG.getIntPtrConstant(0, true));
Gabor Greif1c80d112008-08-28 21:40:38 +00001786 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001787 Ops.push_back(InFlag);
1788 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1789 InFlag = Chain.getValue(1);
1790
1791 // Returns a chain & a flag for retval copy to use.
1792 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1793 Ops.clear();
1794 }
1795
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001796 Ops.push_back(Chain);
1797 Ops.push_back(Callee);
1798
Gordon Henriksen18ace102008-01-05 16:56:59 +00001799 if (IsTailCall)
1800 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001801
Gordon Henriksen18ace102008-01-05 16:56:59 +00001802 // Add argument registers to the end of the list so that they are known live
1803 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001804 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1805 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1806 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001807
Evan Cheng8ba45e62008-03-18 23:36:35 +00001808 // Add an implicit use GOT pointer in EBX.
1809 if (!IsTailCall && !Is64Bit &&
1810 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1811 Subtarget->isPICStyleGOT())
1812 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1813
1814 // Add an implicit use of AL for x86 vararg functions.
1815 if (Is64Bit && isVarArg)
1816 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1817
Gabor Greif1c80d112008-08-28 21:40:38 +00001818 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001819 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001820
Gordon Henriksen18ace102008-01-05 16:56:59 +00001821 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001822 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001823 "Flag must be set. Depend on flag being set in LowerRET");
1824 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman705e3f72008-09-13 01:54:27 +00001825 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001826
Gabor Greif1c80d112008-08-28 21:40:38 +00001827 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001828 }
1829
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001830 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001831 InFlag = Chain.getValue(1);
1832
1833 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001834 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001835 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001836 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001837 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001838 // If this is is a call to a struct-return function, the callee
1839 // pops the hidden struct pointer, so we have to push it back.
1840 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001841 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001842 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001843 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001844
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001845 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001846 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001847 DAG.getIntPtrConstant(NumBytes, true),
1848 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1849 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001850 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001851 InFlag = Chain.getValue(1);
1852
1853 // Handle result values, copying them out of physregs into vregs that we
1854 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001855 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001856 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001857}
1858
1859
1860//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001861// Fast Calling Convention (tail call) implementation
1862//===----------------------------------------------------------------------===//
1863
1864// Like std call, callee cleans arguments, convention except that ECX is
1865// reserved for storing the tail called function address. Only 2 registers are
1866// free for argument passing (inreg). Tail call optimization is performed
1867// provided:
1868// * tailcallopt is enabled
1869// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001870// On X86_64 architecture with GOT-style position independent code only local
1871// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001872// To keep the stack aligned according to platform abi the function
1873// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1874// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001875// If a tail called function callee has more arguments than the caller the
1876// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001877// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001878// original REtADDR, but before the saved framepointer or the spilled registers
1879// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1880// stack layout:
1881// arg1
1882// arg2
1883// RETADDR
1884// [ new RETADDR
1885// move area ]
1886// (possible EBP)
1887// ESI
1888// EDI
1889// local1 ..
1890
1891/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1892/// for a 16 byte align requirement.
1893unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1894 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001895 MachineFunction &MF = DAG.getMachineFunction();
1896 const TargetMachine &TM = MF.getTarget();
1897 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1898 unsigned StackAlignment = TFI.getStackAlignment();
1899 uint64_t AlignMask = StackAlignment - 1;
1900 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001901 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001902 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1903 // Number smaller than 12 so just add the difference.
1904 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1905 } else {
1906 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1907 Offset = ((~AlignMask) & Offset) + StackAlignment +
1908 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001909 }
Evan Chengded8f902008-09-07 09:07:23 +00001910 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001911}
1912
1913/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001914/// following the call is a return. A function is eligible if caller/callee
1915/// calling conventions match, currently only fastcc supports tail calls, and
1916/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001917bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001918 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001919 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001920 if (!PerformTailCallOpt)
1921 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001922
Dan Gohman705e3f72008-09-13 01:54:27 +00001923 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001924 MachineFunction &MF = DAG.getMachineFunction();
1925 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001926 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001927 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001928 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001929 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001930 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001931 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001932 return true;
1933
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001934 // Can only do local tail calls (in same module, hidden or protected) on
1935 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001936 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1937 return G->getGlobal()->hasHiddenVisibility()
1938 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001939 }
1940 }
Evan Chenge7a87392007-11-02 01:26:22 +00001941
1942 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001943}
1944
Dan Gohmanca4857a2008-09-03 23:12:08 +00001945FastISel *
1946X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001947 MachineModuleInfo *mmo,
Devang Patelfcf1c752009-01-13 00:35:13 +00001948 DwarfWriter *dw,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001949 DenseMap<const Value *, unsigned> &vm,
1950 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001951 MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00001952 DenseMap<const AllocaInst *, int> &am
1953#ifndef NDEBUG
1954 , SmallSet<Instruction*, 8> &cil
1955#endif
1956 ) {
Devang Patelfcf1c752009-01-13 00:35:13 +00001957 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohman9dd43582008-10-14 23:54:11 +00001958#ifndef NDEBUG
1959 , cil
1960#endif
1961 );
Dan Gohman97805ee2008-08-19 21:32:53 +00001962}
1963
1964
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001965//===----------------------------------------------------------------------===//
1966// Other Lowering Hooks
1967//===----------------------------------------------------------------------===//
1968
1969
Dan Gohman8181bd12008-07-27 21:46:04 +00001970SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001971 MachineFunction &MF = DAG.getMachineFunction();
1972 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1973 int ReturnAddrIndex = FuncInfo->getRAIndex();
1974
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001975 if (ReturnAddrIndex == 0) {
1976 // Set up a frame object for the return address.
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001977 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001978 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001979 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001980 }
1981
1982 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1983}
1984
1985
Chris Lattnerebb91142008-12-24 23:53:05 +00001986/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
1987/// specific condition code, returning the condition code and the LHS/RHS of the
1988/// comparison to make.
1989static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1990 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001991 if (!isFP) {
1992 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1993 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1994 // X > -1 -> X == 0, jump !sign.
1995 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00001996 return X86::COND_NS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001997 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1998 // X < 0 -> X == 0, jump on sign.
Chris Lattnerebb91142008-12-24 23:53:05 +00001999 return X86::COND_S;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002000 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00002001 // X < 1 -> X <= 0
2002 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00002003 return X86::COND_LE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002004 }
2005 }
2006
2007 switch (SetCCOpcode) {
Chris Lattnerb8397512008-12-23 23:42:27 +00002008 default: assert(0 && "Invalid integer condition!");
Chris Lattnerebb91142008-12-24 23:53:05 +00002009 case ISD::SETEQ: return X86::COND_E;
2010 case ISD::SETGT: return X86::COND_G;
2011 case ISD::SETGE: return X86::COND_GE;
2012 case ISD::SETLT: return X86::COND_L;
2013 case ISD::SETLE: return X86::COND_LE;
2014 case ISD::SETNE: return X86::COND_NE;
2015 case ISD::SETULT: return X86::COND_B;
2016 case ISD::SETUGT: return X86::COND_A;
2017 case ISD::SETULE: return X86::COND_BE;
2018 case ISD::SETUGE: return X86::COND_AE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002019 }
Chris Lattnerb8397512008-12-23 23:42:27 +00002020 }
2021
2022 // First determine if it is required or is profitable to flip the operands.
Duncan Sandsc2a04622008-10-24 13:03:10 +00002023
Chris Lattnerb8397512008-12-23 23:42:27 +00002024 // If LHS is a foldable load, but RHS is not, flip the condition.
2025 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2026 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2027 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2028 std::swap(LHS, RHS);
Evan Chengfc937c92008-08-28 23:48:31 +00002029 }
2030
Chris Lattnerb8397512008-12-23 23:42:27 +00002031 switch (SetCCOpcode) {
2032 default: break;
2033 case ISD::SETOLT:
2034 case ISD::SETOLE:
2035 case ISD::SETUGT:
2036 case ISD::SETUGE:
2037 std::swap(LHS, RHS);
2038 break;
2039 }
2040
2041 // On a floating point condition, the flags are set as follows:
2042 // ZF PF CF op
2043 // 0 | 0 | 0 | X > Y
2044 // 0 | 0 | 1 | X < Y
2045 // 1 | 0 | 0 | X == Y
2046 // 1 | 1 | 1 | unordered
2047 switch (SetCCOpcode) {
Chris Lattnerebb91142008-12-24 23:53:05 +00002048 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattnerb8397512008-12-23 23:42:27 +00002049 case ISD::SETUEQ:
Chris Lattnerebb91142008-12-24 23:53:05 +00002050 case ISD::SETEQ: return X86::COND_E;
Chris Lattnerb8397512008-12-23 23:42:27 +00002051 case ISD::SETOLT: // flipped
2052 case ISD::SETOGT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002053 case ISD::SETGT: return X86::COND_A;
Chris Lattnerb8397512008-12-23 23:42:27 +00002054 case ISD::SETOLE: // flipped
2055 case ISD::SETOGE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002056 case ISD::SETGE: return X86::COND_AE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002057 case ISD::SETUGT: // flipped
2058 case ISD::SETULT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002059 case ISD::SETLT: return X86::COND_B;
Chris Lattnerb8397512008-12-23 23:42:27 +00002060 case ISD::SETUGE: // flipped
2061 case ISD::SETULE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002062 case ISD::SETLE: return X86::COND_BE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002063 case ISD::SETONE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002064 case ISD::SETNE: return X86::COND_NE;
2065 case ISD::SETUO: return X86::COND_P;
2066 case ISD::SETO: return X86::COND_NP;
Chris Lattnerb8397512008-12-23 23:42:27 +00002067 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002068}
2069
2070/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2071/// code. Current x86 isa includes the following FP cmov instructions:
2072/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2073static bool hasFPCMov(unsigned X86CC) {
2074 switch (X86CC) {
2075 default:
2076 return false;
2077 case X86::COND_B:
2078 case X86::COND_BE:
2079 case X86::COND_E:
2080 case X86::COND_P:
2081 case X86::COND_A:
2082 case X86::COND_AE:
2083 case X86::COND_NE:
2084 case X86::COND_NP:
2085 return true;
2086 }
2087}
2088
2089/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2090/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002091static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002092 if (Op.getOpcode() == ISD::UNDEF)
2093 return true;
2094
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002095 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002096 return (Val >= Low && Val < Hi);
2097}
2098
2099/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2100/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002101static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002102 if (Op.getOpcode() == ISD::UNDEF)
2103 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002104 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002105}
2106
2107/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2108/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2109bool X86::isPSHUFDMask(SDNode *N) {
2110 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2111
Dan Gohman7dc19012007-08-02 21:17:01 +00002112 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002113 return false;
2114
2115 // Check if the value doesn't reference the second vector.
2116 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002117 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002118 if (Arg.getOpcode() == ISD::UNDEF) continue;
2119 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002120 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002121 return false;
2122 }
2123
2124 return true;
2125}
2126
2127/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2128/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2129bool X86::isPSHUFHWMask(SDNode *N) {
2130 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2131
2132 if (N->getNumOperands() != 8)
2133 return false;
2134
2135 // Lower quadword copied in order.
2136 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002137 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002138 if (Arg.getOpcode() == ISD::UNDEF) continue;
2139 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002140 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002141 return false;
2142 }
2143
2144 // Upper quadword shuffled.
2145 for (unsigned i = 4; i != 8; ++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 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002150 if (Val < 4 || Val > 7)
2151 return false;
2152 }
2153
2154 return true;
2155}
2156
2157/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2158/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2159bool X86::isPSHUFLWMask(SDNode *N) {
2160 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2161
2162 if (N->getNumOperands() != 8)
2163 return false;
2164
2165 // Upper quadword copied in order.
2166 for (unsigned i = 4; i != 8; ++i)
2167 if (!isUndefOrEqual(N->getOperand(i), i))
2168 return false;
2169
2170 // Lower quadword shuffled.
2171 for (unsigned i = 0; i != 4; ++i)
2172 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2173 return false;
2174
2175 return true;
2176}
2177
2178/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2179/// specifies a shuffle of elements that is suitable for input to SHUFP*.
djgc2517d32009-01-26 04:35:06 +00002180template<class SDOperand>
2181static bool isSHUFPMask(SDOperand *Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002182 if (NumElems != 2 && NumElems != 4) return false;
2183
2184 unsigned Half = NumElems / 2;
2185 for (unsigned i = 0; i < Half; ++i)
2186 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2187 return false;
2188 for (unsigned i = Half; i < NumElems; ++i)
2189 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2190 return false;
2191
2192 return true;
2193}
2194
2195bool X86::isSHUFPMask(SDNode *N) {
2196 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2197 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2198}
2199
2200/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2201/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2202/// half elements to come from vector 1 (which would equal the dest.) and
2203/// the upper half to come from vector 2.
djgc2517d32009-01-26 04:35:06 +00002204template<class SDOperand>
2205static bool isCommutedSHUFP(SDOperand *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002206 if (NumOps != 2 && NumOps != 4) return false;
2207
2208 unsigned Half = NumOps / 2;
2209 for (unsigned i = 0; i < Half; ++i)
2210 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2211 return false;
2212 for (unsigned i = Half; i < NumOps; ++i)
2213 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2214 return false;
2215 return true;
2216}
2217
2218static bool isCommutedSHUFP(SDNode *N) {
2219 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2220 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2221}
2222
2223/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2224/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2225bool X86::isMOVHLPSMask(SDNode *N) {
2226 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2227
2228 if (N->getNumOperands() != 4)
2229 return false;
2230
2231 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2232 return isUndefOrEqual(N->getOperand(0), 6) &&
2233 isUndefOrEqual(N->getOperand(1), 7) &&
2234 isUndefOrEqual(N->getOperand(2), 2) &&
2235 isUndefOrEqual(N->getOperand(3), 3);
2236}
2237
2238/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2239/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2240/// <2, 3, 2, 3>
2241bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2242 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2243
2244 if (N->getNumOperands() != 4)
2245 return false;
2246
2247 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2248 return isUndefOrEqual(N->getOperand(0), 2) &&
2249 isUndefOrEqual(N->getOperand(1), 3) &&
2250 isUndefOrEqual(N->getOperand(2), 2) &&
2251 isUndefOrEqual(N->getOperand(3), 3);
2252}
2253
2254/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2255/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2256bool X86::isMOVLPMask(SDNode *N) {
2257 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2258
2259 unsigned NumElems = N->getNumOperands();
2260 if (NumElems != 2 && NumElems != 4)
2261 return false;
2262
2263 for (unsigned i = 0; i < NumElems/2; ++i)
2264 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2265 return false;
2266
2267 for (unsigned i = NumElems/2; i < NumElems; ++i)
2268 if (!isUndefOrEqual(N->getOperand(i), i))
2269 return false;
2270
2271 return true;
2272}
2273
2274/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2275/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2276/// and MOVLHPS.
2277bool X86::isMOVHPMask(SDNode *N) {
2278 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2279
2280 unsigned NumElems = N->getNumOperands();
2281 if (NumElems != 2 && NumElems != 4)
2282 return false;
2283
2284 for (unsigned i = 0; i < NumElems/2; ++i)
2285 if (!isUndefOrEqual(N->getOperand(i), i))
2286 return false;
2287
2288 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002289 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002290 if (!isUndefOrEqual(Arg, i + NumElems))
2291 return false;
2292 }
2293
2294 return true;
2295}
2296
2297/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2298/// specifies a shuffle of elements that is suitable for input to UNPCKL.
djgc2517d32009-01-26 04:35:06 +00002299template<class SDOperand>
2300bool static isUNPCKLMask(SDOperand *Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002301 bool V2IsSplat = false) {
2302 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2303 return false;
2304
2305 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002306 SDValue BitI = Elts[i];
2307 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002308 if (!isUndefOrEqual(BitI, j))
2309 return false;
2310 if (V2IsSplat) {
2311 if (isUndefOrEqual(BitI1, NumElts))
2312 return false;
2313 } else {
2314 if (!isUndefOrEqual(BitI1, j + NumElts))
2315 return false;
2316 }
2317 }
2318
2319 return true;
2320}
2321
2322bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2323 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2324 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2325}
2326
2327/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2328/// specifies a shuffle of elements that is suitable for input to UNPCKH.
djgc2517d32009-01-26 04:35:06 +00002329template<class SDOperand>
2330bool static isUNPCKHMask(SDOperand *Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002331 bool V2IsSplat = false) {
2332 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2333 return false;
2334
2335 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002336 SDValue BitI = Elts[i];
2337 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002338 if (!isUndefOrEqual(BitI, j + NumElts/2))
2339 return false;
2340 if (V2IsSplat) {
2341 if (isUndefOrEqual(BitI1, NumElts))
2342 return false;
2343 } else {
2344 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2345 return false;
2346 }
2347 }
2348
2349 return true;
2350}
2351
2352bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2353 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2354 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2355}
2356
2357/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2358/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2359/// <0, 0, 1, 1>
2360bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2361 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2362
2363 unsigned NumElems = N->getNumOperands();
2364 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2365 return false;
2366
2367 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002368 SDValue BitI = N->getOperand(i);
2369 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002370
2371 if (!isUndefOrEqual(BitI, j))
2372 return false;
2373 if (!isUndefOrEqual(BitI1, j))
2374 return false;
2375 }
2376
2377 return true;
2378}
2379
2380/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2381/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2382/// <2, 2, 3, 3>
2383bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2384 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2385
2386 unsigned NumElems = N->getNumOperands();
2387 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2388 return false;
2389
2390 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002391 SDValue BitI = N->getOperand(i);
2392 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002393
2394 if (!isUndefOrEqual(BitI, j))
2395 return false;
2396 if (!isUndefOrEqual(BitI1, j))
2397 return false;
2398 }
2399
2400 return true;
2401}
2402
2403/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2404/// specifies a shuffle of elements that is suitable for input to MOVSS,
2405/// MOVSD, and MOVD, i.e. setting the lowest element.
djgc2517d32009-01-26 04:35:06 +00002406template<class SDOperand>
2407static bool isMOVLMask(SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002408 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002409 return false;
2410
2411 if (!isUndefOrEqual(Elts[0], NumElts))
2412 return false;
2413
2414 for (unsigned i = 1; i < NumElts; ++i) {
2415 if (!isUndefOrEqual(Elts[i], i))
2416 return false;
2417 }
2418
2419 return true;
2420}
2421
2422bool X86::isMOVLMask(SDNode *N) {
2423 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2424 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2425}
2426
2427/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2428/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2429/// element of vector 2 and the other elements to come from vector 1 in order.
djgc2517d32009-01-26 04:35:06 +00002430template<class SDOperand>
2431static bool isCommutedMOVL(SDOperand *Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002432 bool V2IsSplat = false,
2433 bool V2IsUndef = false) {
2434 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2435 return false;
2436
2437 if (!isUndefOrEqual(Ops[0], 0))
2438 return false;
2439
2440 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002441 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002442 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2443 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2444 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2445 return false;
2446 }
2447
2448 return true;
2449}
2450
2451static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2452 bool V2IsUndef = false) {
2453 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2454 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2455 V2IsSplat, V2IsUndef);
2456}
2457
2458/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2459/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2460bool X86::isMOVSHDUPMask(SDNode *N) {
2461 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2462
2463 if (N->getNumOperands() != 4)
2464 return false;
2465
2466 // Expect 1, 1, 3, 3
2467 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002468 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002469 if (Arg.getOpcode() == ISD::UNDEF) continue;
2470 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002471 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002472 if (Val != 1) return false;
2473 }
2474
2475 bool HasHi = false;
2476 for (unsigned i = 2; i < 4; ++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 != 3) return false;
2482 HasHi = true;
2483 }
2484
2485 // Don't use movshdup if it can be done with a shufps.
2486 return HasHi;
2487}
2488
2489/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2490/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2491bool X86::isMOVSLDUPMask(SDNode *N) {
2492 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2493
2494 if (N->getNumOperands() != 4)
2495 return false;
2496
2497 // Expect 0, 0, 2, 2
2498 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002499 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002500 if (Arg.getOpcode() == ISD::UNDEF) continue;
2501 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002502 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002503 if (Val != 0) return false;
2504 }
2505
2506 bool HasHi = false;
2507 for (unsigned i = 2; i < 4; ++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 != 2) return false;
2513 HasHi = true;
2514 }
2515
2516 // Don't use movshdup if it can be done with a shufps.
2517 return HasHi;
2518}
2519
2520/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2521/// specifies a identity operation on the LHS or RHS.
2522static bool isIdentityMask(SDNode *N, bool RHS = false) {
2523 unsigned NumElems = N->getNumOperands();
2524 for (unsigned i = 0; i < NumElems; ++i)
2525 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2526 return false;
2527 return true;
2528}
2529
2530/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2531/// a splat of a single element.
2532static bool isSplatMask(SDNode *N) {
2533 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2534
2535 // This is a splat operation if each element of the permute is the same, and
2536 // if the value doesn't reference the second vector.
2537 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002538 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002539 unsigned i = 0;
2540 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002541 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002542 if (isa<ConstantSDNode>(Elt)) {
2543 ElementBase = Elt;
2544 break;
2545 }
2546 }
2547
Gabor Greif1c80d112008-08-28 21:40:38 +00002548 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002549 return false;
2550
2551 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002552 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002553 if (Arg.getOpcode() == ISD::UNDEF) continue;
2554 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2555 if (Arg != ElementBase) return false;
2556 }
2557
2558 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002559 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002560}
2561
Mon P Wang532c9632008-12-23 04:03:27 +00002562/// getSplatMaskEltNo - Given a splat mask, return the index to the element
2563/// we want to splat.
2564static SDValue getSplatMaskEltNo(SDNode *N) {
2565 assert(isSplatMask(N) && "Not a splat mask");
2566 unsigned NumElems = N->getNumOperands();
2567 SDValue ElementBase;
2568 unsigned i = 0;
2569 for (; i != NumElems; ++i) {
2570 SDValue Elt = N->getOperand(i);
2571 if (isa<ConstantSDNode>(Elt))
2572 return Elt;
2573 }
2574 assert(0 && " No splat value found!");
2575 return SDValue();
2576}
2577
2578
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002579/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2580/// a splat of a single element and it's a 2 or 4 element mask.
2581bool X86::isSplatMask(SDNode *N) {
2582 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2583
2584 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2585 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2586 return false;
2587 return ::isSplatMask(N);
2588}
2589
2590/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2591/// specifies a splat of zero element.
2592bool X86::isSplatLoMask(SDNode *N) {
2593 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2594
2595 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2596 if (!isUndefOrEqual(N->getOperand(i), 0))
2597 return false;
2598 return true;
2599}
2600
Evan Chenga2497eb2008-09-25 20:50:48 +00002601/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2602/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2603bool X86::isMOVDDUPMask(SDNode *N) {
2604 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2605
2606 unsigned e = N->getNumOperands() / 2;
2607 for (unsigned i = 0; i < e; ++i)
2608 if (!isUndefOrEqual(N->getOperand(i), i))
2609 return false;
2610 for (unsigned i = 0; i < e; ++i)
2611 if (!isUndefOrEqual(N->getOperand(e+i), i))
2612 return false;
2613 return true;
2614}
2615
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002616/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2617/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2618/// instructions.
2619unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2620 unsigned NumOperands = N->getNumOperands();
2621 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2622 unsigned Mask = 0;
2623 for (unsigned i = 0; i < NumOperands; ++i) {
2624 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002625 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002626 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002627 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002628 if (Val >= NumOperands) Val -= NumOperands;
2629 Mask |= Val;
2630 if (i != NumOperands - 1)
2631 Mask <<= Shift;
2632 }
2633
2634 return Mask;
2635}
2636
2637/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2638/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2639/// instructions.
2640unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2641 unsigned Mask = 0;
2642 // 8 nodes, but we only care about the last 4.
2643 for (unsigned i = 7; i >= 4; --i) {
2644 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002645 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002646 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002647 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002648 Mask |= (Val - 4);
2649 if (i != 4)
2650 Mask <<= 2;
2651 }
2652
2653 return Mask;
2654}
2655
2656/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2657/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2658/// instructions.
2659unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2660 unsigned Mask = 0;
2661 // 8 nodes, but we only care about the first 4.
2662 for (int i = 3; i >= 0; --i) {
2663 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002664 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002666 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002667 Mask |= Val;
2668 if (i != 0)
2669 Mask <<= 2;
2670 }
2671
2672 return Mask;
2673}
2674
2675/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2676/// specifies a 8 element shuffle that can be broken into a pair of
2677/// PSHUFHW and PSHUFLW.
2678static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2679 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2680
2681 if (N->getNumOperands() != 8)
2682 return false;
2683
2684 // Lower quadword shuffled.
2685 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002686 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002687 if (Arg.getOpcode() == ISD::UNDEF) continue;
2688 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002689 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002690 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002691 return false;
2692 }
2693
2694 // Upper quadword shuffled.
2695 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002696 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002697 if (Arg.getOpcode() == ISD::UNDEF) continue;
2698 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002699 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002700 if (Val < 4 || Val > 7)
2701 return false;
2702 }
2703
2704 return true;
2705}
2706
Chris Lattnere6aa3862007-11-25 00:24:49 +00002707/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002708/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002709static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2710 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002711 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002712 MVT VT = Op.getValueType();
2713 MVT MaskVT = Mask.getValueType();
2714 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002715 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002716 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002717
2718 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002719 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002720 if (Arg.getOpcode() == ISD::UNDEF) {
2721 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2722 continue;
2723 }
2724 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002725 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002726 if (Val < NumElems)
2727 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2728 else
2729 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2730 }
2731
2732 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002733 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002734 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2735}
2736
Evan Chenga6769df2007-12-07 21:30:01 +00002737/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2738/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002739static
Dan Gohman8181bd12008-07-27 21:46:04 +00002740SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002741 MVT MaskVT = Mask.getValueType();
2742 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002743 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002744 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002745 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002746 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002747 if (Arg.getOpcode() == ISD::UNDEF) {
2748 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2749 continue;
2750 }
2751 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002752 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002753 if (Val < NumElems)
2754 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2755 else
2756 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2757 }
2758 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2759}
2760
2761
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002762/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2763/// match movhlps. The lower half elements should come from upper half of
2764/// V1 (and in order), and the upper half elements should come from the upper
2765/// half of V2 (and in order).
2766static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2767 unsigned NumElems = Mask->getNumOperands();
2768 if (NumElems != 4)
2769 return false;
2770 for (unsigned i = 0, e = 2; i != e; ++i)
2771 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2772 return false;
2773 for (unsigned i = 2; i != 4; ++i)
2774 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2775 return false;
2776 return true;
2777}
2778
2779/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002780/// is promoted to a vector. It also returns the LoadSDNode by reference if
2781/// required.
2782static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002783 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2784 return false;
2785 N = N->getOperand(0).getNode();
2786 if (!ISD::isNON_EXTLoad(N))
2787 return false;
2788 if (LD)
2789 *LD = cast<LoadSDNode>(N);
2790 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002791}
2792
2793/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2794/// match movlp{s|d}. The lower half elements should come from lower half of
2795/// V1 (and in order), and the upper half elements should come from the upper
2796/// half of V2 (and in order). And since V1 will become the source of the
2797/// MOVLP, it must be either a vector load or a scalar load to vector.
2798static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2799 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2800 return false;
2801 // Is V2 is a vector load, don't do this transformation. We will try to use
2802 // load folding shufps op.
2803 if (ISD::isNON_EXTLoad(V2))
2804 return false;
2805
2806 unsigned NumElems = Mask->getNumOperands();
2807 if (NumElems != 2 && NumElems != 4)
2808 return false;
2809 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2810 if (!isUndefOrEqual(Mask->getOperand(i), i))
2811 return false;
2812 for (unsigned i = NumElems/2; i != NumElems; ++i)
2813 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2814 return false;
2815 return true;
2816}
2817
2818/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2819/// all the same.
2820static bool isSplatVector(SDNode *N) {
2821 if (N->getOpcode() != ISD::BUILD_VECTOR)
2822 return false;
2823
Dan Gohman8181bd12008-07-27 21:46:04 +00002824 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002825 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2826 if (N->getOperand(i) != SplatValue)
2827 return false;
2828 return true;
2829}
2830
2831/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2832/// to an undef.
2833static bool isUndefShuffle(SDNode *N) {
2834 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2835 return false;
2836
Dan Gohman8181bd12008-07-27 21:46:04 +00002837 SDValue V1 = N->getOperand(0);
2838 SDValue V2 = N->getOperand(1);
2839 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002840 unsigned NumElems = Mask.getNumOperands();
2841 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002842 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002843 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002844 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002845 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2846 return false;
2847 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2848 return false;
2849 }
2850 }
2851 return true;
2852}
2853
2854/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2855/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002856static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002857 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002858 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002859 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002860 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002861}
2862
2863/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2864/// to an zero vector.
2865static bool isZeroShuffle(SDNode *N) {
2866 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2867 return false;
2868
Dan Gohman8181bd12008-07-27 21:46:04 +00002869 SDValue V1 = N->getOperand(0);
2870 SDValue V2 = N->getOperand(1);
2871 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002872 unsigned NumElems = Mask.getNumOperands();
2873 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002874 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002875 if (Arg.getOpcode() == ISD::UNDEF)
2876 continue;
2877
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002878 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002879 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002880 unsigned Opc = V1.getNode()->getOpcode();
2881 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002882 continue;
2883 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002884 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002885 return false;
2886 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002887 unsigned Opc = V2.getNode()->getOpcode();
2888 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002889 continue;
2890 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002891 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002892 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002893 }
2894 }
2895 return true;
2896}
2897
2898/// getZeroVector - Returns a vector of specified type with all zero elements.
2899///
Dan Gohman8181bd12008-07-27 21:46:04 +00002900static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002901 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002902
2903 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2904 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002905 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002906 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002907 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002908 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002909 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002910 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002911 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002912 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002913 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002914 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2915 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002916 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002917}
2918
Chris Lattnere6aa3862007-11-25 00:24:49 +00002919/// getOnesVector - Returns a vector of specified type with all bits set.
2920///
Dan Gohman8181bd12008-07-27 21:46:04 +00002921static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002922 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002923
2924 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2925 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002926 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2927 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002928 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002929 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2930 else // SSE
2931 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2932 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2933}
2934
2935
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002936/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2937/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002938static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002939 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2940
2941 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002942 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943 unsigned NumElems = Mask.getNumOperands();
2944 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002945 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002946 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002947 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002948 if (Val > NumElems) {
2949 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2950 Changed = true;
2951 }
2952 }
2953 MaskVec.push_back(Arg);
2954 }
2955
2956 if (Changed)
2957 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2958 &MaskVec[0], MaskVec.size());
2959 return Mask;
2960}
2961
2962/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2963/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002964static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002965 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2966 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002967
Dan Gohman8181bd12008-07-27 21:46:04 +00002968 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002969 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2970 for (unsigned i = 1; i != NumElems; ++i)
2971 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2972 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2973}
2974
2975/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2976/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002977static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002978 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2979 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002980 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002981 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2982 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2983 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2984 }
2985 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2986}
2987
2988/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2989/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002990static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002991 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2992 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002993 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002994 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002995 for (unsigned i = 0; i != Half; ++i) {
2996 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2997 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2998 }
2999 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3000}
3001
Chris Lattner2d91b962008-03-09 01:05:04 +00003002/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
3003/// element #0 of a vector with the specified index, leaving the rest of the
3004/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00003005static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00003006 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003007 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3008 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003009 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00003010 // Element #0 of the result gets the elt we are replacing.
3011 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
3012 for (unsigned i = 1; i != NumElems; ++i)
3013 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
3014 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
3015}
3016
Evan Chengbf8b2c52008-04-05 00:30:36 +00003017/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00003018static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003019 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
3020 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003021 if (PVT == VT)
3022 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00003023 SDValue V1 = Op.getOperand(0);
3024 SDValue Mask = Op.getOperand(2);
Mon P Wang532c9632008-12-23 04:03:27 +00003025 unsigned MaskNumElems = Mask.getNumOperands();
3026 unsigned NumElems = MaskNumElems;
Evan Chengbf8b2c52008-04-05 00:30:36 +00003027 // Special handling of v4f32 -> v4i32.
3028 if (VT != MVT::v4f32) {
Mon P Wang532c9632008-12-23 04:03:27 +00003029 // Find which element we want to splat.
3030 SDNode* EltNoNode = getSplatMaskEltNo(Mask.getNode()).getNode();
3031 unsigned EltNo = cast<ConstantSDNode>(EltNoNode)->getZExtValue();
3032 // unpack elements to the correct location
Evan Chengbf8b2c52008-04-05 00:30:36 +00003033 while (NumElems > 4) {
Mon P Wang532c9632008-12-23 04:03:27 +00003034 if (EltNo < NumElems/2) {
3035 Mask = getUnpacklMask(MaskNumElems, DAG);
3036 } else {
3037 Mask = getUnpackhMask(MaskNumElems, DAG);
3038 EltNo -= NumElems/2;
3039 }
Evan Chengbf8b2c52008-04-05 00:30:36 +00003040 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
3041 NumElems >>= 1;
3042 }
Mon P Wang532c9632008-12-23 04:03:27 +00003043 SDValue Cst = DAG.getConstant(EltNo, MVT::i32);
3044 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003045 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003046
Evan Chengbf8b2c52008-04-05 00:30:36 +00003047 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00003048 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00003049 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003050 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3051}
3052
Evan Chenga2497eb2008-09-25 20:50:48 +00003053/// isVectorLoad - Returns true if the node is a vector load, a scalar
3054/// load that's promoted to vector, or a load bitcasted.
3055static bool isVectorLoad(SDValue Op) {
3056 assert(Op.getValueType().isVector() && "Expected a vector type");
3057 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
3058 Op.getOpcode() == ISD::BIT_CONVERT) {
3059 return isa<LoadSDNode>(Op.getOperand(0));
3060 }
3061 return isa<LoadSDNode>(Op);
3062}
3063
3064
3065/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3066///
3067static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3068 SelectionDAG &DAG, bool HasSSE3) {
3069 // If we have sse3 and shuffle has more than one use or input is a load, then
3070 // use movddup. Otherwise, use movlhps.
3071 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3072 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3073 MVT VT = Op.getValueType();
3074 if (VT == PVT)
3075 return Op;
3076 unsigned NumElems = PVT.getVectorNumElements();
3077 if (NumElems == 2) {
3078 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3079 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
3080 } else {
3081 assert(NumElems == 4);
3082 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3083 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
3084 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst0, Cst1, Cst0, Cst1);
3085 }
3086
3087 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
3088 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
3089 DAG.getNode(ISD::UNDEF, PVT), Mask);
3090 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3091}
3092
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003093/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003094/// vector of zero or undef vector. This produces a shuffle where the low
3095/// element of V2 is swizzled into the zero/undef vector, landing at element
3096/// 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 +00003097static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003098 bool isZero, bool HasSSE2,
3099 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003100 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003101 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00003102 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003103 unsigned NumElems = V2.getValueType().getVectorNumElements();
3104 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3105 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003106 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003107 for (unsigned i = 0; i != NumElems; ++i)
3108 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3109 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3110 else
3111 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003112 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003113 &MaskVec[0], MaskVec.size());
3114 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3115}
3116
Evan Chengdea99362008-05-29 08:22:04 +00003117/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3118/// a shuffle that is zero.
3119static
Dan Gohman8181bd12008-07-27 21:46:04 +00003120unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003121 unsigned NumElems, bool Low,
3122 SelectionDAG &DAG) {
3123 unsigned NumZeros = 0;
3124 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003125 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003126 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003127 if (Idx.getOpcode() == ISD::UNDEF) {
3128 ++NumZeros;
3129 continue;
3130 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003131 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3132 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003133 ++NumZeros;
3134 else
3135 break;
3136 }
3137 return NumZeros;
3138}
3139
3140/// isVectorShift - Returns true if the shuffle can be implemented as a
3141/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003142static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3143 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003144 unsigned NumElems = Mask.getNumOperands();
3145
3146 isLeft = true;
3147 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3148 if (!NumZeros) {
3149 isLeft = false;
3150 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3151 if (!NumZeros)
3152 return false;
3153 }
3154
3155 bool SeenV1 = false;
3156 bool SeenV2 = false;
3157 for (unsigned i = NumZeros; i < NumElems; ++i) {
3158 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003159 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003160 if (Idx.getOpcode() == ISD::UNDEF)
3161 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003162 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003163 if (Index < NumElems)
3164 SeenV1 = true;
3165 else {
3166 Index -= NumElems;
3167 SeenV2 = true;
3168 }
3169 if (Index != Val)
3170 return false;
3171 }
3172 if (SeenV1 && SeenV2)
3173 return false;
3174
3175 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3176 ShAmt = NumZeros;
3177 return true;
3178}
3179
3180
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003181/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3182///
Dan Gohman8181bd12008-07-27 21:46:04 +00003183static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003184 unsigned NumNonZero, unsigned NumZero,
3185 SelectionDAG &DAG, TargetLowering &TLI) {
3186 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003187 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003188
Dan Gohman8181bd12008-07-27 21:46:04 +00003189 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003190 bool First = true;
3191 for (unsigned i = 0; i < 16; ++i) {
3192 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3193 if (ThisIsNonZero && First) {
3194 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003195 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003196 else
3197 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3198 First = false;
3199 }
3200
3201 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003202 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003203 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3204 if (LastIsNonZero) {
3205 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3206 }
3207 if (ThisIsNonZero) {
3208 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3209 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3210 ThisElt, DAG.getConstant(8, MVT::i8));
3211 if (LastIsNonZero)
3212 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3213 } else
3214 ThisElt = LastElt;
3215
Gabor Greif1c80d112008-08-28 21:40:38 +00003216 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003217 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003218 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003219 }
3220 }
3221
3222 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3223}
3224
3225/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3226///
Dan Gohman8181bd12008-07-27 21:46:04 +00003227static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003228 unsigned NumNonZero, unsigned NumZero,
3229 SelectionDAG &DAG, TargetLowering &TLI) {
3230 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003231 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003232
Dan Gohman8181bd12008-07-27 21:46:04 +00003233 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003234 bool First = true;
3235 for (unsigned i = 0; i < 8; ++i) {
3236 bool isNonZero = (NonZeros & (1 << i)) != 0;
3237 if (isNonZero) {
3238 if (First) {
3239 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003240 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003241 else
3242 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3243 First = false;
3244 }
3245 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003246 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003247 }
3248 }
3249
3250 return V;
3251}
3252
Evan Chengdea99362008-05-29 08:22:04 +00003253/// getVShift - Return a vector logical shift node.
3254///
Dan Gohman8181bd12008-07-27 21:46:04 +00003255static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003256 unsigned NumBits, SelectionDAG &DAG,
3257 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003258 bool isMMX = VT.getSizeInBits() == 64;
3259 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003260 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3261 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3262 return DAG.getNode(ISD::BIT_CONVERT, VT,
3263 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003264 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003265}
3266
Dan Gohman8181bd12008-07-27 21:46:04 +00003267SDValue
3268X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003269 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003270 if (ISD::isBuildVectorAllZeros(Op.getNode())
3271 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003272 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3273 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3274 // eliminated on x86-32 hosts.
3275 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3276 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003277
Gabor Greif1c80d112008-08-28 21:40:38 +00003278 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003279 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003280 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003281 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003282
Duncan Sands92c43912008-06-06 12:08:01 +00003283 MVT VT = Op.getValueType();
3284 MVT EVT = VT.getVectorElementType();
3285 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286
3287 unsigned NumElems = Op.getNumOperands();
3288 unsigned NumZero = 0;
3289 unsigned NumNonZero = 0;
3290 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003291 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003292 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003293 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003294 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003295 if (Elt.getOpcode() == ISD::UNDEF)
3296 continue;
3297 Values.insert(Elt);
3298 if (Elt.getOpcode() != ISD::Constant &&
3299 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003300 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003301 if (isZeroNode(Elt))
3302 NumZero++;
3303 else {
3304 NonZeros |= (1 << i);
3305 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003306 }
3307 }
3308
3309 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003310 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3311 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003312 }
3313
Chris Lattner66a4dda2008-03-09 05:42:06 +00003314 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003315 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003316 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003317 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003318
Chris Lattner2d91b962008-03-09 01:05:04 +00003319 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3320 // the value are obviously zero, truncate the value to i32 and do the
3321 // insertion that way. Only do this if the value is non-constant or if the
3322 // value is a constant being inserted into element 0. It is cheaper to do
3323 // a constant pool load than it is to do a movd + shuffle.
3324 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3325 (!IsAllConstants || Idx == 0)) {
3326 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3327 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003328 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3329 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003330
3331 // Truncate the value (which may itself be a constant) to i32, and
3332 // convert it to a vector with movd (S2V+shuffle to zero extend).
3333 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3334 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003335 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3336 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003337
3338 // Now we have our 32-bit value zero extended in the low element of
3339 // a vector. If Idx != 0, swizzle it into place.
3340 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003341 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003342 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3343 getSwapEltZeroMask(VecElts, Idx, DAG)
3344 };
3345 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3346 }
3347 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3348 }
3349 }
3350
Chris Lattnerac914892008-03-08 22:59:52 +00003351 // If we have a constant or non-constant insertion into the low element of
3352 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3353 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3354 // depending on what the source datatype is. Because we can only get here
3355 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3356 if (Idx == 0 &&
3357 // Don't do this for i64 values on x86-32.
3358 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003359 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003360 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003361 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3362 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003363 }
Evan Chengdea99362008-05-29 08:22:04 +00003364
3365 // Is it a vector logical left shift?
3366 if (NumElems == 2 && Idx == 1 &&
3367 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003368 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003369 return getVShift(true, VT,
3370 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3371 NumBits/2, DAG, *this);
3372 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003373
3374 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003375 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003376
Chris Lattnerac914892008-03-08 22:59:52 +00003377 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3378 // is a non-constant being inserted into an element other than the low one,
3379 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3380 // movd/movss) to move this into the low element, then shuffle it into
3381 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003382 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003383 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3384
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003385 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003386 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3387 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003388 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3389 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003390 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003391 for (unsigned i = 0; i < NumElems; i++)
3392 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003393 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003394 &MaskVec[0], MaskVec.size());
3395 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3396 DAG.getNode(ISD::UNDEF, VT), Mask);
3397 }
3398 }
3399
Chris Lattner66a4dda2008-03-09 05:42:06 +00003400 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3401 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003402 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003403
Dan Gohman21463242007-07-24 22:55:08 +00003404 // A vector full of immediates; various special cases are already
3405 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003406 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003407 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003408
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003409 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003410 if (EVTBits == 64) {
3411 if (NumNonZero == 1) {
3412 // One half is zero or undef.
3413 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003414 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003415 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003416 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3417 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003418 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003419 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003420 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003421
3422 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3423 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003424 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003425 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003426 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003427 }
3428
3429 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003430 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003431 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003432 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003433 }
3434
3435 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003436 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003437 V.resize(NumElems);
3438 if (NumElems == 4 && NumZero > 0) {
3439 for (unsigned i = 0; i < 4; ++i) {
3440 bool isZero = !(NonZeros & (1 << i));
3441 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003442 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003443 else
3444 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3445 }
3446
3447 for (unsigned i = 0; i < 2; ++i) {
3448 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3449 default: break;
3450 case 0:
3451 V[i] = V[i*2]; // Must be a zero vector.
3452 break;
3453 case 1:
3454 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3455 getMOVLMask(NumElems, DAG));
3456 break;
3457 case 2:
3458 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3459 getMOVLMask(NumElems, DAG));
3460 break;
3461 case 3:
3462 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3463 getUnpacklMask(NumElems, DAG));
3464 break;
3465 }
3466 }
3467
Duncan Sands92c43912008-06-06 12:08:01 +00003468 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3469 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003470 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003471 bool Reverse = (NonZeros & 0x3) == 2;
3472 for (unsigned i = 0; i < 2; ++i)
3473 if (Reverse)
3474 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3475 else
3476 MaskVec.push_back(DAG.getConstant(i, EVT));
3477 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3478 for (unsigned i = 0; i < 2; ++i)
3479 if (Reverse)
3480 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3481 else
3482 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003483 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003484 &MaskVec[0], MaskVec.size());
3485 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3486 }
3487
3488 if (Values.size() > 2) {
3489 // Expand into a number of unpckl*.
3490 // e.g. for v4f32
3491 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3492 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3493 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003494 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003495 for (unsigned i = 0; i < NumElems; ++i)
3496 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3497 NumElems >>= 1;
3498 while (NumElems != 0) {
3499 for (unsigned i = 0; i < NumElems; ++i)
3500 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3501 UnpckMask);
3502 NumElems >>= 1;
3503 }
3504 return V[0];
3505 }
3506
Dan Gohman8181bd12008-07-27 21:46:04 +00003507 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003508}
3509
Evan Chengfca29242007-12-07 08:07:39 +00003510static
Dan Gohman8181bd12008-07-27 21:46:04 +00003511SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003512 SDValue PermMask, SelectionDAG &DAG,
3513 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003514 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003515 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3516 MVT MaskEVT = MaskVT.getVectorElementType();
3517 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003518 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3519 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003520
3521 // First record which half of which vector the low elements come from.
3522 SmallVector<unsigned, 4> LowQuad(4);
3523 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003524 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003525 if (Elt.getOpcode() == ISD::UNDEF)
3526 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003527 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003528 int QuadIdx = EltIdx / 4;
3529 ++LowQuad[QuadIdx];
3530 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003531
Evan Cheng75184a92007-12-11 01:46:18 +00003532 int BestLowQuad = -1;
3533 unsigned MaxQuad = 1;
3534 for (unsigned i = 0; i < 4; ++i) {
3535 if (LowQuad[i] > MaxQuad) {
3536 BestLowQuad = i;
3537 MaxQuad = LowQuad[i];
3538 }
Evan Chengfca29242007-12-07 08:07:39 +00003539 }
3540
Evan Cheng75184a92007-12-11 01:46:18 +00003541 // Record which half of which vector the high elements come from.
3542 SmallVector<unsigned, 4> HighQuad(4);
3543 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003544 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003545 if (Elt.getOpcode() == ISD::UNDEF)
3546 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003547 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003548 int QuadIdx = EltIdx / 4;
3549 ++HighQuad[QuadIdx];
3550 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003551
Evan Cheng75184a92007-12-11 01:46:18 +00003552 int BestHighQuad = -1;
3553 MaxQuad = 1;
3554 for (unsigned i = 0; i < 4; ++i) {
3555 if (HighQuad[i] > MaxQuad) {
3556 BestHighQuad = i;
3557 MaxQuad = HighQuad[i];
3558 }
3559 }
3560
3561 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3562 if (BestLowQuad != -1 || BestHighQuad != -1) {
3563 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003564 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003565
Evan Cheng75184a92007-12-11 01:46:18 +00003566 if (BestLowQuad != -1)
3567 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3568 else
3569 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003570
Evan Cheng75184a92007-12-11 01:46:18 +00003571 if (BestHighQuad != -1)
3572 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3573 else
3574 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003575
Dan Gohman8181bd12008-07-27 21:46:04 +00003576 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003577 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3578 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3579 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3580 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3581
3582 // Now sort high and low parts separately.
3583 BitVector InOrder(8);
3584 if (BestLowQuad != -1) {
3585 // Sort lower half in order using PSHUFLW.
3586 MaskVec.clear();
3587 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003588
Evan Cheng75184a92007-12-11 01:46:18 +00003589 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003590 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003591 if (Elt.getOpcode() == ISD::UNDEF) {
3592 MaskVec.push_back(Elt);
3593 InOrder.set(i);
3594 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003595 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003596 if (EltIdx != i)
3597 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003598
Evan Cheng75184a92007-12-11 01:46:18 +00003599 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003600
Evan Cheng75184a92007-12-11 01:46:18 +00003601 // If this element is in the right place after this shuffle, then
3602 // remember it.
3603 if ((int)(EltIdx / 4) == BestLowQuad)
3604 InOrder.set(i);
3605 }
3606 }
3607 if (AnyOutOrder) {
3608 for (unsigned i = 4; i != 8; ++i)
3609 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003610 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003611 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3612 }
3613 }
3614
3615 if (BestHighQuad != -1) {
3616 // Sort high half in order using PSHUFHW if possible.
3617 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003618
Evan Cheng75184a92007-12-11 01:46:18 +00003619 for (unsigned i = 0; i != 4; ++i)
3620 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003621
Evan Cheng75184a92007-12-11 01:46:18 +00003622 bool AnyOutOrder = false;
3623 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003624 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003625 if (Elt.getOpcode() == ISD::UNDEF) {
3626 MaskVec.push_back(Elt);
3627 InOrder.set(i);
3628 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003629 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003630 if (EltIdx != i)
3631 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003632
Evan Cheng75184a92007-12-11 01:46:18 +00003633 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003634
Evan Cheng75184a92007-12-11 01:46:18 +00003635 // If this element is in the right place after this shuffle, then
3636 // remember it.
3637 if ((int)(EltIdx / 4) == BestHighQuad)
3638 InOrder.set(i);
3639 }
3640 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003641
Evan Cheng75184a92007-12-11 01:46:18 +00003642 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003643 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003644 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3645 }
3646 }
3647
3648 // The other elements are put in the right place using pextrw and pinsrw.
3649 for (unsigned i = 0; i != 8; ++i) {
3650 if (InOrder[i])
3651 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003652 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003653 if (Elt.getOpcode() == ISD::UNDEF)
3654 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003655 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003656 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003657 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3658 DAG.getConstant(EltIdx, PtrVT))
3659 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3660 DAG.getConstant(EltIdx - 8, PtrVT));
3661 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3662 DAG.getConstant(i, PtrVT));
3663 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003664
Evan Cheng75184a92007-12-11 01:46:18 +00003665 return NewV;
3666 }
3667
Bill Wendling2c7cd592008-08-21 22:35:37 +00003668 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3669 // few as possible. First, let's find out how many elements are already in the
3670 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003671 unsigned V1InOrder = 0;
3672 unsigned V1FromV1 = 0;
3673 unsigned V2InOrder = 0;
3674 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003675 SmallVector<SDValue, 8> V1Elts;
3676 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003677 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003678 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003679 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003680 V1Elts.push_back(Elt);
3681 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003682 ++V1InOrder;
3683 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003684 continue;
3685 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003686 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003687 if (EltIdx == i) {
3688 V1Elts.push_back(Elt);
3689 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3690 ++V1InOrder;
3691 } else if (EltIdx == i+8) {
3692 V1Elts.push_back(Elt);
3693 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3694 ++V2InOrder;
3695 } else if (EltIdx < 8) {
3696 V1Elts.push_back(Elt);
Mon P Wang75498182009-01-28 23:11:14 +00003697 V2Elts.push_back(DAG.getConstant(EltIdx+8, MaskEVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003698 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003699 } else {
Mon P Wang532c9632008-12-23 04:03:27 +00003700 V1Elts.push_back(Elt);
Evan Cheng75184a92007-12-11 01:46:18 +00003701 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3702 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003703 }
3704 }
3705
3706 if (V2InOrder > V1InOrder) {
3707 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3708 std::swap(V1, V2);
3709 std::swap(V1Elts, V2Elts);
3710 std::swap(V1FromV1, V2FromV2);
3711 }
3712
Evan Cheng75184a92007-12-11 01:46:18 +00003713 if ((V1FromV1 + V1InOrder) != 8) {
3714 // Some elements are from V2.
3715 if (V1FromV1) {
3716 // If there are elements that are from V1 but out of place,
3717 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003718 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003719 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003720 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003721 if (Elt.getOpcode() == ISD::UNDEF) {
3722 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3723 continue;
3724 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003725 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003726 if (EltIdx >= 8)
3727 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3728 else
3729 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3730 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003731 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003732 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003733 }
Evan Cheng75184a92007-12-11 01:46:18 +00003734
3735 NewV = V1;
3736 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003737 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003738 if (Elt.getOpcode() == ISD::UNDEF)
3739 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003740 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003741 if (EltIdx < 8)
3742 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003743 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003744 DAG.getConstant(EltIdx - 8, PtrVT));
3745 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3746 DAG.getConstant(i, PtrVT));
3747 }
3748 return NewV;
3749 } else {
3750 // All elements are from V1.
3751 NewV = V1;
3752 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003753 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003754 if (Elt.getOpcode() == ISD::UNDEF)
3755 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003756 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003757 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003758 DAG.getConstant(EltIdx, PtrVT));
3759 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3760 DAG.getConstant(i, PtrVT));
3761 }
3762 return NewV;
3763 }
3764}
3765
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003766/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3767/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3768/// done when every pair / quad of shuffle mask elements point to elements in
3769/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003770/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3771static
Dan Gohman8181bd12008-07-27 21:46:04 +00003772SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003773 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003774 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003775 TargetLowering &TLI) {
3776 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003777 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003778 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003779 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003780 MVT NewVT = MaskVT;
3781 switch (VT.getSimpleVT()) {
3782 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003783 case MVT::v4f32: NewVT = MVT::v2f64; break;
3784 case MVT::v4i32: NewVT = MVT::v2i64; break;
3785 case MVT::v8i16: NewVT = MVT::v4i32; break;
3786 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003787 }
3788
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003789 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003790 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003791 NewVT = MVT::v2i64;
3792 else
3793 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003794 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003795 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003796 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003797 for (unsigned i = 0; i < NumElems; i += Scale) {
3798 unsigned StartIdx = ~0U;
3799 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003800 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003801 if (Elt.getOpcode() == ISD::UNDEF)
3802 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003803 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003804 if (StartIdx == ~0U)
3805 StartIdx = EltIdx - (EltIdx % Scale);
3806 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003807 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003808 }
3809 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003810 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003811 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003812 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003813 }
3814
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003815 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3816 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3817 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3818 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3819 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003820}
3821
Evan Chenge9b9c672008-05-09 21:53:03 +00003822/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003823///
Dan Gohman8181bd12008-07-27 21:46:04 +00003824static SDValue getVZextMovL(MVT VT, MVT OpVT,
3825 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003826 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003827 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3828 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003829 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003830 LD = dyn_cast<LoadSDNode>(SrcOp);
3831 if (!LD) {
3832 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3833 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003834 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003835 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3836 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3837 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3838 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3839 // PR2108
3840 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3841 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003842 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003843 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003844 SrcOp.getOperand(0)
3845 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003846 }
3847 }
3848 }
3849
3850 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003851 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003852 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3853}
3854
Evan Chengf50554e2008-07-22 21:13:36 +00003855/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3856/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003857static SDValue
3858LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3859 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003860 MVT MaskVT = PermMask.getValueType();
3861 MVT MaskEVT = MaskVT.getVectorElementType();
3862 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003863 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003864 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003865 unsigned NumHi = 0;
3866 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003867 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003868 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003869 if (Elt.getOpcode() == ISD::UNDEF) {
3870 Locs[i] = std::make_pair(-1, -1);
3871 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003872 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003873 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003874 if (Val < 4) {
3875 Locs[i] = std::make_pair(0, NumLo);
3876 Mask1[NumLo] = Elt;
3877 NumLo++;
3878 } else {
3879 Locs[i] = std::make_pair(1, NumHi);
3880 if (2+NumHi < 4)
3881 Mask1[2+NumHi] = Elt;
3882 NumHi++;
3883 }
3884 }
3885 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003886
Evan Chengf50554e2008-07-22 21:13:36 +00003887 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003888 // If no more than two elements come from either vector. This can be
3889 // implemented with two shuffles. First shuffle gather the elements.
3890 // The second shuffle, which takes the first shuffle as both of its
3891 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003892 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3893 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3894 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003895
Dan Gohman8181bd12008-07-27 21:46:04 +00003896 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003897 for (unsigned i = 0; i != 4; ++i) {
3898 if (Locs[i].first == -1)
3899 continue;
3900 else {
3901 unsigned Idx = (i < 2) ? 0 : 4;
3902 Idx += Locs[i].first * 2 + Locs[i].second;
3903 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3904 }
3905 }
3906
3907 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3908 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3909 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003910 } else if (NumLo == 3 || NumHi == 3) {
3911 // Otherwise, we must have three elements from one vector, call it X, and
3912 // one element from the other, call it Y. First, use a shufps to build an
3913 // intermediate vector with the one element from Y and the element from X
3914 // that will be in the same half in the final destination (the indexes don't
3915 // matter). Then, use a shufps to build the final vector, taking the half
3916 // containing the element from Y from the intermediate, and the other half
3917 // from X.
3918 if (NumHi == 3) {
3919 // Normalize it so the 3 elements come from V1.
3920 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3921 std::swap(V1, V2);
3922 }
3923
3924 // Find the element from V2.
3925 unsigned HiIndex;
3926 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003927 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003928 if (Elt.getOpcode() == ISD::UNDEF)
3929 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003930 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003931 if (Val >= 4)
3932 break;
3933 }
3934
3935 Mask1[0] = PermMask.getOperand(HiIndex);
3936 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3937 Mask1[2] = PermMask.getOperand(HiIndex^1);
3938 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3939 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3940 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3941
3942 if (HiIndex >= 2) {
3943 Mask1[0] = PermMask.getOperand(0);
3944 Mask1[1] = PermMask.getOperand(1);
3945 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3946 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3947 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3948 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3949 } else {
3950 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3951 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3952 Mask1[2] = PermMask.getOperand(2);
3953 Mask1[3] = PermMask.getOperand(3);
3954 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003955 Mask1[2] =
3956 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3957 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003958 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003959 Mask1[3] =
3960 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3961 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003962 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3963 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3964 }
Evan Chengf50554e2008-07-22 21:13:36 +00003965 }
3966
3967 // Break it into (shuffle shuffle_hi, shuffle_lo).
3968 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003969 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3970 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3971 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003972 unsigned MaskIdx = 0;
3973 unsigned LoIdx = 0;
3974 unsigned HiIdx = 2;
3975 for (unsigned i = 0; i != 4; ++i) {
3976 if (i == 2) {
3977 MaskPtr = &HiMask;
3978 MaskIdx = 1;
3979 LoIdx = 0;
3980 HiIdx = 2;
3981 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003982 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003983 if (Elt.getOpcode() == ISD::UNDEF) {
3984 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003985 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003986 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3987 (*MaskPtr)[LoIdx] = Elt;
3988 LoIdx++;
3989 } else {
3990 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3991 (*MaskPtr)[HiIdx] = Elt;
3992 HiIdx++;
3993 }
3994 }
3995
Dan Gohman8181bd12008-07-27 21:46:04 +00003996 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003997 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3998 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003999 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00004000 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4001 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004002 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00004003 for (unsigned i = 0; i != 4; ++i) {
4004 if (Locs[i].first == -1) {
4005 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
4006 } else {
4007 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
4008 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
4009 }
4010 }
4011 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
4012 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4013 &MaskOps[0], MaskOps.size()));
4014}
4015
Dan Gohman8181bd12008-07-27 21:46:04 +00004016SDValue
4017X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
4018 SDValue V1 = Op.getOperand(0);
4019 SDValue V2 = Op.getOperand(1);
4020 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00004021 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004022 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00004023 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004024 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4025 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
4026 bool V1IsSplat = false;
4027 bool V2IsSplat = false;
4028
Gabor Greif1c80d112008-08-28 21:40:38 +00004029 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004030 return DAG.getNode(ISD::UNDEF, VT);
4031
Gabor Greif1c80d112008-08-28 21:40:38 +00004032 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00004033 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004034
Gabor Greif1c80d112008-08-28 21:40:38 +00004035 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004036 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00004037 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004038 return V2;
4039
Evan Chengae6c9212008-09-25 23:35:16 +00004040 // Canonicalize movddup shuffles.
4041 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Chengbdd9d9f2008-10-06 21:13:08 +00004042 VT.getSizeInBits() == 128 &&
Evan Chengae6c9212008-09-25 23:35:16 +00004043 X86::isMOVDDUPMask(PermMask.getNode()))
4044 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
4045
Gabor Greif1c80d112008-08-28 21:40:38 +00004046 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004047 if (isMMX || NumElems < 4) return Op;
4048 // Promote it to a v4{if}32 splat.
4049 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004050 }
4051
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004052 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4053 // do it!
4054 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004055 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004056 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004057 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
4058 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4059 // FIXME: Figure out a cleaner way to do this.
4060 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004061 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004062 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004063 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004064 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004065 SDValue NewV1 = NewOp.getOperand(0);
4066 SDValue NewV2 = NewOp.getOperand(1);
4067 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00004068 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004069 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00004070 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004071 }
4072 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004073 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004074 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004075 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004076 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004077 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00004078 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004079 }
4080 }
4081
Evan Chengdea99362008-05-29 08:22:04 +00004082 // Check if this can be converted into a logical shift.
4083 bool isLeft = false;
4084 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004085 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00004086 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4087 if (isShift && ShVal.hasOneUse()) {
4088 // If the shifted value has multiple uses, it may be cheaper to use
4089 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004090 MVT EVT = VT.getVectorElementType();
4091 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004092 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4093 }
4094
Gabor Greif1c80d112008-08-28 21:40:38 +00004095 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004096 if (V1IsUndef)
4097 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004098 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004099 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004100 if (!isMMX)
4101 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004102 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004103
Gabor Greif1c80d112008-08-28 21:40:38 +00004104 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4105 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4106 X86::isMOVHLPSMask(PermMask.getNode()) ||
4107 X86::isMOVHPMask(PermMask.getNode()) ||
4108 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004109 return Op;
4110
Gabor Greif1c80d112008-08-28 21:40:38 +00004111 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4112 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004113 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4114
Evan Chengdea99362008-05-29 08:22:04 +00004115 if (isShift) {
4116 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004117 MVT EVT = VT.getVectorElementType();
4118 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004119 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4120 }
4121
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004122 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004123 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4124 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004125 V1IsSplat = isSplatVector(V1.getNode());
4126 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004127
4128 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004129 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4130 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4131 std::swap(V1IsSplat, V2IsSplat);
4132 std::swap(V1IsUndef, V2IsUndef);
4133 Commuted = true;
4134 }
4135
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004136 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004137 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004138 if (V2IsUndef) return V1;
4139 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4140 if (V2IsSplat) {
4141 // V2 is a splat, so the mask may be malformed. That is, it may point
4142 // to any V2 element. The instruction selectior won't like this. Get
4143 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004144 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004145 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004146 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4147 }
4148 return Op;
4149 }
4150
Gabor Greif1c80d112008-08-28 21:40:38 +00004151 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4152 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4153 X86::isUNPCKLMask(PermMask.getNode()) ||
4154 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004155 return Op;
4156
4157 if (V2IsSplat) {
4158 // Normalize mask so all entries that point to V2 points to its first
4159 // element then try to match unpck{h|l} again. If match, return a
4160 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004161 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004162 if (NewMask.getNode() != PermMask.getNode()) {
4163 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004164 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004165 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004166 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004167 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004168 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4169 }
4170 }
4171 }
4172
4173 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004174 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004175 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4176
4177 if (Commuted) {
4178 // Commute is back and try unpck* again.
4179 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004180 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4181 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4182 X86::isUNPCKLMask(PermMask.getNode()) ||
4183 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004184 return Op;
4185 }
4186
Evan Chengbf8b2c52008-04-05 00:30:36 +00004187 // Try PSHUF* first, then SHUFP*.
4188 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4189 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004190 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004191 if (V2.getOpcode() != ISD::UNDEF)
4192 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4193 DAG.getNode(ISD::UNDEF, VT), PermMask);
4194 return Op;
4195 }
4196
4197 if (!isMMX) {
4198 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004199 (X86::isPSHUFDMask(PermMask.getNode()) ||
4200 X86::isPSHUFHWMask(PermMask.getNode()) ||
4201 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004202 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004203 if (VT == MVT::v4f32) {
4204 RVT = MVT::v4i32;
4205 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4206 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4207 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4208 } else if (V2.getOpcode() != ISD::UNDEF)
4209 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4210 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4211 if (RVT != VT)
4212 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004213 return Op;
4214 }
4215
Evan Chengbf8b2c52008-04-05 00:30:36 +00004216 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004217 if (X86::isSHUFPMask(PermMask.getNode()) ||
4218 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004219 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004220 }
4221
Evan Cheng75184a92007-12-11 01:46:18 +00004222 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4223 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004224 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004225 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004226 return NewOp;
4227 }
4228
Evan Chengf50554e2008-07-22 21:13:36 +00004229 // Handle all 4 wide cases with a number of shuffles except for MMX.
4230 if (NumElems == 4 && !isMMX)
4231 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004232
Dan Gohman8181bd12008-07-27 21:46:04 +00004233 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004234}
4235
Dan Gohman8181bd12008-07-27 21:46:04 +00004236SDValue
4237X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004238 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004239 MVT VT = Op.getValueType();
4240 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004241 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004242 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004243 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004244 DAG.getValueType(VT));
4245 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004246 } else if (VT.getSizeInBits() == 16) {
Evan Chengf9393b32009-01-02 05:29:08 +00004247 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4248 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4249 if (Idx == 0)
4250 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4251 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4252 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32,
4253 Op.getOperand(0)),
4254 Op.getOperand(1)));
Dan Gohman8181bd12008-07-27 21:46:04 +00004255 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004256 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004257 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004258 DAG.getValueType(VT));
4259 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004260 } else if (VT == MVT::f32) {
4261 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4262 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004263 // result has a single use which is a store or a bitcast to i32. And in
4264 // the case of a store, it's not worth it if the index is a constant 0,
4265 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004266 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004267 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004268 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004269 if ((User->getOpcode() != ISD::STORE ||
4270 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4271 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004272 (User->getOpcode() != ISD::BIT_CONVERT ||
4273 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004274 return SDValue();
4275 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004276 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4277 Op.getOperand(1));
4278 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004279 } else if (VT == MVT::i32) {
4280 // ExtractPS works with constant index.
4281 if (isa<ConstantSDNode>(Op.getOperand(1)))
4282 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004283 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004284 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004285}
4286
4287
Dan Gohman8181bd12008-07-27 21:46:04 +00004288SDValue
4289X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004290 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004291 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004292
Evan Cheng6c249332008-03-24 21:52:23 +00004293 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004294 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004295 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004296 return Res;
4297 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004298
Duncan Sands92c43912008-06-06 12:08:01 +00004299 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004300 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004301 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004302 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004303 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004304 if (Idx == 0)
4305 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4306 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4307 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4308 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004309 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004310 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004311 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004312 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004313 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004314 DAG.getValueType(VT));
4315 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004316 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004317 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004318 if (Idx == 0)
4319 return Op;
4320 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004321 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004322 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004323 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004324 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004325 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004326 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004327 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004328 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004329 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004330 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004331 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004332 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004333 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004334 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4335 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4336 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004337 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004338 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004339 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4340 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4341 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004342 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343 if (Idx == 0)
4344 return Op;
4345
4346 // UNPCKHPD the element to the lowest double word, then movsd.
4347 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4348 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004349 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004350 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004351 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004352 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004353 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004354 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004355 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004356 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004357 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4358 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4359 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004360 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004361 }
4362
Dan Gohman8181bd12008-07-27 21:46:04 +00004363 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004364}
4365
Dan Gohman8181bd12008-07-27 21:46:04 +00004366SDValue
4367X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004368 MVT VT = Op.getValueType();
4369 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004370
Dan Gohman8181bd12008-07-27 21:46:04 +00004371 SDValue N0 = Op.getOperand(0);
4372 SDValue N1 = Op.getOperand(1);
4373 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004374
Dan Gohman5a7af042008-08-14 22:53:18 +00004375 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4376 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004377 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004378 : X86ISD::PINSRW;
4379 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4380 // argument.
4381 if (N1.getValueType() != MVT::i32)
4382 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4383 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004384 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begemand77e59e2008-02-11 04:19:36 +00004385 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004386 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004387 // Bits [7:6] of the constant are the source select. This will always be
4388 // zero here. The DAG Combiner may combine an extract_elt index into these
4389 // bits. For example (insert (extract, 3), 2) could be matched by putting
4390 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4391 // Bits [5:4] of the constant are the destination select. This is the
4392 // value of the incoming immediate.
4393 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4394 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004395 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begemand77e59e2008-02-11 04:19:36 +00004396 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004397 } else if (EVT == MVT::i32) {
4398 // InsertPS works with constant index.
4399 if (isa<ConstantSDNode>(N2))
4400 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004401 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004402 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004403}
4404
Dan Gohman8181bd12008-07-27 21:46:04 +00004405SDValue
4406X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004407 MVT VT = Op.getValueType();
4408 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004409
4410 if (Subtarget->hasSSE41())
4411 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4412
Evan Chenge12a7eb2007-12-12 07:55:34 +00004413 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004414 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004415
Dan Gohman8181bd12008-07-27 21:46:04 +00004416 SDValue N0 = Op.getOperand(0);
4417 SDValue N1 = Op.getOperand(1);
4418 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004419
Duncan Sands92c43912008-06-06 12:08:01 +00004420 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004421 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4422 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423 if (N1.getValueType() != MVT::i32)
4424 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4425 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004426 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004427 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004428 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004429 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004430}
4431
Dan Gohman8181bd12008-07-27 21:46:04 +00004432SDValue
4433X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004434 if (Op.getValueType() == MVT::v2f32)
4435 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4436 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4437 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4438 Op.getOperand(0))));
4439
Dan Gohman8181bd12008-07-27 21:46:04 +00004440 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004441 MVT VT = MVT::v2i32;
4442 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004443 default: break;
4444 case MVT::v16i8:
4445 case MVT::v8i16:
4446 VT = MVT::v4i32;
4447 break;
4448 }
4449 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4450 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004451}
4452
Bill Wendlingfef06052008-09-16 21:48:12 +00004453// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4454// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4455// one of the above mentioned nodes. It has to be wrapped because otherwise
4456// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4457// be used to form addressing mode. These wrapped nodes will be selected
4458// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004459SDValue
4460X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004461 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004462 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004463 getPointerTy(),
4464 CP->getAlignment());
4465 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4466 // With PIC, the address is actually $g + Offset.
4467 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4468 !Subtarget->isPICStyleRIPRel()) {
4469 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4470 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4471 Result);
4472 }
4473
4474 return Result;
4475}
4476
Dan Gohman8181bd12008-07-27 21:46:04 +00004477SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004478X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
Dan Gohman36322c72008-10-18 02:06:02 +00004479 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00004480 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00004481 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4482 bool ExtraLoadRequired =
4483 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4484
4485 // Create the TargetGlobalAddress node, folding in the constant
4486 // offset if it is legal.
4487 SDValue Result;
Dan Gohman3d5257c2008-10-21 03:38:42 +00004488 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +00004489 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4490 Offset = 0;
4491 } else
4492 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004493 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00004494
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004495 // With PIC, the address is actually $g + Offset.
Dan Gohman36322c72008-10-18 02:06:02 +00004496 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004497 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4498 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4499 Result);
4500 }
4501
4502 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4503 // load the value at address GV, not the value of GV itself. This means that
4504 // the GlobalAddress must be in the base or index register of the address, not
4505 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4506 // The same applies for external symbols during PIC codegen
Dan Gohman36322c72008-10-18 02:06:02 +00004507 if (ExtraLoadRequired)
Dan Gohman12a9c082008-02-06 22:27:42 +00004508 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004509 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004510
Dan Gohman36322c72008-10-18 02:06:02 +00004511 // If there was a non-zero offset that we didn't fold, create an explicit
4512 // addition for it.
4513 if (Offset != 0)
4514 Result = DAG.getNode(ISD::ADD, getPointerTy(), Result,
4515 DAG.getConstant(Offset, getPointerTy()));
4516
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004517 return Result;
4518}
4519
Evan Cheng7f250d62008-09-24 00:05:32 +00004520SDValue
4521X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4522 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00004523 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
4524 return LowerGlobalAddress(GV, Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00004525}
4526
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004527// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004528static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004529LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004530 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004531 SDValue InFlag;
4532 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004533 DAG.getNode(X86ISD::GlobalBaseReg,
4534 PtrVT), InFlag);
4535 InFlag = Chain.getValue(1);
4536
4537 // emit leal symbol@TLSGD(,%ebx,1), %eax
4538 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004539 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004540 GA->getValueType(0),
4541 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004542 SDValue Ops[] = { Chain, TGA, InFlag };
4543 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004544 InFlag = Result.getValue(2);
4545 Chain = Result.getValue(1);
4546
4547 // call ___tls_get_addr. This function receives its argument in
4548 // the register EAX.
4549 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4550 InFlag = Chain.getValue(1);
4551
4552 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004553 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004554 DAG.getTargetExternalSymbol("___tls_get_addr",
4555 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004556 DAG.getRegister(X86::EAX, PtrVT),
4557 DAG.getRegister(X86::EBX, PtrVT),
4558 InFlag };
4559 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4560 InFlag = Chain.getValue(1);
4561
4562 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4563}
4564
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004565// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004566static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004567LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004568 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004569 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004570
4571 // emit leaq symbol@TLSGD(%rip), %rdi
4572 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004573 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004574 GA->getValueType(0),
4575 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004576 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4577 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004578 Chain = Result.getValue(1);
4579 InFlag = Result.getValue(2);
4580
aslb204cd52008-08-16 12:58:29 +00004581 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004582 // the register RDI.
4583 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4584 InFlag = Chain.getValue(1);
4585
4586 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004587 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004588 DAG.getTargetExternalSymbol("__tls_get_addr",
4589 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004590 DAG.getRegister(X86::RDI, PtrVT),
4591 InFlag };
4592 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4593 InFlag = Chain.getValue(1);
4594
4595 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4596}
4597
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004598// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4599// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004600static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004601 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004602 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004603 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004604 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4605 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004606 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004607 GA->getValueType(0),
4608 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004609 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004610
4611 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004612 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004613 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004614
4615 // The address of the thread local variable is the add of the thread
4616 // pointer with the offset of the variable.
4617 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4618}
4619
Dan Gohman8181bd12008-07-27 21:46:04 +00004620SDValue
4621X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004622 // TODO: implement the "local dynamic" model
4623 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004624 assert(Subtarget->isTargetELF() &&
4625 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004626 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4627 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4628 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004629 if (Subtarget->is64Bit()) {
4630 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4631 } else {
4632 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4633 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4634 else
4635 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4636 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004637}
4638
Dan Gohman8181bd12008-07-27 21:46:04 +00004639SDValue
4640X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004641 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4642 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004643 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4644 // With PIC, the address is actually $g + Offset.
4645 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4646 !Subtarget->isPICStyleRIPRel()) {
4647 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4648 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4649 Result);
4650 }
4651
4652 return Result;
4653}
4654
Dan Gohman8181bd12008-07-27 21:46:04 +00004655SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004656 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004657 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004658 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4659 // With PIC, the address is actually $g + Offset.
4660 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4661 !Subtarget->isPICStyleRIPRel()) {
4662 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4663 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4664 Result);
4665 }
4666
4667 return Result;
4668}
4669
Chris Lattner62814a32007-10-17 06:02:13 +00004670/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4671/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004672SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004673 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004674 MVT VT = Op.getValueType();
4675 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004676 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004677 SDValue ShOpLo = Op.getOperand(0);
4678 SDValue ShOpHi = Op.getOperand(1);
4679 SDValue ShAmt = Op.getOperand(2);
4680 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004681 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4682 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004683
Dan Gohman8181bd12008-07-27 21:46:04 +00004684 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004685 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004686 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4687 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004688 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004689 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4690 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004691 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004692
Dan Gohman8181bd12008-07-27 21:46:04 +00004693 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004694 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004695 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004696 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004697
Dan Gohman8181bd12008-07-27 21:46:04 +00004698 SDValue Hi, Lo;
4699 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4700 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4701 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004702
Chris Lattner62814a32007-10-17 06:02:13 +00004703 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004704 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4705 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004706 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004707 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4708 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004709 }
4710
Dan Gohman8181bd12008-07-27 21:46:04 +00004711 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004712 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004713}
4714
Dan Gohman8181bd12008-07-27 21:46:04 +00004715SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004716 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004717 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004718 "Unknown SINT_TO_FP to lower!");
4719
4720 // These are really Legal; caller falls through into that case.
4721 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004722 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004723 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4724 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004725 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004726
Duncan Sands92c43912008-06-06 12:08:01 +00004727 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004728 MachineFunction &MF = DAG.getMachineFunction();
4729 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004730 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4731 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004732 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004733 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004734
4735 // Build the FILD
4736 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004737 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004738 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004739 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4740 else
4741 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004742 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004743 Ops.push_back(Chain);
4744 Ops.push_back(StackSlot);
4745 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004746 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004747 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004748
Dale Johannesen2fc20782007-09-14 22:26:36 +00004749 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004750 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004751 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004752
4753 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4754 // shouldn't be necessary except that RFP cannot be live across
4755 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4756 MachineFunction &MF = DAG.getMachineFunction();
4757 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004758 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004759 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004760 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004761 Ops.push_back(Chain);
4762 Ops.push_back(Result);
4763 Ops.push_back(StackSlot);
4764 Ops.push_back(DAG.getValueType(Op.getValueType()));
4765 Ops.push_back(InFlag);
4766 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004767 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004768 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004769 }
4770
4771 return Result;
4772}
4773
Bill Wendling14a30ef2009-01-17 03:56:04 +00004774// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4775SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4776 // This algorithm is not obvious. Here it is in C code, more or less:
4777 /*
4778 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4779 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4780 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesenfb019af2008-10-21 23:07:49 +00004781
Bill Wendling14a30ef2009-01-17 03:56:04 +00004782 // Copy ints to xmm registers.
4783 __m128i xh = _mm_cvtsi32_si128( hi );
4784 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004785
Bill Wendling14a30ef2009-01-17 03:56:04 +00004786 // Combine into low half of a single xmm register.
4787 __m128i x = _mm_unpacklo_epi32( xh, xl );
4788 __m128d d;
4789 double sd;
Dale Johannesenfb019af2008-10-21 23:07:49 +00004790
Bill Wendling14a30ef2009-01-17 03:56:04 +00004791 // Merge in appropriate exponents to give the integer bits the right
4792 // magnitude.
4793 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004794
Bill Wendling14a30ef2009-01-17 03:56:04 +00004795 // Subtract away the biases to deal with the IEEE-754 double precision
4796 // implicit 1.
4797 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesenfb019af2008-10-21 23:07:49 +00004798
Bill Wendling14a30ef2009-01-17 03:56:04 +00004799 // All conversions up to here are exact. The correctly rounded result is
4800 // calculated using the current rounding mode using the following
4801 // horizontal add.
4802 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4803 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4804 // store doesn't really need to be here (except
4805 // maybe to zero the other double)
4806 return sd;
4807 }
4808 */
Dale Johannesenfb019af2008-10-21 23:07:49 +00004809
Dale Johannesena359b8b2008-10-21 20:50:01 +00004810 // Build some magic constants.
Bill Wendling14a30ef2009-01-17 03:56:04 +00004811 std::vector<Constant*> CV0;
Dale Johannesena359b8b2008-10-21 20:50:01 +00004812 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4813 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4814 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4815 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4816 Constant *C0 = ConstantVector::get(CV0);
4817 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 4);
4818
Bill Wendling14a30ef2009-01-17 03:56:04 +00004819 std::vector<Constant*> CV1;
Dale Johannesena359b8b2008-10-21 20:50:01 +00004820 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4821 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4822 Constant *C1 = ConstantVector::get(CV1);
4823 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 4);
4824
4825 SmallVector<SDValue, 4> MaskVec;
4826 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
4827 MaskVec.push_back(DAG.getConstant(4, MVT::i32));
4828 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
4829 MaskVec.push_back(DAG.getConstant(5, MVT::i32));
4830 SDValue UnpcklMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, &MaskVec[0],
4831 MaskVec.size());
4832 SmallVector<SDValue, 4> MaskVec2;
Duncan Sandsca872ca2008-10-22 11:24:12 +00004833 MaskVec2.push_back(DAG.getConstant(1, MVT::i32));
4834 MaskVec2.push_back(DAG.getConstant(0, MVT::i32));
4835 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec2[0],
Dale Johannesena359b8b2008-10-21 20:50:01 +00004836 MaskVec2.size());
4837
4838 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004839 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4840 Op.getOperand(0),
4841 DAG.getIntPtrConstant(1)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004842 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004843 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4844 Op.getOperand(0),
4845 DAG.getIntPtrConstant(0)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004846 SDValue Unpck1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4847 XR1, XR2, UnpcklMask);
4848 SDValue CLod0 = DAG.getLoad(MVT::v4i32, DAG.getEntryNode(), CPIdx0,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004849 PseudoSourceValue::getConstantPool(), 0,
4850 false, 16);
Dale Johannesena359b8b2008-10-21 20:50:01 +00004851 SDValue Unpck2 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004852 Unpck1, CLod0, UnpcklMask);
Dale Johannesena359b8b2008-10-21 20:50:01 +00004853 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Unpck2);
4854 SDValue CLod1 = DAG.getLoad(MVT::v2f64, CLod0.getValue(1), CPIdx1,
Bill Wendling14a30ef2009-01-17 03:56:04 +00004855 PseudoSourceValue::getConstantPool(), 0,
4856 false, 16);
Dale Johannesena359b8b2008-10-21 20:50:01 +00004857 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::v2f64, XR2F, CLod1);
Bill Wendling14a30ef2009-01-17 03:56:04 +00004858
Dale Johannesena359b8b2008-10-21 20:50:01 +00004859 // Add the halves; easiest way is to swap them into another reg first.
4860 SDValue Shuf = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2f64,
4861 Sub, Sub, ShufMask);
4862 SDValue Add = DAG.getNode(ISD::FADD, MVT::v2f64, Shuf, Sub);
4863 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64, Add,
4864 DAG.getIntPtrConstant(0));
4865}
4866
Bill Wendling14a30ef2009-01-17 03:56:04 +00004867// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4868SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
4869 // FP constant to bias correct the final result.
4870 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4871 MVT::f64);
4872
4873 // Load the 32-bit value into an XMM register.
4874 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
4875 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4876 Op.getOperand(0),
4877 DAG.getIntPtrConstant(0)));
4878
4879 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64,
4880 DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Load),
4881 DAG.getIntPtrConstant(0));
4882
4883 // Or the load with the bias.
4884 SDValue Or = DAG.getNode(ISD::OR, MVT::v2i64,
4885 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64,
4886 DAG.getNode(ISD::SCALAR_TO_VECTOR,
Evan Chengc0ab5e52009-01-19 08:19:57 +00004887 MVT::v2f64, Load)),
Bill Wendling14a30ef2009-01-17 03:56:04 +00004888 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64,
4889 DAG.getNode(ISD::SCALAR_TO_VECTOR,
Evan Chengc0ab5e52009-01-19 08:19:57 +00004890 MVT::v2f64, Bias)));
Bill Wendling14a30ef2009-01-17 03:56:04 +00004891 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64,
4892 DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Or),
4893 DAG.getIntPtrConstant(0));
4894
4895 // Subtract the bias.
4896 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Or, Bias);
4897
4898 // Handle final rounding.
Bill Wendlingdb547de2009-01-17 07:40:19 +00004899 MVT DestVT = Op.getValueType();
4900
4901 if (DestVT.bitsLT(MVT::f64)) {
4902 return DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
4903 DAG.getIntPtrConstant(0));
4904 } else if (DestVT.bitsGT(MVT::f64)) {
4905 return DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
4906 }
4907
4908 // Handle final rounding.
4909 return Sub;
Bill Wendling14a30ef2009-01-17 03:56:04 +00004910}
4911
4912SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Cheng44fd2392009-01-19 08:08:22 +00004913 SDValue N0 = Op.getOperand(0);
Bill Wendling14a30ef2009-01-17 03:56:04 +00004914
Evan Cheng44fd2392009-01-19 08:08:22 +00004915 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4916 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4917 // the optimization here.
4918 if (DAG.SignBitIsZero(N0))
4919 return DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), N0);
4920
4921 MVT SrcVT = N0.getValueType();
Bill Wendling14a30ef2009-01-17 03:56:04 +00004922 if (SrcVT == MVT::i64) {
4923 // We only handle SSE2 f64 target here; caller can handle the rest.
4924 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4925 return SDValue();
Bill Wendlingdb547de2009-01-17 07:40:19 +00004926
Bill Wendling14a30ef2009-01-17 03:56:04 +00004927 return LowerUINT_TO_FP_i64(Op, DAG);
4928 } else if (SrcVT == MVT::i32) {
Bill Wendling14a30ef2009-01-17 03:56:04 +00004929 return LowerUINT_TO_FP_i32(Op, DAG);
4930 }
4931
4932 assert(0 && "Unknown UINT_TO_FP to lower!");
4933 return SDValue();
4934}
4935
Dan Gohman8181bd12008-07-27 21:46:04 +00004936std::pair<SDValue,SDValue> X86TargetLowering::
4937FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004938 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4939 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004940 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004941
Dale Johannesen2fc20782007-09-14 22:26:36 +00004942 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004943 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004944 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004945 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004946 if (Subtarget->is64Bit() &&
4947 Op.getValueType() == MVT::i64 &&
4948 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004949 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004950
Evan Cheng05441e62007-10-15 20:11:21 +00004951 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4952 // stack slot.
4953 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004954 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004955 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004956 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004957 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004958 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004959 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4960 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4961 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4962 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004963 }
4964
Dan Gohman8181bd12008-07-27 21:46:04 +00004965 SDValue Chain = DAG.getEntryNode();
4966 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004967 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004968 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004969 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004970 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004971 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004972 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004973 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4974 };
4975 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4976 Chain = Value.getValue(1);
4977 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4978 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4979 }
4980
4981 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004982 SDValue Ops[] = { Chain, Value, StackSlot };
4983 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004984
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004985 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004986}
4987
Dan Gohman8181bd12008-07-27 21:46:04 +00004988SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4989 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4990 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004991 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004992
4993 // Load the result.
4994 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4995}
4996
Dan Gohman8181bd12008-07-27 21:46:04 +00004997SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004998 MVT VT = Op.getValueType();
4999 MVT EltVT = VT;
5000 if (VT.isVector())
5001 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002 std::vector<Constant*> CV;
5003 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005004 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005005 CV.push_back(C);
5006 CV.push_back(C);
5007 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005008 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005009 CV.push_back(C);
5010 CV.push_back(C);
5011 CV.push_back(C);
5012 CV.push_back(C);
5013 }
Dan Gohman11821702007-07-27 17:16:43 +00005014 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005015 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
5016 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005017 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005018 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005019 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
5020}
5021
Dan Gohman8181bd12008-07-27 21:46:04 +00005022SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005023 MVT VT = Op.getValueType();
5024 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00005025 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00005026 if (VT.isVector()) {
5027 EltVT = VT.getVectorElementType();
5028 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00005029 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005030 std::vector<Constant*> CV;
5031 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005032 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005033 CV.push_back(C);
5034 CV.push_back(C);
5035 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005036 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005037 CV.push_back(C);
5038 CV.push_back(C);
5039 CV.push_back(C);
5040 CV.push_back(C);
5041 }
Dan Gohman11821702007-07-27 17:16:43 +00005042 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005043 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
5044 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005045 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005046 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00005047 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00005048 return DAG.getNode(ISD::BIT_CONVERT, VT,
5049 DAG.getNode(ISD::XOR, MVT::v2i64,
5050 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
5051 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
5052 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00005053 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
5054 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005055}
5056
Dan Gohman8181bd12008-07-27 21:46:04 +00005057SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
5058 SDValue Op0 = Op.getOperand(0);
5059 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00005060 MVT VT = Op.getValueType();
5061 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005062
5063 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005064 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005065 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
5066 SrcVT = VT;
5067 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005068 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005069 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00005070 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005071 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005072 }
5073
5074 // At this point the operands and the result should have the same
5075 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005076
5077 // First get the sign bit of second operand.
5078 std::vector<Constant*> CV;
5079 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005080 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
5081 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005082 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005083 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
5084 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5085 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5086 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005087 }
Dan Gohman11821702007-07-27 17:16:43 +00005088 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00005089 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
5090 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005091 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005092 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00005093 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005094
5095 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005096 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005097 // Op0 is MVT::f32, Op1 is MVT::f64.
5098 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
5099 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
5100 DAG.getConstant(32, MVT::i32));
5101 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
5102 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00005103 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005104 }
5105
5106 // Clear first operand sign bit.
5107 CV.clear();
5108 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005109 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
5110 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005111 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005112 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
5113 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5114 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5115 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005116 }
Dan Gohman11821702007-07-27 17:16:43 +00005117 C = ConstantVector::get(CV);
5118 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00005119 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005120 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005121 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00005122 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005123
5124 // Or the value with the sign bit.
5125 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
5126}
5127
Dan Gohman8181bd12008-07-27 21:46:04 +00005128SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00005129 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00005130 SDValue Op0 = Op.getOperand(0);
5131 SDValue Op1 = Op.getOperand(1);
Chris Lattner77a62312008-12-25 05:34:37 +00005132 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5133
Dan Gohman22cefb02009-01-29 01:59:02 +00005134 // Lower (X & (1 << N)) == 0 to BT(X, N).
5135 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5136 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman13dd9522009-01-13 23:25:30 +00005137 if (Op0.getOpcode() == ISD::AND &&
5138 Op0.hasOneUse() &&
5139 Op1.getOpcode() == ISD::Constant &&
Dan Gohman22cefb02009-01-29 01:59:02 +00005140 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattner77a62312008-12-25 05:34:37 +00005141 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohman22cefb02009-01-29 01:59:02 +00005142 SDValue LHS, RHS;
5143 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5144 if (ConstantSDNode *Op010C =
5145 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5146 if (Op010C->getZExtValue() == 1) {
5147 LHS = Op0.getOperand(0);
5148 RHS = Op0.getOperand(1).getOperand(1);
5149 }
5150 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5151 if (ConstantSDNode *Op000C =
5152 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5153 if (Op000C->getZExtValue() == 1) {
5154 LHS = Op0.getOperand(1);
5155 RHS = Op0.getOperand(0).getOperand(1);
5156 }
5157 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5158 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5159 SDValue AndLHS = Op0.getOperand(0);
5160 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5161 LHS = AndLHS.getOperand(0);
5162 RHS = AndLHS.getOperand(1);
5163 }
5164 }
Evan Cheng950aac02007-09-25 01:57:46 +00005165
Dan Gohman22cefb02009-01-29 01:59:02 +00005166 if (LHS.getNode()) {
Chris Lattner77a62312008-12-25 05:34:37 +00005167 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5168 // instruction. Since the shift amount is in-range-or-undefined, we know
5169 // that doing a bittest on the i16 value is ok. We extend to i32 because
5170 // the encoding for the i16 version is larger than the i32 version.
5171 if (LHS.getValueType() == MVT::i8)
5172 LHS = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, LHS);
5173
5174 // If the operand types disagree, extend the shift amount to match. Since
5175 // BT ignores high bits (like shifts) we can use anyextend.
5176 if (LHS.getValueType() != RHS.getValueType())
5177 RHS = DAG.getNode(ISD::ANY_EXTEND, LHS.getValueType(), RHS);
Dan Gohman22cefb02009-01-29 01:59:02 +00005178
Chris Lattner77a62312008-12-25 05:34:37 +00005179 SDValue BT = DAG.getNode(X86ISD::BT, MVT::i32, LHS, RHS);
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005180 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dan Gohman22cefb02009-01-29 01:59:02 +00005181 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Chris Lattner77a62312008-12-25 05:34:37 +00005182 DAG.getConstant(Cond, MVT::i8), BT);
5183 }
5184 }
5185
5186 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5187 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Chris Lattner60435922008-12-24 00:11:37 +00005188
Chris Lattner77a62312008-12-25 05:34:37 +00005189 SDValue Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Chris Lattner60435922008-12-24 00:11:37 +00005190 return DAG.getNode(X86ISD::SETCC, MVT::i8,
5191 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005192}
5193
Dan Gohman8181bd12008-07-27 21:46:04 +00005194SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5195 SDValue Cond;
5196 SDValue Op0 = Op.getOperand(0);
5197 SDValue Op1 = Op.getOperand(1);
5198 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00005199 MVT VT = Op.getValueType();
5200 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5201 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5202
5203 if (isFP) {
5204 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00005205 MVT VT0 = Op0.getValueType();
5206 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5207 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00005208 bool Swap = false;
5209
5210 switch (SetCCOpcode) {
5211 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005212 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00005213 case ISD::SETEQ: SSECC = 0; break;
5214 case ISD::SETOGT:
5215 case ISD::SETGT: Swap = true; // Fallthrough
5216 case ISD::SETLT:
5217 case ISD::SETOLT: SSECC = 1; break;
5218 case ISD::SETOGE:
5219 case ISD::SETGE: Swap = true; // Fallthrough
5220 case ISD::SETLE:
5221 case ISD::SETOLE: SSECC = 2; break;
5222 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005223 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00005224 case ISD::SETNE: SSECC = 4; break;
5225 case ISD::SETULE: Swap = true;
5226 case ISD::SETUGE: SSECC = 5; break;
5227 case ISD::SETULT: Swap = true;
5228 case ISD::SETUGT: SSECC = 6; break;
5229 case ISD::SETO: SSECC = 7; break;
5230 }
5231 if (Swap)
5232 std::swap(Op0, Op1);
5233
Nate Begeman6357f9d2008-07-25 19:05:58 +00005234 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00005235 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00005236 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005237 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005238 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5239 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5240 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
5241 }
5242 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005243 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005244 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5245 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5246 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
5247 }
5248 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00005249 }
5250 // Handle all other FP comparisons here.
5251 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
5252 }
5253
5254 // We are handling one of the integer comparisons here. Since SSE only has
5255 // GT and EQ comparisons for integer, swapping operands and multiple
5256 // operations may be required for some comparisons.
5257 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5258 bool Swap = false, Invert = false, FlipSigns = false;
5259
5260 switch (VT.getSimpleVT()) {
5261 default: break;
5262 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5263 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5264 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5265 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5266 }
5267
5268 switch (SetCCOpcode) {
5269 default: break;
5270 case ISD::SETNE: Invert = true;
5271 case ISD::SETEQ: Opc = EQOpc; break;
5272 case ISD::SETLT: Swap = true;
5273 case ISD::SETGT: Opc = GTOpc; break;
5274 case ISD::SETGE: Swap = true;
5275 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5276 case ISD::SETULT: Swap = true;
5277 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5278 case ISD::SETUGE: Swap = true;
5279 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5280 }
5281 if (Swap)
5282 std::swap(Op0, Op1);
5283
5284 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5285 // bits of the inputs before performing those operations.
5286 if (FlipSigns) {
5287 MVT EltVT = VT.getVectorElementType();
Duncan Sands505ba942009-02-01 18:06:53 +00005288 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5289 EltVT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005290 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
5291 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005292 SignBits.size());
5293 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
5294 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
5295 }
5296
Dan Gohman8181bd12008-07-27 21:46:04 +00005297 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005298
5299 // If the logical-not of the result is required, perform that now.
Bob Wilson81a42cf2009-01-22 17:39:32 +00005300 if (Invert)
Bill Wendlingfcfb47d2009-01-30 23:03:19 +00005301 Result = DAG.getNOT(Op.getDebugLoc(), Result, VT);
Bob Wilson81a42cf2009-01-22 17:39:32 +00005302
Nate Begeman03605a02008-07-17 16:51:19 +00005303 return Result;
5304}
Evan Cheng950aac02007-09-25 01:57:46 +00005305
Evan Chengd580f022008-12-03 08:38:43 +00005306// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
5307static bool isX86LogicalCmp(unsigned Opc) {
5308 return Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI;
5309}
5310
Dan Gohman8181bd12008-07-27 21:46:04 +00005311SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005312 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005313 SDValue Cond = Op.getOperand(0);
5314 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005315
5316 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005317 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005318
Evan Cheng50d37ab2007-10-08 22:16:29 +00005319 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5320 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005321 if (Cond.getOpcode() == X86ISD::SETCC) {
5322 CC = Cond.getOperand(0);
5323
Dan Gohman8181bd12008-07-27 21:46:04 +00005324 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005326 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005327
Evan Cheng50d37ab2007-10-08 22:16:29 +00005328 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005329 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005330 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005331 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005332
Dan Gohman22cefb02009-01-29 01:59:02 +00005333 if ((isX86LogicalCmp(Opc) && !IllegalFPCMov) || Opc == X86ISD::BT) { // FIXME
Evan Cheng50d37ab2007-10-08 22:16:29 +00005334 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005335 addTest = false;
5336 }
5337 }
5338
5339 if (addTest) {
5340 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00005341 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005342 }
5343
Duncan Sands92c43912008-06-06 12:08:01 +00005344 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005345 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005346 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005347 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5348 // condition is true.
5349 Ops.push_back(Op.getOperand(2));
5350 Ops.push_back(Op.getOperand(1));
5351 Ops.push_back(CC);
5352 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00005353 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005354}
5355
Evan Chengd580f022008-12-03 08:38:43 +00005356// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5357// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5358// from the AND / OR.
5359static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5360 Opc = Op.getOpcode();
5361 if (Opc != ISD::OR && Opc != ISD::AND)
5362 return false;
5363 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5364 Op.getOperand(0).hasOneUse() &&
5365 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5366 Op.getOperand(1).hasOneUse());
5367}
5368
Dan Gohman8181bd12008-07-27 21:46:04 +00005369SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005370 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005371 SDValue Chain = Op.getOperand(0);
5372 SDValue Cond = Op.getOperand(1);
5373 SDValue Dest = Op.getOperand(2);
5374 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005375
5376 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005377 Cond = LowerSETCC(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005378#if 0
5379 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingf5399032008-12-12 21:15:41 +00005380 else if (Cond.getOpcode() == X86ISD::ADD ||
5381 Cond.getOpcode() == X86ISD::SUB ||
5382 Cond.getOpcode() == X86ISD::SMUL ||
5383 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling7e04be62008-12-09 22:08:41 +00005384 Cond = LowerXALUO(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005385#endif
5386
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();
Chris Lattner77a62312008-12-25 05:34:37 +00005394 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
5395 if (isX86LogicalCmp(Opc) || Opc == X86ISD::BT) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005396 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005397 addTest = false;
Bill Wendlingd3511522008-12-02 01:06:39 +00005398 } else {
Evan Chengd580f022008-12-03 08:38:43 +00005399 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling809e7bd2008-12-03 08:32:02 +00005400 default: break;
5401 case X86::COND_O:
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005402 case X86::COND_B:
Chris Lattner77a62312008-12-25 05:34:37 +00005403 // These can only come from an arithmetic instruction with overflow,
5404 // e.g. SADDO, UADDO.
Bill Wendling809e7bd2008-12-03 08:32:02 +00005405 Cond = Cond.getNode()->getOperand(1);
5406 addTest = false;
5407 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00005408 }
Evan Cheng950aac02007-09-25 01:57:46 +00005409 }
Evan Chengd580f022008-12-03 08:38:43 +00005410 } else {
5411 unsigned CondOpc;
5412 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5413 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5414 unsigned Opc = Cmp.getOpcode();
5415 if (CondOpc == ISD::OR) {
5416 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5417 // two branches instead of an explicit OR instruction with a
5418 // separate test.
5419 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5420 isX86LogicalCmp(Opc)) {
5421 CC = Cond.getOperand(0).getOperand(0);
5422 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5423 Chain, Dest, CC, Cmp);
5424 CC = Cond.getOperand(1).getOperand(0);
5425 Cond = Cmp;
5426 addTest = false;
5427 }
5428 } else { // ISD::AND
5429 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5430 // two branches instead of an explicit AND instruction with a
5431 // separate test. However, we only do this if this block doesn't
5432 // have a fall-through edge, because this requires an explicit
5433 // jmp when the condition is false.
5434 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5435 isX86LogicalCmp(Opc) &&
5436 Op.getNode()->hasOneUse()) {
5437 X86::CondCode CCode =
5438 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5439 CCode = X86::GetOppositeBranchCondition(CCode);
5440 CC = DAG.getConstant(CCode, MVT::i8);
5441 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5442 // Look for an unconditional branch following this conditional branch.
5443 // We need this because we need to reverse the successors in order
5444 // to implement FCMP_OEQ.
5445 if (User.getOpcode() == ISD::BR) {
5446 SDValue FalseBB = User.getOperand(1);
5447 SDValue NewBR =
5448 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5449 assert(NewBR == User);
5450 Dest = FalseBB;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005451
Evan Chengd580f022008-12-03 08:38:43 +00005452 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5453 Chain, Dest, CC, Cmp);
5454 X86::CondCode CCode =
5455 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5456 CCode = X86::GetOppositeBranchCondition(CCode);
5457 CC = DAG.getConstant(CCode, MVT::i8);
5458 Cond = Cmp;
5459 addTest = false;
5460 }
5461 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005462 }
5463 }
Evan Cheng950aac02007-09-25 01:57:46 +00005464 }
5465
5466 if (addTest) {
5467 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005468 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005469 }
Evan Cheng621216e2007-09-29 00:00:36 +00005470 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005471 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005472}
5473
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005474
5475// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5476// Calls to _alloca is needed to probe the stack when allocating more than 4k
5477// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5478// that the guard pages used by the OS virtual memory manager are allocated in
5479// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005480SDValue
5481X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005482 SelectionDAG &DAG) {
5483 assert(Subtarget->isTargetCygMing() &&
5484 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005485
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005486 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005487 SDValue Chain = Op.getOperand(0);
5488 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005489 // FIXME: Ensure alignment here
5490
Dan Gohman8181bd12008-07-27 21:46:04 +00005491 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005492
Duncan Sands92c43912008-06-06 12:08:01 +00005493 MVT IntPtr = getPointerTy();
5494 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005495
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005496 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005497
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005498 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5499 Flag = Chain.getValue(1);
5500
5501 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005502 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005503 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005504 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005505 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005506 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005507 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005508 Flag = Chain.getValue(1);
5509
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005510 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005511 DAG.getIntPtrConstant(0, true),
5512 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005513 Flag);
5514
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005515 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005516
Dan Gohman8181bd12008-07-27 21:46:04 +00005517 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005518 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005519}
5520
Dan Gohman8181bd12008-07-27 21:46:04 +00005521SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005522X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005523 SDValue Chain,
5524 SDValue Dst, SDValue Src,
5525 SDValue Size, unsigned Align,
5526 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005527 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005528 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005529
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005530 // If not DWORD aligned or size is more than the threshold, call the library.
5531 // The libc version is likely to be faster for these cases. It can use the
5532 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005533 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005534 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005535 ConstantSize->getZExtValue() >
5536 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005537 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005538
5539 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005540 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005541
Bill Wendling4b2e3782008-10-01 00:59:58 +00005542 if (const char *bzeroEntry = V &&
5543 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5544 MVT IntPtr = getPointerTy();
5545 const Type *IntPtrTy = TD->getIntPtrType();
5546 TargetLowering::ArgListTy Args;
5547 TargetLowering::ArgListEntry Entry;
5548 Entry.Node = Dst;
5549 Entry.Ty = IntPtrTy;
5550 Args.push_back(Entry);
5551 Entry.Node = Size;
5552 Args.push_back(Entry);
Dale Johannesenca6237b2009-01-30 23:10:59 +00005553 // FIXME provide DebugLoc info
Bill Wendling4b2e3782008-10-01 00:59:58 +00005554 std::pair<SDValue,SDValue> CallResult =
5555 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5556 CallingConv::C, false,
Dale Johannesenca6237b2009-01-30 23:10:59 +00005557 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG,
5558 DebugLoc::getUnknownLoc());
Bill Wendling4b2e3782008-10-01 00:59:58 +00005559 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005560 }
5561
Dan Gohmane8b391e2008-04-12 04:36:06 +00005562 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005563 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005564 }
5565
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005566 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005567 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005568 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005569 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005570 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005571 unsigned BytesLeft = 0;
5572 bool TwoRepStos = false;
5573 if (ValC) {
5574 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005575 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005576
5577 // If the value is a constant, then we can potentially use larger sets.
5578 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005579 case 2: // WORD aligned
5580 AVT = MVT::i16;
5581 ValReg = X86::AX;
5582 Val = (Val << 8) | Val;
5583 break;
5584 case 0: // DWORD aligned
5585 AVT = MVT::i32;
5586 ValReg = X86::EAX;
5587 Val = (Val << 8) | Val;
5588 Val = (Val << 16) | Val;
5589 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5590 AVT = MVT::i64;
5591 ValReg = X86::RAX;
5592 Val = (Val << 32) | Val;
5593 }
5594 break;
5595 default: // Byte aligned
5596 AVT = MVT::i8;
5597 ValReg = X86::AL;
5598 Count = DAG.getIntPtrConstant(SizeVal);
5599 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005600 }
5601
Duncan Sandsec142ee2008-06-08 20:54:56 +00005602 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005603 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005604 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5605 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005606 }
5607
5608 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5609 InFlag);
5610 InFlag = Chain.getValue(1);
5611 } else {
5612 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005613 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005614 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005615 InFlag = Chain.getValue(1);
5616 }
5617
5618 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5619 Count, InFlag);
5620 InFlag = Chain.getValue(1);
5621 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005622 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005623 InFlag = Chain.getValue(1);
5624
5625 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005626 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005627 Ops.push_back(Chain);
5628 Ops.push_back(DAG.getValueType(AVT));
5629 Ops.push_back(InFlag);
5630 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5631
5632 if (TwoRepStos) {
5633 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005634 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005635 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005636 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005637 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5638 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5639 Left, InFlag);
5640 InFlag = Chain.getValue(1);
5641 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5642 Ops.clear();
5643 Ops.push_back(Chain);
5644 Ops.push_back(DAG.getValueType(MVT::i8));
5645 Ops.push_back(InFlag);
5646 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5647 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005648 // Handle the last 1 - 7 bytes.
5649 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005650 MVT AddrVT = Dst.getValueType();
5651 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005652
5653 Chain = DAG.getMemset(Chain,
5654 DAG.getNode(ISD::ADD, AddrVT, Dst,
5655 DAG.getConstant(Offset, AddrVT)),
5656 Src,
5657 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005658 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005659 }
5660
Dan Gohmane8b391e2008-04-12 04:36:06 +00005661 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005662 return Chain;
5663}
5664
Dan Gohman8181bd12008-07-27 21:46:04 +00005665SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005666X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005667 SDValue Chain, SDValue Dst, SDValue Src,
5668 SDValue Size, unsigned Align,
5669 bool AlwaysInline,
5670 const Value *DstSV, uint64_t DstSVOff,
5671 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005672 // This requires the copy size to be a constant, preferrably
5673 // within a subtarget-specific limit.
5674 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5675 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005676 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005677 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005678 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005679 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005680
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005681 /// If not DWORD aligned, call the library.
5682 if ((Align & 3) != 0)
5683 return SDValue();
5684
5685 // DWORD aligned
5686 MVT AVT = MVT::i32;
5687 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005688 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005689
Duncan Sands92c43912008-06-06 12:08:01 +00005690 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005691 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005692 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005693 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005694
Dan Gohman8181bd12008-07-27 21:46:04 +00005695 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005696 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5697 Count, InFlag);
5698 InFlag = Chain.getValue(1);
5699 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005700 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005701 InFlag = Chain.getValue(1);
5702 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005703 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005704 InFlag = Chain.getValue(1);
5705
5706 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005707 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005708 Ops.push_back(Chain);
5709 Ops.push_back(DAG.getValueType(AVT));
5710 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005711 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005712
Dan Gohman8181bd12008-07-27 21:46:04 +00005713 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005714 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005715 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005716 // Handle the last 1 - 7 bytes.
5717 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005718 MVT DstVT = Dst.getValueType();
5719 MVT SrcVT = Src.getValueType();
5720 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005721 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005722 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005723 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005724 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005725 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005726 DAG.getConstant(BytesLeft, SizeVT),
5727 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005728 DstSV, DstSVOff + Offset,
5729 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005730 }
5731
Dan Gohmane8b391e2008-04-12 04:36:06 +00005732 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005733}
5734
Dan Gohman8181bd12008-07-27 21:46:04 +00005735SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005736 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005737
5738 if (!Subtarget->is64Bit()) {
5739 // vastart just stores the address of the VarArgsFrameIndex slot into the
5740 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005741 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005742 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005743 }
5744
5745 // __va_list_tag:
5746 // gp_offset (0 - 6 * 8)
5747 // fp_offset (48 - 48 + 8 * 16)
5748 // overflow_arg_area (point to parameters coming in memory).
5749 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005750 SmallVector<SDValue, 8> MemOps;
5751 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005752 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005753 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005754 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005755 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005756 MemOps.push_back(Store);
5757
5758 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005759 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005760 Store = DAG.getStore(Op.getOperand(0),
5761 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005762 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005763 MemOps.push_back(Store);
5764
5765 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005766 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005767 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005768 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005769 MemOps.push_back(Store);
5770
5771 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005772 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005773 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005774 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005775 MemOps.push_back(Store);
5776 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5777}
5778
Dan Gohman8181bd12008-07-27 21:46:04 +00005779SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005780 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5781 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005782 SDValue Chain = Op.getOperand(0);
5783 SDValue SrcPtr = Op.getOperand(1);
5784 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005785
5786 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5787 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005788 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005789}
5790
Dan Gohman8181bd12008-07-27 21:46:04 +00005791SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005792 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005793 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005794 SDValue Chain = Op.getOperand(0);
5795 SDValue DstPtr = Op.getOperand(1);
5796 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005797 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5798 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005799
Dan Gohman840ff5c2008-04-18 20:55:41 +00005800 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5801 DAG.getIntPtrConstant(24), 8, false,
5802 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005803}
5804
Dan Gohman8181bd12008-07-27 21:46:04 +00005805SDValue
5806X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005807 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005808 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005809 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005810 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005811 case Intrinsic::x86_sse_comieq_ss:
5812 case Intrinsic::x86_sse_comilt_ss:
5813 case Intrinsic::x86_sse_comile_ss:
5814 case Intrinsic::x86_sse_comigt_ss:
5815 case Intrinsic::x86_sse_comige_ss:
5816 case Intrinsic::x86_sse_comineq_ss:
5817 case Intrinsic::x86_sse_ucomieq_ss:
5818 case Intrinsic::x86_sse_ucomilt_ss:
5819 case Intrinsic::x86_sse_ucomile_ss:
5820 case Intrinsic::x86_sse_ucomigt_ss:
5821 case Intrinsic::x86_sse_ucomige_ss:
5822 case Intrinsic::x86_sse_ucomineq_ss:
5823 case Intrinsic::x86_sse2_comieq_sd:
5824 case Intrinsic::x86_sse2_comilt_sd:
5825 case Intrinsic::x86_sse2_comile_sd:
5826 case Intrinsic::x86_sse2_comigt_sd:
5827 case Intrinsic::x86_sse2_comige_sd:
5828 case Intrinsic::x86_sse2_comineq_sd:
5829 case Intrinsic::x86_sse2_ucomieq_sd:
5830 case Intrinsic::x86_sse2_ucomilt_sd:
5831 case Intrinsic::x86_sse2_ucomile_sd:
5832 case Intrinsic::x86_sse2_ucomigt_sd:
5833 case Intrinsic::x86_sse2_ucomige_sd:
5834 case Intrinsic::x86_sse2_ucomineq_sd: {
5835 unsigned Opc = 0;
5836 ISD::CondCode CC = ISD::SETCC_INVALID;
5837 switch (IntNo) {
5838 default: break;
5839 case Intrinsic::x86_sse_comieq_ss:
5840 case Intrinsic::x86_sse2_comieq_sd:
5841 Opc = X86ISD::COMI;
5842 CC = ISD::SETEQ;
5843 break;
5844 case Intrinsic::x86_sse_comilt_ss:
5845 case Intrinsic::x86_sse2_comilt_sd:
5846 Opc = X86ISD::COMI;
5847 CC = ISD::SETLT;
5848 break;
5849 case Intrinsic::x86_sse_comile_ss:
5850 case Intrinsic::x86_sse2_comile_sd:
5851 Opc = X86ISD::COMI;
5852 CC = ISD::SETLE;
5853 break;
5854 case Intrinsic::x86_sse_comigt_ss:
5855 case Intrinsic::x86_sse2_comigt_sd:
5856 Opc = X86ISD::COMI;
5857 CC = ISD::SETGT;
5858 break;
5859 case Intrinsic::x86_sse_comige_ss:
5860 case Intrinsic::x86_sse2_comige_sd:
5861 Opc = X86ISD::COMI;
5862 CC = ISD::SETGE;
5863 break;
5864 case Intrinsic::x86_sse_comineq_ss:
5865 case Intrinsic::x86_sse2_comineq_sd:
5866 Opc = X86ISD::COMI;
5867 CC = ISD::SETNE;
5868 break;
5869 case Intrinsic::x86_sse_ucomieq_ss:
5870 case Intrinsic::x86_sse2_ucomieq_sd:
5871 Opc = X86ISD::UCOMI;
5872 CC = ISD::SETEQ;
5873 break;
5874 case Intrinsic::x86_sse_ucomilt_ss:
5875 case Intrinsic::x86_sse2_ucomilt_sd:
5876 Opc = X86ISD::UCOMI;
5877 CC = ISD::SETLT;
5878 break;
5879 case Intrinsic::x86_sse_ucomile_ss:
5880 case Intrinsic::x86_sse2_ucomile_sd:
5881 Opc = X86ISD::UCOMI;
5882 CC = ISD::SETLE;
5883 break;
5884 case Intrinsic::x86_sse_ucomigt_ss:
5885 case Intrinsic::x86_sse2_ucomigt_sd:
5886 Opc = X86ISD::UCOMI;
5887 CC = ISD::SETGT;
5888 break;
5889 case Intrinsic::x86_sse_ucomige_ss:
5890 case Intrinsic::x86_sse2_ucomige_sd:
5891 Opc = X86ISD::UCOMI;
5892 CC = ISD::SETGE;
5893 break;
5894 case Intrinsic::x86_sse_ucomineq_ss:
5895 case Intrinsic::x86_sse2_ucomineq_sd:
5896 Opc = X86ISD::UCOMI;
5897 CC = ISD::SETNE;
5898 break;
5899 }
5900
Dan Gohman8181bd12008-07-27 21:46:04 +00005901 SDValue LHS = Op.getOperand(1);
5902 SDValue RHS = Op.getOperand(2);
Chris Lattnerebb91142008-12-24 23:53:05 +00005903 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman8181bd12008-07-27 21:46:04 +00005904 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5905 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005906 DAG.getConstant(X86CC, MVT::i8), Cond);
5907 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005908 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005909
5910 // Fix vector shift instructions where the last operand is a non-immediate
5911 // i32 value.
5912 case Intrinsic::x86_sse2_pslli_w:
5913 case Intrinsic::x86_sse2_pslli_d:
5914 case Intrinsic::x86_sse2_pslli_q:
5915 case Intrinsic::x86_sse2_psrli_w:
5916 case Intrinsic::x86_sse2_psrli_d:
5917 case Intrinsic::x86_sse2_psrli_q:
5918 case Intrinsic::x86_sse2_psrai_w:
5919 case Intrinsic::x86_sse2_psrai_d:
5920 case Intrinsic::x86_mmx_pslli_w:
5921 case Intrinsic::x86_mmx_pslli_d:
5922 case Intrinsic::x86_mmx_pslli_q:
5923 case Intrinsic::x86_mmx_psrli_w:
5924 case Intrinsic::x86_mmx_psrli_d:
5925 case Intrinsic::x86_mmx_psrli_q:
5926 case Intrinsic::x86_mmx_psrai_w:
5927 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005928 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005929 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005930 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005931
5932 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005933 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005934 switch (IntNo) {
5935 case Intrinsic::x86_sse2_pslli_w:
5936 NewIntNo = Intrinsic::x86_sse2_psll_w;
5937 break;
5938 case Intrinsic::x86_sse2_pslli_d:
5939 NewIntNo = Intrinsic::x86_sse2_psll_d;
5940 break;
5941 case Intrinsic::x86_sse2_pslli_q:
5942 NewIntNo = Intrinsic::x86_sse2_psll_q;
5943 break;
5944 case Intrinsic::x86_sse2_psrli_w:
5945 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5946 break;
5947 case Intrinsic::x86_sse2_psrli_d:
5948 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5949 break;
5950 case Intrinsic::x86_sse2_psrli_q:
5951 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5952 break;
5953 case Intrinsic::x86_sse2_psrai_w:
5954 NewIntNo = Intrinsic::x86_sse2_psra_w;
5955 break;
5956 case Intrinsic::x86_sse2_psrai_d:
5957 NewIntNo = Intrinsic::x86_sse2_psra_d;
5958 break;
5959 default: {
5960 ShAmtVT = MVT::v2i32;
5961 switch (IntNo) {
5962 case Intrinsic::x86_mmx_pslli_w:
5963 NewIntNo = Intrinsic::x86_mmx_psll_w;
5964 break;
5965 case Intrinsic::x86_mmx_pslli_d:
5966 NewIntNo = Intrinsic::x86_mmx_psll_d;
5967 break;
5968 case Intrinsic::x86_mmx_pslli_q:
5969 NewIntNo = Intrinsic::x86_mmx_psll_q;
5970 break;
5971 case Intrinsic::x86_mmx_psrli_w:
5972 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5973 break;
5974 case Intrinsic::x86_mmx_psrli_d:
5975 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5976 break;
5977 case Intrinsic::x86_mmx_psrli_q:
5978 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5979 break;
5980 case Intrinsic::x86_mmx_psrai_w:
5981 NewIntNo = Intrinsic::x86_mmx_psra_w;
5982 break;
5983 case Intrinsic::x86_mmx_psrai_d:
5984 NewIntNo = Intrinsic::x86_mmx_psra_d;
5985 break;
5986 default: abort(); // Can't reach here.
5987 }
5988 break;
5989 }
5990 }
Duncan Sands92c43912008-06-06 12:08:01 +00005991 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005992 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5993 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5994 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5995 DAG.getConstant(NewIntNo, MVT::i32),
5996 Op.getOperand(1), ShAmt);
5997 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005998 }
5999}
6000
Dan Gohman8181bd12008-07-27 21:46:04 +00006001SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006002 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6003
6004 if (Depth > 0) {
6005 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6006 SDValue Offset =
6007 DAG.getConstant(TD->getPointerSize(),
6008 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
6009 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(),
6010 DAG.getNode(ISD::ADD, getPointerTy(), FrameAddr, Offset),
6011 NULL, 0);
6012 }
6013
6014 // Just load the return address.
Dan Gohman8181bd12008-07-27 21:46:04 +00006015 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006016 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
6017}
6018
Dan Gohman8181bd12008-07-27 21:46:04 +00006019SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00006020 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6021 MFI->setFrameAddressIsTaken(true);
6022 MVT VT = Op.getValueType();
6023 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6024 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
6025 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), FrameReg, VT);
6026 while (Depth--)
6027 FrameAddr = DAG.getLoad(VT, DAG.getEntryNode(), FrameAddr, NULL, 0);
6028 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006029}
6030
Dan Gohman8181bd12008-07-27 21:46:04 +00006031SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00006032 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006033 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006034}
6035
Dan Gohman8181bd12008-07-27 21:46:04 +00006036SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006037{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006038 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00006039 SDValue Chain = Op.getOperand(0);
6040 SDValue Offset = Op.getOperand(1);
6041 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006042
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006043 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6044 getPointerTy());
6045 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006046
Dan Gohman8181bd12008-07-27 21:46:04 +00006047 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006048 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006049 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
6050 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006051 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
6052 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006053
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006054 return DAG.getNode(X86ISD::EH_RETURN,
6055 MVT::Other,
6056 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006057}
6058
Dan Gohman8181bd12008-07-27 21:46:04 +00006059SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006060 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006061 SDValue Root = Op.getOperand(0);
6062 SDValue Trmp = Op.getOperand(1); // trampoline
6063 SDValue FPtr = Op.getOperand(2); // nested function
6064 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006065
Dan Gohman12a9c082008-02-06 22:27:42 +00006066 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006067
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006068 const X86InstrInfo *TII =
6069 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6070
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006071 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006072 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006073
6074 // Large code-model.
6075
6076 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6077 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6078
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006079 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6080 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006081
6082 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6083
6084 // Load the pointer to the nested function into R11.
6085 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00006086 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006087 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006088 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006089
6090 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00006091 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006092
6093 // Load the 'nest' parameter value into R10.
6094 // R10 is specified in X86CallingConv.td
6095 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
6096 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
6097 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006098 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006099
6100 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00006101 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006102
6103 // Jump to the nested function.
6104 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
6105 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
6106 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006107 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006108
6109 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
6110 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
6111 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006112 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006113
Dan Gohman8181bd12008-07-27 21:46:04 +00006114 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006115 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00006116 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006117 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00006118 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006119 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6120 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00006121 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006122
6123 switch (CC) {
6124 default:
6125 assert(0 && "Unsupported calling convention");
6126 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006127 case CallingConv::X86_StdCall: {
6128 // Pass 'nest' parameter in ECX.
6129 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006130 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006131
6132 // Check that ECX wasn't needed by an 'inreg' parameter.
6133 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00006134 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006135
Chris Lattner1c8733e2008-03-12 17:45:29 +00006136 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006137 unsigned InRegCount = 0;
6138 unsigned Idx = 1;
6139
6140 for (FunctionType::param_iterator I = FTy->param_begin(),
6141 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00006142 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006143 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006144 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006145
6146 if (InRegCount > 2) {
6147 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6148 abort();
6149 }
6150 }
6151 break;
6152 }
6153 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00006154 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006155 // Pass 'nest' parameter in EAX.
6156 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006157 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006158 break;
6159 }
6160
Dan Gohman8181bd12008-07-27 21:46:04 +00006161 SDValue OutChains[4];
6162 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006163
6164 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
6165 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
6166
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006167 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006168 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00006169 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00006170 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006171
6172 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006173 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006174
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006175 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006176 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
6177 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006178 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006179
6180 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006181 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006182
Dan Gohman8181bd12008-07-27 21:46:04 +00006183 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00006184 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00006185 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006186 }
6187}
6188
Dan Gohman8181bd12008-07-27 21:46:04 +00006189SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006190 /*
6191 The rounding mode is in bits 11:10 of FPSR, and has the following
6192 settings:
6193 00 Round to nearest
6194 01 Round to -inf
6195 10 Round to +inf
6196 11 Round to 0
6197
6198 FLT_ROUNDS, on the other hand, expects the following:
6199 -1 Undefined
6200 0 Round to 0
6201 1 Round to nearest
6202 2 Round to +inf
6203 3 Round to -inf
6204
6205 To perform the conversion, we do:
6206 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6207 */
6208
6209 MachineFunction &MF = DAG.getMachineFunction();
6210 const TargetMachine &TM = MF.getTarget();
6211 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6212 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00006213 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006214
6215 // Save FP Control Word to stack slot
6216 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00006217 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006218
Dan Gohman8181bd12008-07-27 21:46:04 +00006219 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00006220 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006221
6222 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00006223 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006224
6225 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00006226 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006227 DAG.getNode(ISD::SRL, MVT::i16,
6228 DAG.getNode(ISD::AND, MVT::i16,
6229 CWD, DAG.getConstant(0x800, MVT::i16)),
6230 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006231 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006232 DAG.getNode(ISD::SRL, MVT::i16,
6233 DAG.getNode(ISD::AND, MVT::i16,
6234 CWD, DAG.getConstant(0x400, MVT::i16)),
6235 DAG.getConstant(9, MVT::i8));
6236
Dan Gohman8181bd12008-07-27 21:46:04 +00006237 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006238 DAG.getNode(ISD::AND, MVT::i16,
6239 DAG.getNode(ISD::ADD, MVT::i16,
6240 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
6241 DAG.getConstant(1, MVT::i16)),
6242 DAG.getConstant(3, MVT::i16));
6243
6244
Duncan Sands92c43912008-06-06 12:08:01 +00006245 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006246 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6247}
6248
Dan Gohman8181bd12008-07-27 21:46:04 +00006249SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006250 MVT VT = Op.getValueType();
6251 MVT OpVT = VT;
6252 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006253
6254 Op = Op.getOperand(0);
6255 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006256 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00006257 OpVT = MVT::i32;
6258 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6259 }
Evan Cheng48679f42007-12-14 02:13:44 +00006260
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006261 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6262 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6263 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
6264
6265 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006266 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006267 Ops.push_back(Op);
6268 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6269 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6270 Ops.push_back(Op.getValue(1));
6271 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6272
6273 // Finally xor with NumBits-1.
6274 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
6275
Evan Cheng48679f42007-12-14 02:13:44 +00006276 if (VT == MVT::i8)
6277 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6278 return Op;
6279}
6280
Dan Gohman8181bd12008-07-27 21:46:04 +00006281SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006282 MVT VT = Op.getValueType();
6283 MVT OpVT = VT;
6284 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006285
6286 Op = Op.getOperand(0);
6287 if (VT == MVT::i8) {
6288 OpVT = MVT::i32;
6289 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6290 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006291
6292 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6293 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6294 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
6295
6296 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006297 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006298 Ops.push_back(Op);
6299 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6300 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6301 Ops.push_back(Op.getValue(1));
6302 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6303
Evan Cheng48679f42007-12-14 02:13:44 +00006304 if (VT == MVT::i8)
6305 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6306 return Op;
6307}
6308
Mon P Wang14edb092008-12-18 21:42:19 +00006309SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6310 MVT VT = Op.getValueType();
6311 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
6312
6313 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6314 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6315 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6316 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6317 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6318 //
6319 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6320 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6321 // return AloBlo + AloBhi + AhiBlo;
6322
6323 SDValue A = Op.getOperand(0);
6324 SDValue B = Op.getOperand(1);
6325
6326 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6327 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6328 A, DAG.getConstant(32, MVT::i32));
6329 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6330 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6331 B, DAG.getConstant(32, MVT::i32));
6332 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6333 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6334 A, B);
6335 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6336 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6337 A, Bhi);
6338 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6339 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6340 Ahi, B);
6341 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6342 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6343 AloBhi, DAG.getConstant(32, MVT::i32));
6344 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6345 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6346 AhiBlo, DAG.getConstant(32, MVT::i32));
6347 SDValue Res = DAG.getNode(ISD::ADD, VT, AloBlo, AloBhi);
6348 Res = DAG.getNode(ISD::ADD, VT, Res, AhiBlo);
6349 return Res;
6350}
6351
6352
Bill Wendling7e04be62008-12-09 22:08:41 +00006353SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6354 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6355 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendlingd3511522008-12-02 01:06:39 +00006356 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6357 // has only one use.
Bill Wendlingd06b4202008-11-26 22:37:40 +00006358 SDNode *N = Op.getNode();
Bill Wendlingd3511522008-12-02 01:06:39 +00006359 SDValue LHS = N->getOperand(0);
6360 SDValue RHS = N->getOperand(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00006361 unsigned BaseOp = 0;
6362 unsigned Cond = 0;
6363
6364 switch (Op.getOpcode()) {
6365 default: assert(0 && "Unknown ovf instruction!");
6366 case ISD::SADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006367 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00006368 Cond = X86::COND_O;
6369 break;
6370 case ISD::UADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006371 BaseOp = X86ISD::ADD;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006372 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006373 break;
6374 case ISD::SSUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006375 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00006376 Cond = X86::COND_O;
6377 break;
6378 case ISD::USUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006379 BaseOp = X86ISD::SUB;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006380 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006381 break;
6382 case ISD::SMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006383 BaseOp = X86ISD::SMUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00006384 Cond = X86::COND_O;
6385 break;
6386 case ISD::UMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006387 BaseOp = X86ISD::UMUL;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006388 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006389 break;
6390 }
Bill Wendlingd06b4202008-11-26 22:37:40 +00006391
Bill Wendlingd3511522008-12-02 01:06:39 +00006392 // Also sets EFLAGS.
6393 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Bill Wendling7e04be62008-12-09 22:08:41 +00006394 SDValue Sum = DAG.getNode(BaseOp, VTs, LHS, RHS);
Bill Wendlingd06b4202008-11-26 22:37:40 +00006395
Bill Wendlingd3511522008-12-02 01:06:39 +00006396 SDValue SetCC =
6397 DAG.getNode(X86ISD::SETCC, N->getValueType(1),
Bill Wendling35f1a9d2008-12-10 02:01:32 +00006398 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendlingd06b4202008-11-26 22:37:40 +00006399
Bill Wendlingd3511522008-12-02 01:06:39 +00006400 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6401 return Sum;
Bill Wendling4c134df2008-11-24 19:21:46 +00006402}
6403
Dan Gohman8181bd12008-07-27 21:46:04 +00006404SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006405 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00006406 unsigned Reg = 0;
6407 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006408 switch(T.getSimpleVT()) {
6409 default:
6410 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006411 case MVT::i8: Reg = X86::AL; size = 1; break;
6412 case MVT::i16: Reg = X86::AX; size = 2; break;
6413 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00006414 case MVT::i64:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006415 assert(Subtarget->is64Bit() && "Node not type legal!");
6416 Reg = X86::RAX; size = 8;
Andrew Lenharth81580822008-03-05 01:15:49 +00006417 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00006418 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006419 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00006420 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00006421 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006422 Op.getOperand(1),
6423 Op.getOperand(3),
6424 DAG.getTargetConstant(size, MVT::i8),
6425 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006426 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006427 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
6428 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006429 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
6430 return cpOut;
6431}
6432
Duncan Sands7d9834b2008-12-01 11:39:25 +00006433SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif825aa892008-08-28 23:19:51 +00006434 SelectionDAG &DAG) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006435 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharth81580822008-03-05 01:15:49 +00006436 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006437 SDValue TheChain = Op.getOperand(0);
6438 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6439 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
6440 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX, MVT::i64,
6441 rax.getValue(2));
6442 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
6443 DAG.getConstant(32, MVT::i8));
6444 SDValue Ops[] = {
6445 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp),
6446 rdx.getValue(1)
6447 };
6448 return DAG.getMergeValues(Ops, 2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006449}
6450
Dale Johannesen9011d872008-09-29 22:25:26 +00006451SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6452 SDNode *Node = Op.getNode();
6453 MVT T = Node->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006454 SDValue negOp = DAG.getNode(ISD::SUB, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006455 DAG.getConstant(0, T), Node->getOperand(2));
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006456 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD,
6457 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen9011d872008-09-29 22:25:26 +00006458 Node->getOperand(0),
6459 Node->getOperand(1), negOp,
6460 cast<AtomicSDNode>(Node)->getSrcValue(),
6461 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006462}
6463
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006464/// LowerOperation - Provide custom lowering hooks for some operations.
6465///
Dan Gohman8181bd12008-07-27 21:46:04 +00006466SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006467 switch (Op.getOpcode()) {
6468 default: assert(0 && "Should not custom lower this!");
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006469 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6470 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006471 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6472 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6473 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6474 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6475 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6476 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6477 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6478 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006479 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006480 case ISD::SHL_PARTS:
6481 case ISD::SRA_PARTS:
6482 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6483 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00006484 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006485 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6486 case ISD::FABS: return LowerFABS(Op, DAG);
6487 case ISD::FNEG: return LowerFNEG(Op, DAG);
6488 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006489 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006490 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006491 case ISD::SELECT: return LowerSELECT(Op, DAG);
6492 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006493 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6494 case ISD::CALL: return LowerCALL(Op, DAG);
6495 case ISD::RET: return LowerRET(Op, DAG);
6496 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006497 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006498 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006499 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6500 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6501 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6502 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6503 case ISD::FRAME_TO_ARGS_OFFSET:
6504 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6505 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6506 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006507 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006508 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006509 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6510 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wang14edb092008-12-18 21:42:19 +00006511 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling7e04be62008-12-09 22:08:41 +00006512 case ISD::SADDO:
6513 case ISD::UADDO:
6514 case ISD::SSUBO:
6515 case ISD::USUBO:
6516 case ISD::SMULO:
6517 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006518 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006519 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006520}
6521
Duncan Sands7d9834b2008-12-01 11:39:25 +00006522void X86TargetLowering::
6523ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6524 SelectionDAG &DAG, unsigned NewOp) {
6525 MVT T = Node->getValueType(0);
6526 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6527
6528 SDValue Chain = Node->getOperand(0);
6529 SDValue In1 = Node->getOperand(1);
6530 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6531 Node->getOperand(2), DAG.getIntPtrConstant(0));
6532 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6533 Node->getOperand(2), DAG.getIntPtrConstant(1));
6534 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6535 // have a MemOperand. Pass the info through as a normal operand.
6536 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6537 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6538 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
6539 SDValue Result = DAG.getNode(NewOp, Tys, Ops, 5);
6540 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
6541 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6542 Results.push_back(Result.getValue(2));
6543}
6544
Duncan Sandsac496a12008-07-04 11:47:58 +00006545/// ReplaceNodeResults - Replace a node with an illegal result type
6546/// with a new node built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00006547void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6548 SmallVectorImpl<SDValue>&Results,
6549 SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006550 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006551 default:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006552 assert(false && "Do not know how to custom type legalize this operation!");
6553 return;
6554 case ISD::FP_TO_SINT: {
6555 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
6556 SDValue FIST = Vals.first, StackSlot = Vals.second;
6557 if (FIST.getNode() != 0) {
6558 MVT VT = N->getValueType(0);
6559 // Return a load from the stack slot.
6560 Results.push_back(DAG.getLoad(VT, FIST, StackSlot, NULL, 0));
6561 }
6562 return;
6563 }
6564 case ISD::READCYCLECOUNTER: {
6565 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6566 SDValue TheChain = N->getOperand(0);
6567 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6568 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
6569 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX, MVT::i32,
6570 eax.getValue(2));
6571 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6572 SDValue Ops[] = { eax, edx };
6573 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2));
6574 Results.push_back(edx.getValue(1));
6575 return;
6576 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006577 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006578 MVT T = N->getValueType(0);
6579 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6580 SDValue cpInL, cpInH;
6581 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6582 DAG.getConstant(0, MVT::i32));
6583 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6584 DAG.getConstant(1, MVT::i32));
6585 cpInL = DAG.getCopyToReg(N->getOperand(0), X86::EAX, cpInL, SDValue());
6586 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX, cpInH,
6587 cpInL.getValue(1));
6588 SDValue swapInL, swapInH;
6589 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6590 DAG.getConstant(0, MVT::i32));
6591 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6592 DAG.getConstant(1, MVT::i32));
6593 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX, swapInL,
6594 cpInH.getValue(1));
6595 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX, swapInH,
6596 swapInL.getValue(1));
6597 SDValue Ops[] = { swapInH.getValue(0),
6598 N->getOperand(1),
6599 swapInH.getValue(1) };
6600 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6601 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
6602 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
6603 Result.getValue(1));
6604 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
6605 cpOutL.getValue(2));
6606 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
6607 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6608 Results.push_back(cpOutH.getValue(1));
6609 return;
6610 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006611 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006612 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6613 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006614 case ISD::ATOMIC_LOAD_AND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006615 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6616 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006617 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006618 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6619 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006620 case ISD::ATOMIC_LOAD_OR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006621 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6622 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006623 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006624 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6625 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006626 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006627 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6628 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006629 case ISD::ATOMIC_SWAP:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006630 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6631 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006632 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006633}
6634
6635const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6636 switch (Opcode) {
6637 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006638 case X86ISD::BSF: return "X86ISD::BSF";
6639 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006640 case X86ISD::SHLD: return "X86ISD::SHLD";
6641 case X86ISD::SHRD: return "X86ISD::SHRD";
6642 case X86ISD::FAND: return "X86ISD::FAND";
6643 case X86ISD::FOR: return "X86ISD::FOR";
6644 case X86ISD::FXOR: return "X86ISD::FXOR";
6645 case X86ISD::FSRL: return "X86ISD::FSRL";
6646 case X86ISD::FILD: return "X86ISD::FILD";
6647 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6648 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6649 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6650 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6651 case X86ISD::FLD: return "X86ISD::FLD";
6652 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006653 case X86ISD::CALL: return "X86ISD::CALL";
6654 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6655 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohman7fe9b7f2008-12-23 22:45:23 +00006656 case X86ISD::BT: return "X86ISD::BT";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006657 case X86ISD::CMP: return "X86ISD::CMP";
6658 case X86ISD::COMI: return "X86ISD::COMI";
6659 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6660 case X86ISD::SETCC: return "X86ISD::SETCC";
6661 case X86ISD::CMOV: return "X86ISD::CMOV";
6662 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6663 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6664 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6665 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006666 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6667 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006668 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006669 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006670 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6671 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006672 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6673 case X86ISD::FMAX: return "X86ISD::FMAX";
6674 case X86ISD::FMIN: return "X86ISD::FMIN";
6675 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6676 case X86ISD::FRCP: return "X86ISD::FRCP";
6677 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6678 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6679 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006680 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006681 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006682 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6683 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006684 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6685 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6686 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6687 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6688 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6689 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006690 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6691 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006692 case X86ISD::VSHL: return "X86ISD::VSHL";
6693 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006694 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6695 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6696 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6697 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6698 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6699 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6700 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6701 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6702 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6703 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingae034ed2008-12-12 00:56:36 +00006704 case X86ISD::ADD: return "X86ISD::ADD";
6705 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingf5399032008-12-12 21:15:41 +00006706 case X86ISD::SMUL: return "X86ISD::SMUL";
6707 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006708 }
6709}
6710
6711// isLegalAddressingMode - Return true if the addressing mode represented
6712// by AM is legal for this target, for a load/store of the specified type.
6713bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6714 const Type *Ty) const {
6715 // X86 supports extremely general addressing modes.
6716
6717 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6718 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6719 return false;
6720
6721 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006722 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006723 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6724 return false;
Dale Johannesen64660e92008-12-05 21:47:27 +00006725 // If BaseGV requires a register, we cannot also have a BaseReg.
6726 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6727 AM.HasBaseReg)
6728 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006729
6730 // X86-64 only supports addr of globals in small code model.
6731 if (Subtarget->is64Bit()) {
6732 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6733 return false;
6734 // If lower 4G is not available, then we must use rip-relative addressing.
6735 if (AM.BaseOffs || AM.Scale > 1)
6736 return false;
6737 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006738 }
6739
6740 switch (AM.Scale) {
6741 case 0:
6742 case 1:
6743 case 2:
6744 case 4:
6745 case 8:
6746 // These scales always work.
6747 break;
6748 case 3:
6749 case 5:
6750 case 9:
6751 // These scales are formed with basereg+scalereg. Only accept if there is
6752 // no basereg yet.
6753 if (AM.HasBaseReg)
6754 return false;
6755 break;
6756 default: // Other stuff never works.
6757 return false;
6758 }
6759
6760 return true;
6761}
6762
6763
Evan Cheng27a820a2007-10-26 01:56:11 +00006764bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6765 if (!Ty1->isInteger() || !Ty2->isInteger())
6766 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006767 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6768 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006769 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006770 return false;
6771 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006772}
6773
Duncan Sands92c43912008-06-06 12:08:01 +00006774bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6775 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006776 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006777 unsigned NumBits1 = VT1.getSizeInBits();
6778 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006779 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006780 return false;
6781 return Subtarget->is64Bit() || NumBits1 < 64;
6782}
Evan Cheng27a820a2007-10-26 01:56:11 +00006783
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006784/// isShuffleMaskLegal - Targets can use this to indicate that they only
6785/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6786/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6787/// are assumed to be legal.
6788bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006789X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006790 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006791 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006792 return (Mask.getNode()->getNumOperands() <= 4 ||
6793 isIdentityMask(Mask.getNode()) ||
6794 isIdentityMask(Mask.getNode(), true) ||
6795 isSplatMask(Mask.getNode()) ||
6796 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6797 X86::isUNPCKLMask(Mask.getNode()) ||
6798 X86::isUNPCKHMask(Mask.getNode()) ||
6799 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6800 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006801}
6802
Dan Gohman48d5f062008-04-09 20:09:42 +00006803bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006804X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006805 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006806 unsigned NumElts = BVOps.size();
6807 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006808 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006809 if (NumElts == 2) return true;
6810 if (NumElts == 4) {
6811 return (isMOVLMask(&BVOps[0], 4) ||
6812 isCommutedMOVL(&BVOps[0], 4, true) ||
6813 isSHUFPMask(&BVOps[0], 4) ||
6814 isCommutedSHUFP(&BVOps[0], 4));
6815 }
6816 return false;
6817}
6818
6819//===----------------------------------------------------------------------===//
6820// X86 Scheduler Hooks
6821//===----------------------------------------------------------------------===//
6822
Mon P Wang078a62d2008-05-05 19:05:59 +00006823// private utility function
6824MachineBasicBlock *
6825X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6826 MachineBasicBlock *MBB,
6827 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006828 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006829 unsigned LoadOpc,
6830 unsigned CXchgOpc,
6831 unsigned copyOpc,
6832 unsigned notOpc,
6833 unsigned EAXreg,
6834 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006835 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006836 // For the atomic bitwise operator, we generate
6837 // thisMBB:
6838 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006839 // ld t1 = [bitinstr.addr]
6840 // op t2 = t1, [bitinstr.val]
6841 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006842 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6843 // bz newMBB
6844 // fallthrough -->nextMBB
6845 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6846 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006847 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006848 ++MBBIter;
6849
6850 /// First build the CFG
6851 MachineFunction *F = MBB->getParent();
6852 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006853 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6854 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6855 F->insert(MBBIter, newMBB);
6856 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006857
6858 // Move all successors to thisMBB to nextMBB
6859 nextMBB->transferSuccessors(thisMBB);
6860
6861 // Update thisMBB to fall through to newMBB
6862 thisMBB->addSuccessor(newMBB);
6863
6864 // newMBB jumps to itself and fall through to nextMBB
6865 newMBB->addSuccessor(nextMBB);
6866 newMBB->addSuccessor(newMBB);
6867
6868 // Insert instructions into newMBB based on incoming instruction
6869 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6870 MachineOperand& destOper = bInstr->getOperand(0);
6871 MachineOperand* argOpers[6];
6872 int numArgs = bInstr->getNumOperands() - 1;
6873 for (int i=0; i < numArgs; ++i)
6874 argOpers[i] = &bInstr->getOperand(i+1);
6875
6876 // x86 address has 4 operands: base, index, scale, and displacement
6877 int lastAddrIndx = 3; // [0,3]
6878 int valArgIndx = 4;
6879
Dale Johannesend20e4452008-08-19 18:47:28 +00006880 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6881 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006882 for (int i=0; i <= lastAddrIndx; ++i)
6883 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006884
Dale Johannesend20e4452008-08-19 18:47:28 +00006885 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006886 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006887 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006888 }
6889 else
6890 tt = t1;
6891
Dale Johannesend20e4452008-08-19 18:47:28 +00006892 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006893 assert((argOpers[valArgIndx]->isReg() ||
6894 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006895 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006896 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006897 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6898 else
6899 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006900 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006901 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006902
Dale Johannesend20e4452008-08-19 18:47:28 +00006903 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006904 MIB.addReg(t1);
6905
Dale Johannesend20e4452008-08-19 18:47:28 +00006906 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006907 for (int i=0; i <= lastAddrIndx; ++i)
6908 (*MIB).addOperand(*argOpers[i]);
6909 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006910 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6911 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6912
Dale Johannesend20e4452008-08-19 18:47:28 +00006913 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6914 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006915
6916 // insert branch
6917 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6918
Dan Gohman221a4372008-07-07 23:14:23 +00006919 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006920 return nextMBB;
6921}
6922
Dale Johannesen44eb5372008-10-03 19:41:08 +00006923// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00006924MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00006925X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
6926 MachineBasicBlock *MBB,
6927 unsigned regOpcL,
6928 unsigned regOpcH,
6929 unsigned immOpcL,
6930 unsigned immOpcH,
6931 bool invSrc) {
6932 // For the atomic bitwise operator, we generate
6933 // thisMBB (instructions are in pairs, except cmpxchg8b)
6934 // ld t1,t2 = [bitinstr.addr]
6935 // newMBB:
6936 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
6937 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006938 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00006939 // mov ECX, EBX <- t5, t6
6940 // mov EAX, EDX <- t1, t2
6941 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
6942 // mov t3, t4 <- EAX, EDX
6943 // bz newMBB
6944 // result in out1, out2
6945 // fallthrough -->nextMBB
6946
6947 const TargetRegisterClass *RC = X86::GR32RegisterClass;
6948 const unsigned LoadOpc = X86::MOV32rm;
6949 const unsigned copyOpc = X86::MOV32rr;
6950 const unsigned NotOpc = X86::NOT32r;
6951 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6952 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6953 MachineFunction::iterator MBBIter = MBB;
6954 ++MBBIter;
6955
6956 /// First build the CFG
6957 MachineFunction *F = MBB->getParent();
6958 MachineBasicBlock *thisMBB = MBB;
6959 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6960 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6961 F->insert(MBBIter, newMBB);
6962 F->insert(MBBIter, nextMBB);
6963
6964 // Move all successors to thisMBB to nextMBB
6965 nextMBB->transferSuccessors(thisMBB);
6966
6967 // Update thisMBB to fall through to newMBB
6968 thisMBB->addSuccessor(newMBB);
6969
6970 // newMBB jumps to itself and fall through to nextMBB
6971 newMBB->addSuccessor(nextMBB);
6972 newMBB->addSuccessor(newMBB);
6973
6974 // Insert instructions into newMBB based on incoming instruction
6975 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
6976 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
6977 MachineOperand& dest1Oper = bInstr->getOperand(0);
6978 MachineOperand& dest2Oper = bInstr->getOperand(1);
6979 MachineOperand* argOpers[6];
6980 for (int i=0; i < 6; ++i)
6981 argOpers[i] = &bInstr->getOperand(i+2);
6982
6983 // x86 address has 4 operands: base, index, scale, and displacement
6984 int lastAddrIndx = 3; // [0,3]
6985
6986 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6987 MachineInstrBuilder MIB = BuildMI(thisMBB, TII->get(LoadOpc), t1);
6988 for (int i=0; i <= lastAddrIndx; ++i)
6989 (*MIB).addOperand(*argOpers[i]);
6990 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
6991 MIB = BuildMI(thisMBB, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006992 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00006993 for (int i=0; i <= lastAddrIndx-1; ++i)
6994 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006995 MachineOperand newOp3 = *(argOpers[3]);
6996 if (newOp3.isImm())
6997 newOp3.setImm(newOp3.getImm()+4);
6998 else
6999 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00007000 (*MIB).addOperand(newOp3);
7001
7002 // t3/4 are defined later, at the bottom of the loop
7003 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7004 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
7005 BuildMI(newMBB, TII->get(X86::PHI), dest1Oper.getReg())
7006 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
7007 BuildMI(newMBB, TII->get(X86::PHI), dest2Oper.getReg())
7008 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7009
7010 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7011 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
7012 if (invSrc) {
7013 MIB = BuildMI(newMBB, TII->get(NotOpc), tt1).addReg(t1);
7014 MIB = BuildMI(newMBB, TII->get(NotOpc), tt2).addReg(t2);
7015 } else {
7016 tt1 = t1;
7017 tt2 = t2;
7018 }
7019
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007020 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00007021 "invalid operand");
7022 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7023 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007024 if (argOpers[4]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007025 MIB = BuildMI(newMBB, TII->get(regOpcL), t5);
7026 else
7027 MIB = BuildMI(newMBB, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007028 if (regOpcL != X86::MOV32rr)
7029 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00007030 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007031 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
7032 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
7033 if (argOpers[5]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007034 MIB = BuildMI(newMBB, TII->get(regOpcH), t6);
7035 else
7036 MIB = BuildMI(newMBB, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007037 if (regOpcH != X86::MOV32rr)
7038 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00007039 (*MIB).addOperand(*argOpers[5]);
7040
7041 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EAX);
7042 MIB.addReg(t1);
7043 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EDX);
7044 MIB.addReg(t2);
7045
7046 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EBX);
7047 MIB.addReg(t5);
7048 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::ECX);
7049 MIB.addReg(t6);
7050
7051 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG8B));
7052 for (int i=0; i <= lastAddrIndx; ++i)
7053 (*MIB).addOperand(*argOpers[i]);
7054
7055 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7056 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7057
7058 MIB = BuildMI(newMBB, TII->get(copyOpc), t3);
7059 MIB.addReg(X86::EAX);
7060 MIB = BuildMI(newMBB, TII->get(copyOpc), t4);
7061 MIB.addReg(X86::EDX);
7062
7063 // insert branch
7064 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
7065
7066 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7067 return nextMBB;
7068}
7069
7070// private utility function
7071MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00007072X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7073 MachineBasicBlock *MBB,
7074 unsigned cmovOpc) {
7075 // For the atomic min/max operator, we generate
7076 // thisMBB:
7077 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00007078 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00007079 // mov t2 = [min/max.val]
7080 // cmp t1, t2
7081 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00007082 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00007083 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7084 // bz newMBB
7085 // fallthrough -->nextMBB
7086 //
7087 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7088 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007089 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00007090 ++MBBIter;
7091
7092 /// First build the CFG
7093 MachineFunction *F = MBB->getParent();
7094 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00007095 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7096 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7097 F->insert(MBBIter, newMBB);
7098 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007099
7100 // Move all successors to thisMBB to nextMBB
7101 nextMBB->transferSuccessors(thisMBB);
7102
7103 // Update thisMBB to fall through to newMBB
7104 thisMBB->addSuccessor(newMBB);
7105
7106 // newMBB jumps to newMBB and fall through to nextMBB
7107 newMBB->addSuccessor(nextMBB);
7108 newMBB->addSuccessor(newMBB);
7109
7110 // Insert instructions into newMBB based on incoming instruction
7111 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
7112 MachineOperand& destOper = mInstr->getOperand(0);
7113 MachineOperand* argOpers[6];
7114 int numArgs = mInstr->getNumOperands() - 1;
7115 for (int i=0; i < numArgs; ++i)
7116 argOpers[i] = &mInstr->getOperand(i+1);
7117
7118 // x86 address has 4 operands: base, index, scale, and displacement
7119 int lastAddrIndx = 3; // [0,3]
7120 int valArgIndx = 4;
7121
Mon P Wang318b0372008-05-05 22:56:23 +00007122 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
7123 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00007124 for (int i=0; i <= lastAddrIndx; ++i)
7125 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00007126
Mon P Wang078a62d2008-05-05 19:05:59 +00007127 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007128 assert((argOpers[valArgIndx]->isReg() ||
7129 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00007130 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00007131
7132 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007133 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00007134 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
7135 else
7136 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
7137 (*MIB).addOperand(*argOpers[valArgIndx]);
7138
Mon P Wang318b0372008-05-05 22:56:23 +00007139 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
7140 MIB.addReg(t1);
7141
Mon P Wang078a62d2008-05-05 19:05:59 +00007142 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
7143 MIB.addReg(t1);
7144 MIB.addReg(t2);
7145
7146 // Generate movc
7147 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
7148 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
7149 MIB.addReg(t2);
7150 MIB.addReg(t1);
7151
7152 // Cmp and exchange if none has modified the memory location
7153 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
7154 for (int i=0; i <= lastAddrIndx; ++i)
7155 (*MIB).addOperand(*argOpers[i]);
7156 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00007157 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7158 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00007159
7160 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
7161 MIB.addReg(X86::EAX);
7162
7163 // insert branch
7164 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
7165
Dan Gohman221a4372008-07-07 23:14:23 +00007166 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00007167 return nextMBB;
7168}
7169
7170
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007171MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00007172X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
7173 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007174 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7175 switch (MI->getOpcode()) {
7176 default: assert(false && "Unexpected instr type to insert");
Mon P Wang83edba52008-12-12 01:25:51 +00007177 case X86::CMOV_V1I64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007178 case X86::CMOV_FR32:
7179 case X86::CMOV_FR64:
7180 case X86::CMOV_V4F32:
7181 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00007182 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007183 // To "insert" a SELECT_CC instruction, we actually have to insert the
7184 // diamond control-flow pattern. The incoming instruction knows the
7185 // destination vreg to set, the condition code register to branch on, the
7186 // true/false values to select between, and a branch opcode to use.
7187 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007188 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007189 ++It;
7190
7191 // thisMBB:
7192 // ...
7193 // TrueVal = ...
7194 // cmpTY ccX, r1, r2
7195 // bCC copy1MBB
7196 // fallthrough --> copy0MBB
7197 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00007198 MachineFunction *F = BB->getParent();
7199 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7200 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007201 unsigned Opc =
7202 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
7203 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00007204 F->insert(It, copy0MBB);
7205 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007206 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007207 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00007208 sinkMBB->transferSuccessors(BB);
7209
7210 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007211 BB->addSuccessor(copy0MBB);
7212 BB->addSuccessor(sinkMBB);
7213
7214 // copy0MBB:
7215 // %FalseValue = ...
7216 // # fallthrough to sinkMBB
7217 BB = copy0MBB;
7218
7219 // Update machine-CFG edges
7220 BB->addSuccessor(sinkMBB);
7221
7222 // sinkMBB:
7223 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7224 // ...
7225 BB = sinkMBB;
7226 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
7227 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7228 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7229
Dan Gohman221a4372008-07-07 23:14:23 +00007230 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007231 return BB;
7232 }
7233
7234 case X86::FP32_TO_INT16_IN_MEM:
7235 case X86::FP32_TO_INT32_IN_MEM:
7236 case X86::FP32_TO_INT64_IN_MEM:
7237 case X86::FP64_TO_INT16_IN_MEM:
7238 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007239 case X86::FP64_TO_INT64_IN_MEM:
7240 case X86::FP80_TO_INT16_IN_MEM:
7241 case X86::FP80_TO_INT32_IN_MEM:
7242 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007243 // Change the floating point control register to use "round towards zero"
7244 // mode when truncating to an integer value.
7245 MachineFunction *F = BB->getParent();
7246 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
7247 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
7248
7249 // Load the old value of the high byte of the control word...
7250 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00007251 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007252 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
7253
7254 // Set the high part to be round to zero...
7255 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
7256 .addImm(0xC7F);
7257
7258 // Reload the modified control word now...
7259 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7260
7261 // Restore the memory image of control word to original value
7262 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
7263 .addReg(OldCW);
7264
7265 // Get the X86 opcode to use.
7266 unsigned Opc;
7267 switch (MI->getOpcode()) {
7268 default: assert(0 && "illegal opcode!");
7269 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7270 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7271 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7272 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7273 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7274 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007275 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7276 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7277 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007278 }
7279
7280 X86AddressMode AM;
7281 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007282 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007283 AM.BaseType = X86AddressMode::RegBase;
7284 AM.Base.Reg = Op.getReg();
7285 } else {
7286 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00007287 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007288 }
7289 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007290 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007291 AM.Scale = Op.getImm();
7292 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007293 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007294 AM.IndexReg = Op.getImm();
7295 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007296 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007297 AM.GV = Op.getGlobal();
7298 } else {
7299 AM.Disp = Op.getImm();
7300 }
7301 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
7302 .addReg(MI->getOperand(4).getReg());
7303
7304 // Reload the original control word now.
7305 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7306
Dan Gohman221a4372008-07-07 23:14:23 +00007307 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007308 return BB;
7309 }
Mon P Wang078a62d2008-05-05 19:05:59 +00007310 case X86::ATOMAND32:
7311 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007312 X86::AND32ri, X86::MOV32rm,
7313 X86::LCMPXCHG32, X86::MOV32rr,
7314 X86::NOT32r, X86::EAX,
7315 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007316 case X86::ATOMOR32:
7317 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007318 X86::OR32ri, X86::MOV32rm,
7319 X86::LCMPXCHG32, X86::MOV32rr,
7320 X86::NOT32r, X86::EAX,
7321 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007322 case X86::ATOMXOR32:
7323 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007324 X86::XOR32ri, X86::MOV32rm,
7325 X86::LCMPXCHG32, X86::MOV32rr,
7326 X86::NOT32r, X86::EAX,
7327 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007328 case X86::ATOMNAND32:
7329 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007330 X86::AND32ri, X86::MOV32rm,
7331 X86::LCMPXCHG32, X86::MOV32rr,
7332 X86::NOT32r, X86::EAX,
7333 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00007334 case X86::ATOMMIN32:
7335 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7336 case X86::ATOMMAX32:
7337 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7338 case X86::ATOMUMIN32:
7339 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7340 case X86::ATOMUMAX32:
7341 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00007342
7343 case X86::ATOMAND16:
7344 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7345 X86::AND16ri, X86::MOV16rm,
7346 X86::LCMPXCHG16, X86::MOV16rr,
7347 X86::NOT16r, X86::AX,
7348 X86::GR16RegisterClass);
7349 case X86::ATOMOR16:
7350 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7351 X86::OR16ri, X86::MOV16rm,
7352 X86::LCMPXCHG16, X86::MOV16rr,
7353 X86::NOT16r, X86::AX,
7354 X86::GR16RegisterClass);
7355 case X86::ATOMXOR16:
7356 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7357 X86::XOR16ri, X86::MOV16rm,
7358 X86::LCMPXCHG16, X86::MOV16rr,
7359 X86::NOT16r, X86::AX,
7360 X86::GR16RegisterClass);
7361 case X86::ATOMNAND16:
7362 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7363 X86::AND16ri, X86::MOV16rm,
7364 X86::LCMPXCHG16, X86::MOV16rr,
7365 X86::NOT16r, X86::AX,
7366 X86::GR16RegisterClass, true);
7367 case X86::ATOMMIN16:
7368 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7369 case X86::ATOMMAX16:
7370 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7371 case X86::ATOMUMIN16:
7372 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7373 case X86::ATOMUMAX16:
7374 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7375
7376 case X86::ATOMAND8:
7377 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7378 X86::AND8ri, X86::MOV8rm,
7379 X86::LCMPXCHG8, X86::MOV8rr,
7380 X86::NOT8r, X86::AL,
7381 X86::GR8RegisterClass);
7382 case X86::ATOMOR8:
7383 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7384 X86::OR8ri, X86::MOV8rm,
7385 X86::LCMPXCHG8, X86::MOV8rr,
7386 X86::NOT8r, X86::AL,
7387 X86::GR8RegisterClass);
7388 case X86::ATOMXOR8:
7389 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7390 X86::XOR8ri, X86::MOV8rm,
7391 X86::LCMPXCHG8, X86::MOV8rr,
7392 X86::NOT8r, X86::AL,
7393 X86::GR8RegisterClass);
7394 case X86::ATOMNAND8:
7395 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7396 X86::AND8ri, X86::MOV8rm,
7397 X86::LCMPXCHG8, X86::MOV8rr,
7398 X86::NOT8r, X86::AL,
7399 X86::GR8RegisterClass, true);
7400 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00007401 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007402 case X86::ATOMAND64:
7403 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7404 X86::AND64ri32, X86::MOV64rm,
7405 X86::LCMPXCHG64, X86::MOV64rr,
7406 X86::NOT64r, X86::RAX,
7407 X86::GR64RegisterClass);
7408 case X86::ATOMOR64:
7409 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7410 X86::OR64ri32, X86::MOV64rm,
7411 X86::LCMPXCHG64, X86::MOV64rr,
7412 X86::NOT64r, X86::RAX,
7413 X86::GR64RegisterClass);
7414 case X86::ATOMXOR64:
7415 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7416 X86::XOR64ri32, X86::MOV64rm,
7417 X86::LCMPXCHG64, X86::MOV64rr,
7418 X86::NOT64r, X86::RAX,
7419 X86::GR64RegisterClass);
7420 case X86::ATOMNAND64:
7421 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7422 X86::AND64ri32, X86::MOV64rm,
7423 X86::LCMPXCHG64, X86::MOV64rr,
7424 X86::NOT64r, X86::RAX,
7425 X86::GR64RegisterClass, true);
7426 case X86::ATOMMIN64:
7427 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7428 case X86::ATOMMAX64:
7429 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7430 case X86::ATOMUMIN64:
7431 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7432 case X86::ATOMUMAX64:
7433 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00007434
7435 // This group does 64-bit operations on a 32-bit host.
7436 case X86::ATOMAND6432:
7437 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7438 X86::AND32rr, X86::AND32rr,
7439 X86::AND32ri, X86::AND32ri,
7440 false);
7441 case X86::ATOMOR6432:
7442 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7443 X86::OR32rr, X86::OR32rr,
7444 X86::OR32ri, X86::OR32ri,
7445 false);
7446 case X86::ATOMXOR6432:
7447 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7448 X86::XOR32rr, X86::XOR32rr,
7449 X86::XOR32ri, X86::XOR32ri,
7450 false);
7451 case X86::ATOMNAND6432:
7452 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7453 X86::AND32rr, X86::AND32rr,
7454 X86::AND32ri, X86::AND32ri,
7455 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00007456 case X86::ATOMADD6432:
7457 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7458 X86::ADD32rr, X86::ADC32rr,
7459 X86::ADD32ri, X86::ADC32ri,
7460 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00007461 case X86::ATOMSUB6432:
7462 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7463 X86::SUB32rr, X86::SBB32rr,
7464 X86::SUB32ri, X86::SBB32ri,
7465 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007466 case X86::ATOMSWAP6432:
7467 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7468 X86::MOV32rr, X86::MOV32rr,
7469 X86::MOV32ri, X86::MOV32ri,
7470 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007471 }
7472}
7473
7474//===----------------------------------------------------------------------===//
7475// X86 Optimization Hooks
7476//===----------------------------------------------------------------------===//
7477
Dan Gohman8181bd12008-07-27 21:46:04 +00007478void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00007479 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00007480 APInt &KnownZero,
7481 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007482 const SelectionDAG &DAG,
7483 unsigned Depth) const {
7484 unsigned Opc = Op.getOpcode();
7485 assert((Opc >= ISD::BUILTIN_OP_END ||
7486 Opc == ISD::INTRINSIC_WO_CHAIN ||
7487 Opc == ISD::INTRINSIC_W_CHAIN ||
7488 Opc == ISD::INTRINSIC_VOID) &&
7489 "Should use MaskedValueIsZero if you don't know whether Op"
7490 " is a target node!");
7491
Dan Gohman1d79e432008-02-13 23:07:24 +00007492 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007493 switch (Opc) {
7494 default: break;
7495 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007496 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7497 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007498 break;
7499 }
7500}
7501
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007502/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007503/// node is a GlobalAddress + offset.
7504bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7505 GlobalValue* &GA, int64_t &Offset) const{
7506 if (N->getOpcode() == X86ISD::Wrapper) {
7507 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007508 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00007509 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007510 return true;
7511 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007512 }
Evan Chengef7be082008-05-12 19:56:52 +00007513 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007514}
7515
Evan Chengef7be082008-05-12 19:56:52 +00007516static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7517 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007518 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007519 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007520 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007521 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007522 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007523 return false;
7524}
7525
Dan Gohman8181bd12008-07-27 21:46:04 +00007526static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007527 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007528 SDNode *&Base,
7529 SelectionDAG &DAG, MachineFrameInfo *MFI,
7530 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007531 Base = NULL;
7532 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007533 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007534 if (Idx.getOpcode() == ISD::UNDEF) {
7535 if (!Base)
7536 return false;
7537 continue;
7538 }
7539
Dan Gohman8181bd12008-07-27 21:46:04 +00007540 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007541 if (!Elt.getNode() ||
7542 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007543 return false;
7544 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007545 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007546 if (Base->getOpcode() == ISD::UNDEF)
7547 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007548 continue;
7549 }
7550 if (Elt.getOpcode() == ISD::UNDEF)
7551 continue;
7552
Gabor Greif1c80d112008-08-28 21:40:38 +00007553 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007554 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007555 return false;
7556 }
7557 return true;
7558}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007559
7560/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7561/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7562/// if the load addresses are consecutive, non-overlapping, and in the right
7563/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007564static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007565 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007566 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00007567 MVT VT = N->getValueType(0);
7568 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007569 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007570 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007571 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007572 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7573 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007574 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007575
Dan Gohman11821702007-07-27 17:16:43 +00007576 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007577 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007578 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00007579 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00007580 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
7581 LD->getSrcValueOffset(), LD->isVolatile(),
7582 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007583}
7584
Evan Chengb6290462008-05-12 23:04:07 +00007585/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007586static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohman22cefb02009-01-29 01:59:02 +00007587 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng6617eed2008-09-24 23:26:36 +00007588 const X86Subtarget *Subtarget,
7589 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007590 unsigned NumOps = N->getNumOperands();
7591
Evan Chenge9b9c672008-05-09 21:53:03 +00007592 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007593 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007594 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007595
Duncan Sands92c43912008-06-06 12:08:01 +00007596 MVT VT = N->getValueType(0);
7597 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007598 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7599 // We are looking for load i64 and zero extend. We want to transform
7600 // it before legalizer has a chance to expand it. Also look for i64
7601 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007602 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007603 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007604 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007605 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007606 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007607
7608 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007609 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007610 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007611 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007612 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007613 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007614 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007615 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007616 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007617
7618 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007619 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007620
7621 // Load must not be an extload.
7622 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007623 return SDValue();
Mon P Wang28b36412009-01-30 07:07:40 +00007624
7625 // Load type should legal type so we don't have to legalize it.
7626 if (!TLI.isTypeLegal(VT))
7627 return SDValue();
7628
Evan Cheng6617eed2008-09-24 23:26:36 +00007629 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7630 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7631 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
Dan Gohman22cefb02009-01-29 01:59:02 +00007632 TargetLowering::TargetLoweringOpt TLO(DAG);
7633 TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1));
7634 DCI.CommitTargetLoweringOpt(TLO);
Evan Cheng6617eed2008-09-24 23:26:36 +00007635 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007636}
7637
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007638/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007639static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007640 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007641 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007642
7643 // If we have SSE[12] support, try to form min/max nodes.
7644 if (Subtarget->hasSSE2() &&
7645 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7646 if (Cond.getOpcode() == ISD::SETCC) {
7647 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007648 SDValue LHS = N->getOperand(1);
7649 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007650 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7651
7652 unsigned Opcode = 0;
7653 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7654 switch (CC) {
7655 default: break;
7656 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7657 case ISD::SETULE:
7658 case ISD::SETLE:
7659 if (!UnsafeFPMath) break;
7660 // FALL THROUGH.
7661 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7662 case ISD::SETLT:
7663 Opcode = X86ISD::FMIN;
7664 break;
7665
7666 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7667 case ISD::SETUGT:
7668 case ISD::SETGT:
7669 if (!UnsafeFPMath) break;
7670 // FALL THROUGH.
7671 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7672 case ISD::SETGE:
7673 Opcode = X86ISD::FMAX;
7674 break;
7675 }
7676 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7677 switch (CC) {
7678 default: break;
7679 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7680 case ISD::SETUGT:
7681 case ISD::SETGT:
7682 if (!UnsafeFPMath) break;
7683 // FALL THROUGH.
7684 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7685 case ISD::SETGE:
7686 Opcode = X86ISD::FMIN;
7687 break;
7688
7689 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7690 case ISD::SETULE:
7691 case ISD::SETLE:
7692 if (!UnsafeFPMath) break;
7693 // FALL THROUGH.
7694 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7695 case ISD::SETLT:
7696 Opcode = X86ISD::FMAX;
7697 break;
7698 }
7699 }
7700
7701 if (Opcode)
7702 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
7703 }
7704
7705 }
7706
Dan Gohman8181bd12008-07-27 21:46:04 +00007707 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007708}
7709
sampo025b75c2009-01-26 00:52:55 +00007710/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
7711/// when possible.
7712static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
7713 const X86Subtarget *Subtarget) {
7714 // On X86 with SSE2 support, we can transform this to a vector shift if
7715 // all elements are shifted by the same amount. We can't do this in legalize
7716 // because the a constant vector is typically transformed to a constant pool
7717 // so we have no knowledge of the shift amount.
sampo087d53c2009-01-26 03:15:31 +00007718 if (!Subtarget->hasSSE2())
7719 return SDValue();
7720
sampo025b75c2009-01-26 00:52:55 +00007721 MVT VT = N->getValueType(0);
sampo087d53c2009-01-26 03:15:31 +00007722 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
7723 return SDValue();
7724
Mon P Wanga91e9642009-01-28 08:12:05 +00007725 SDValue ShAmtOp = N->getOperand(1);
7726 MVT EltVT = VT.getVectorElementType();
7727 SDValue BaseShAmt;
7728 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
7729 unsigned NumElts = VT.getVectorNumElements();
7730 unsigned i = 0;
7731 for (; i != NumElts; ++i) {
7732 SDValue Arg = ShAmtOp.getOperand(i);
7733 if (Arg.getOpcode() == ISD::UNDEF) continue;
7734 BaseShAmt = Arg;
7735 break;
7736 }
7737 for (; i != NumElts; ++i) {
7738 SDValue Arg = ShAmtOp.getOperand(i);
7739 if (Arg.getOpcode() == ISD::UNDEF) continue;
7740 if (Arg != BaseShAmt) {
7741 return SDValue();
7742 }
7743 }
7744 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
7745 isSplatMask(ShAmtOp.getOperand(2).getNode())) {
7746 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, ShAmtOp,
7747 DAG.getIntPtrConstant(0));
7748 } else
sampo087d53c2009-01-26 03:15:31 +00007749 return SDValue();
sampo025b75c2009-01-26 00:52:55 +00007750
sampo087d53c2009-01-26 03:15:31 +00007751 if (EltVT.bitsGT(MVT::i32))
7752 BaseShAmt = DAG.getNode(ISD::TRUNCATE, MVT::i32, BaseShAmt);
7753 else if (EltVT.bitsLT(MVT::i32))
7754 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, BaseShAmt);
sampo025b75c2009-01-26 00:52:55 +00007755
sampo087d53c2009-01-26 03:15:31 +00007756 // The shift amount is identical so we can do a vector shift.
7757 SDValue ValOp = N->getOperand(0);
7758 switch (N->getOpcode()) {
7759 default:
7760 assert(0 && "Unknown shift opcode!");
7761 break;
7762 case ISD::SHL:
7763 if (VT == MVT::v2i64)
7764 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007765 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7766 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007767 if (VT == MVT::v4i32)
7768 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007769 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
7770 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007771 if (VT == MVT::v8i16)
7772 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007773 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
7774 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007775 break;
7776 case ISD::SRA:
7777 if (VT == MVT::v4i32)
7778 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007779 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
7780 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007781 if (VT == MVT::v8i16)
7782 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007783 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
7784 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007785 break;
7786 case ISD::SRL:
7787 if (VT == MVT::v2i64)
7788 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007789 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7790 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007791 if (VT == MVT::v4i32)
7792 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007793 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
7794 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007795 if (VT == MVT::v8i16)
7796 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007797 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
7798 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007799 break;
sampo025b75c2009-01-26 00:52:55 +00007800 }
7801 return SDValue();
7802}
7803
Chris Lattnerce84ae42008-02-22 02:09:43 +00007804/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007805static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007806 const X86Subtarget *Subtarget) {
7807 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7808 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007809 // A preferable solution to the general problem is to figure out the right
7810 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007811 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007812 if (St->getValue().getValueType().isVector() &&
7813 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007814 isa<LoadSDNode>(St->getValue()) &&
7815 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7816 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007817 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007818 LoadSDNode *Ld = 0;
7819 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007820 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007821 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007822 // Must be a store of a load. We currently handle two cases: the load
7823 // is a direct child, and it's under an intervening TokenFactor. It is
7824 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007825 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007826 Ld = cast<LoadSDNode>(St->getChain());
7827 else if (St->getValue().hasOneUse() &&
7828 ChainVal->getOpcode() == ISD::TokenFactor) {
7829 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007830 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007831 TokenFactorIndex = i;
7832 Ld = cast<LoadSDNode>(St->getValue());
7833 } else
7834 Ops.push_back(ChainVal->getOperand(i));
7835 }
7836 }
7837 if (Ld) {
7838 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7839 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007840 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007841 Ld->getBasePtr(), Ld->getSrcValue(),
7842 Ld->getSrcValueOffset(), Ld->isVolatile(),
7843 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007844 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007845 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00007846 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00007847 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7848 Ops.size());
7849 }
7850 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
7851 St->getSrcValue(), St->getSrcValueOffset(),
7852 St->isVolatile(), St->getAlignment());
7853 }
7854
7855 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00007856 SDValue LoAddr = Ld->getBasePtr();
7857 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007858 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007859
Dan Gohman8181bd12008-07-27 21:46:04 +00007860 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007861 Ld->getSrcValue(), Ld->getSrcValueOffset(),
7862 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007863 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007864 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
7865 Ld->isVolatile(),
7866 MinAlign(Ld->getAlignment(), 4));
7867
Dan Gohman8181bd12008-07-27 21:46:04 +00007868 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007869 if (TokenFactorIndex != -1) {
7870 Ops.push_back(LoLd);
7871 Ops.push_back(HiLd);
7872 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7873 Ops.size());
7874 }
7875
7876 LoAddr = St->getBasePtr();
7877 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007878 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007879
Dan Gohman8181bd12008-07-27 21:46:04 +00007880 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007881 St->getSrcValue(), St->getSrcValueOffset(),
7882 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007883 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00007884 St->getSrcValue(),
7885 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00007886 St->isVolatile(),
7887 MinAlign(St->getAlignment(), 4));
7888 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00007889 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00007890 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007891 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00007892}
7893
Chris Lattner470d5dc2008-01-25 06:14:17 +00007894/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7895/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007896static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00007897 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7898 // F[X]OR(0.0, x) -> x
7899 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00007900 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7901 if (C->getValueAPF().isPosZero())
7902 return N->getOperand(1);
7903 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7904 if (C->getValueAPF().isPosZero())
7905 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007906 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007907}
7908
7909/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007910static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007911 // FAND(0.0, x) -> 0.0
7912 // FAND(x, 0.0) -> 0.0
7913 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7914 if (C->getValueAPF().isPosZero())
7915 return N->getOperand(0);
7916 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7917 if (C->getValueAPF().isPosZero())
7918 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007919 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007920}
7921
Dan Gohman22cefb02009-01-29 01:59:02 +00007922static SDValue PerformBTCombine(SDNode *N,
7923 SelectionDAG &DAG,
7924 TargetLowering::DAGCombinerInfo &DCI) {
7925 // BT ignores high bits in the bit index operand.
7926 SDValue Op1 = N->getOperand(1);
7927 if (Op1.hasOneUse()) {
7928 unsigned BitWidth = Op1.getValueSizeInBits();
7929 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
7930 APInt KnownZero, KnownOne;
7931 TargetLowering::TargetLoweringOpt TLO(DAG);
7932 TargetLowering &TLI = DAG.getTargetLoweringInfo();
7933 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
7934 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
7935 DCI.CommitTargetLoweringOpt(TLO);
7936 }
7937 return SDValue();
7938}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007939
Dan Gohman8181bd12008-07-27 21:46:04 +00007940SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00007941 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007942 SelectionDAG &DAG = DCI.DAG;
7943 switch (N->getOpcode()) {
7944 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007945 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7946 case ISD::BUILD_VECTOR:
Dan Gohman22cefb02009-01-29 01:59:02 +00007947 return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007948 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
sampo025b75c2009-01-26 00:52:55 +00007949 case ISD::SHL:
7950 case ISD::SRA:
7951 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007952 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007953 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007954 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7955 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohman22cefb02009-01-29 01:59:02 +00007956 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007957 }
7958
Dan Gohman8181bd12008-07-27 21:46:04 +00007959 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007960}
7961
7962//===----------------------------------------------------------------------===//
7963// X86 Inline Assembly Support
7964//===----------------------------------------------------------------------===//
7965
7966/// getConstraintType - Given a constraint letter, return the type of
7967/// constraint it is for this target.
7968X86TargetLowering::ConstraintType
7969X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7970 if (Constraint.size() == 1) {
7971 switch (Constraint[0]) {
7972 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +00007973 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +00007974 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007975 case 'r':
7976 case 'R':
7977 case 'l':
7978 case 'q':
7979 case 'Q':
7980 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007981 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007982 case 'Y':
7983 return C_RegisterClass;
7984 default:
7985 break;
7986 }
7987 }
7988 return TargetLowering::getConstraintType(Constraint);
7989}
7990
Dale Johannesene99fc902008-01-29 02:21:21 +00007991/// LowerXConstraint - try to replace an X constraint, which matches anything,
7992/// with another that has more specific requirements based on the type of the
7993/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007994const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007995LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007996 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7997 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007998 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007999 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00008000 return "Y";
8001 if (Subtarget->hasSSE1())
8002 return "x";
8003 }
8004
8005 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00008006}
8007
Chris Lattnera531abc2007-08-25 00:47:38 +00008008/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8009/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00008010void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00008011 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00008012 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00008013 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00008014 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00008015 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00008016
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008017 switch (Constraint) {
8018 default: break;
8019 case 'I':
8020 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008021 if (C->getZExtValue() <= 31) {
8022 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008023 break;
8024 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008025 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008026 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00008027 case 'J':
8028 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8029 if (C->getZExtValue() <= 63) {
8030 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8031 break;
8032 }
8033 }
8034 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008035 case 'N':
8036 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008037 if (C->getZExtValue() <= 255) {
8038 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008039 break;
8040 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008041 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008042 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008043 case 'i': {
8044 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00008045 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008046 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008047 break;
8048 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008049
8050 // If we are in non-pic codegen mode, we allow the address of a global (with
8051 // an optional displacement) to be used with 'i'.
8052 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
8053 int64_t Offset = 0;
8054
8055 // Match either (GA) or (GA+C)
8056 if (GA) {
8057 Offset = GA->getOffset();
8058 } else if (Op.getOpcode() == ISD::ADD) {
8059 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8060 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8061 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008062 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008063 } else {
8064 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8065 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8066 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008067 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008068 else
8069 C = 0, GA = 0;
8070 }
8071 }
8072
8073 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00008074 if (hasMemory)
Dan Gohman36322c72008-10-18 02:06:02 +00008075 Op = LowerGlobalAddress(GA->getGlobal(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00008076 else
8077 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8078 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00008079 Result = Op;
8080 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008081 }
8082
8083 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00008084 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008085 }
8086 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008087
Gabor Greif1c80d112008-08-28 21:40:38 +00008088 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00008089 Ops.push_back(Result);
8090 return;
8091 }
Evan Cheng7f250d62008-09-24 00:05:32 +00008092 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8093 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008094}
8095
8096std::vector<unsigned> X86TargetLowering::
8097getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00008098 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008099 if (Constraint.size() == 1) {
8100 // FIXME: not handling fp-stack yet!
8101 switch (Constraint[0]) { // GCC X86 Constraint Letters
8102 default: break; // Unknown constraint letter
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008103 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8104 case 'Q': // Q_REGS
8105 if (VT == MVT::i32)
8106 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8107 else if (VT == MVT::i16)
8108 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8109 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00008110 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00008111 else if (VT == MVT::i64)
8112 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8113 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008114 }
8115 }
8116
8117 return std::vector<unsigned>();
8118}
8119
8120std::pair<unsigned, const TargetRegisterClass*>
8121X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00008122 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008123 // First, see if this is a constraint that directly corresponds to an LLVM
8124 // register class.
8125 if (Constraint.size() == 1) {
8126 // GCC Constraint Letters
8127 switch (Constraint[0]) {
8128 default: break;
8129 case 'r': // GENERAL_REGS
8130 case 'R': // LEGACY_REGS
8131 case 'l': // INDEX_REGS
Chris Lattnerbbfea052008-10-17 18:15:05 +00008132 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008133 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00008134 if (VT == MVT::i16)
8135 return std::make_pair(0U, X86::GR16RegisterClass);
8136 if (VT == MVT::i32 || !Subtarget->is64Bit())
8137 return std::make_pair(0U, X86::GR32RegisterClass);
8138 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00008139 case 'f': // FP Stack registers.
8140 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8141 // value to the correct fpstack register class.
8142 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8143 return std::make_pair(0U, X86::RFP32RegisterClass);
8144 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8145 return std::make_pair(0U, X86::RFP64RegisterClass);
8146 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008147 case 'y': // MMX_REGS if MMX allowed.
8148 if (!Subtarget->hasMMX()) break;
8149 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008150 case 'Y': // SSE_REGS if SSE2 allowed
8151 if (!Subtarget->hasSSE2()) break;
8152 // FALL THROUGH.
8153 case 'x': // SSE_REGS if SSE1 allowed
8154 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00008155
8156 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008157 default: break;
8158 // Scalar SSE types.
8159 case MVT::f32:
8160 case MVT::i32:
8161 return std::make_pair(0U, X86::FR32RegisterClass);
8162 case MVT::f64:
8163 case MVT::i64:
8164 return std::make_pair(0U, X86::FR64RegisterClass);
8165 // Vector types.
8166 case MVT::v16i8:
8167 case MVT::v8i16:
8168 case MVT::v4i32:
8169 case MVT::v2i64:
8170 case MVT::v4f32:
8171 case MVT::v2f64:
8172 return std::make_pair(0U, X86::VR128RegisterClass);
8173 }
8174 break;
8175 }
8176 }
8177
8178 // Use the default implementation in TargetLowering to convert the register
8179 // constraint into a member of a register class.
8180 std::pair<unsigned, const TargetRegisterClass*> Res;
8181 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8182
8183 // Not found as a standard register?
8184 if (Res.second == 0) {
8185 // GCC calls "st(0)" just plain "st".
8186 if (StringsEqualNoCase("{st}", Constraint)) {
8187 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00008188 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008189 }
Dale Johannesen73920c02008-11-13 21:52:36 +00008190 // 'A' means EAX + EDX.
8191 if (Constraint == "A") {
8192 Res.first = X86::EAX;
8193 Res.second = X86::GRADRegisterClass;
8194 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008195 return Res;
8196 }
8197
8198 // Otherwise, check to see if this is a register class of the wrong value
8199 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8200 // turn into {ax},{dx}.
8201 if (Res.second->hasType(VT))
8202 return Res; // Correct type already, nothing to do.
8203
8204 // All of the single-register GCC register classes map their values onto
8205 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8206 // really want an 8-bit or 32-bit register, map to the appropriate register
8207 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00008208 if (Res.second == X86::GR16RegisterClass) {
8209 if (VT == MVT::i8) {
8210 unsigned DestReg = 0;
8211 switch (Res.first) {
8212 default: break;
8213 case X86::AX: DestReg = X86::AL; break;
8214 case X86::DX: DestReg = X86::DL; break;
8215 case X86::CX: DestReg = X86::CL; break;
8216 case X86::BX: DestReg = X86::BL; break;
8217 }
8218 if (DestReg) {
8219 Res.first = DestReg;
8220 Res.second = Res.second = X86::GR8RegisterClass;
8221 }
8222 } else if (VT == MVT::i32) {
8223 unsigned DestReg = 0;
8224 switch (Res.first) {
8225 default: break;
8226 case X86::AX: DestReg = X86::EAX; break;
8227 case X86::DX: DestReg = X86::EDX; break;
8228 case X86::CX: DestReg = X86::ECX; break;
8229 case X86::BX: DestReg = X86::EBX; break;
8230 case X86::SI: DestReg = X86::ESI; break;
8231 case X86::DI: DestReg = X86::EDI; break;
8232 case X86::BP: DestReg = X86::EBP; break;
8233 case X86::SP: DestReg = X86::ESP; break;
8234 }
8235 if (DestReg) {
8236 Res.first = DestReg;
8237 Res.second = Res.second = X86::GR32RegisterClass;
8238 }
8239 } else if (VT == MVT::i64) {
8240 unsigned DestReg = 0;
8241 switch (Res.first) {
8242 default: break;
8243 case X86::AX: DestReg = X86::RAX; break;
8244 case X86::DX: DestReg = X86::RDX; break;
8245 case X86::CX: DestReg = X86::RCX; break;
8246 case X86::BX: DestReg = X86::RBX; break;
8247 case X86::SI: DestReg = X86::RSI; break;
8248 case X86::DI: DestReg = X86::RDI; break;
8249 case X86::BP: DestReg = X86::RBP; break;
8250 case X86::SP: DestReg = X86::RSP; break;
8251 }
8252 if (DestReg) {
8253 Res.first = DestReg;
8254 Res.second = Res.second = X86::GR64RegisterClass;
8255 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008256 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00008257 } else if (Res.second == X86::FR32RegisterClass ||
8258 Res.second == X86::FR64RegisterClass ||
8259 Res.second == X86::VR128RegisterClass) {
8260 // Handle references to XMM physical registers that got mapped into the
8261 // wrong class. This can happen with constraints like {xmm0} where the
8262 // target independent register mapper will just pick the first match it can
8263 // find, ignoring the required type.
8264 if (VT == MVT::f32)
8265 Res.second = X86::FR32RegisterClass;
8266 else if (VT == MVT::f64)
8267 Res.second = X86::FR64RegisterClass;
8268 else if (X86::VR128RegisterClass->hasType(VT))
8269 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008270 }
8271
8272 return Res;
8273}
Mon P Wang1448aad2008-10-30 08:01:45 +00008274
8275//===----------------------------------------------------------------------===//
8276// X86 Widen vector type
8277//===----------------------------------------------------------------------===//
8278
8279/// getWidenVectorType: given a vector type, returns the type to widen
8280/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8281/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +00008282/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +00008283/// scalarizing vs using the wider vector type.
8284
Dan Gohman0fe66c92009-01-15 17:34:08 +00008285MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang1448aad2008-10-30 08:01:45 +00008286 assert(VT.isVector());
8287 if (isTypeLegal(VT))
8288 return VT;
8289
8290 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8291 // type based on element type. This would speed up our search (though
8292 // it may not be worth it since the size of the list is relatively
8293 // small).
8294 MVT EltVT = VT.getVectorElementType();
8295 unsigned NElts = VT.getVectorNumElements();
8296
8297 // On X86, it make sense to widen any vector wider than 1
8298 if (NElts <= 1)
8299 return MVT::Other;
8300
8301 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
8302 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8303 MVT SVT = (MVT::SimpleValueType)nVT;
8304
8305 if (isTypeLegal(SVT) &&
8306 SVT.getVectorElementType() == EltVT &&
8307 SVT.getVectorNumElements() > NElts)
8308 return SVT;
8309 }
8310 return MVT::Other;
8311}